PHP Classes

http client

Recommend this page to a friend!

      PHP HTTP protocol client  >  All threads  >  http client  >  (Un) Subscribe thread alerts  
Subject:http client
Summary:Trying to send a file with no luck
Messages:13
Author:John Czenze
Date:2005-12-05 16:38:36
Update:2005-12-09 20:39:01
 
  1 - 10   11 - 13  

  1. http client   Reply   Report abuse  
Picture of John Czenze John Czenze - 2005-12-05 16:38:36
I am trying to use your class to send a file. After some digging I set the PostFiles array as follows:

$arguments["PostFiles"]= array('first'=>array('FileName'=>'c:\archive.zip','Name'=>'archive.zip'));

The request gets to the server but the script does not seem to have any entries in the $_FILES array to move the downloaded file.

Any suggestions would be greatly appreciated.

  2. Re: http client   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2005-12-05 22:13:12 - In reply to message 1 from John Czenze
This is hard to tell without seeing exactly what you are doing.

Can you please provide an example script that demonstrates what you are doing and is not working, and maybe a test site URL if the site you are posting is under your control?

  3. Re: http client   Reply   Report abuse  
Picture of John Czenze John Czenze - 2005-12-06 12:10:19 - In reply to message 2 from Manuel Lemos
I just updated your test script. The post gets to my internal server but as I mentioned the data is not saved or triggered by PHP
Ths client side script is first and after I have placed the server side script. Thanks for the reply I hope it helps.
as follows:

// This is the client side
?php
/*
* test_http_post.php
*
* @(#) $Header: /home/mlemos/cvsroot/http/test_http_post.php,v 1.5 2004/08/11 00:46:11 mlemos Exp $
*
*/

?><HTML>
<HEAD>
<TITLE>Test for Manuel Lemos' PHP HTTP class to simulate a HTTP POST form submission</TITLE>
</HEAD>
<BODY>
<H1><CENTER>Test for Manuel Lemos' PHP HTTP class to simulate a HTTP POST form submission</CENTER></H1>
<HR>
<UL>
<?php
require("includes/http.php");

set_time_limit(0);
$http=new http_class;
$http->timeout=0;
$http->data_timeout=0;
$http->debug=1;
$http->html_debug=1;

$url="http://192.168.0.213/cgi-bin/custodian/uploadfiles.php";
$error=$http->GetRequestArguments($url,$arguments);
$arguments["RequestMethod"]="POST";

echo "<H2><LI>Opening connection to:</H2>\n<PRE>",HtmlEntities($arguments["HostName"]),"</PRE>\n";
flush();
$error=$http->Open($arguments);

if($error=="")
{
$arguments["PostFiles"]= array('first'=>array('FileName'=>'c:\centraldb\custodian\archive.zip','Name'=>'archive.zip',
'Content-Type'=>'automatic/name'));
$error=$http->SendRequest($arguments);

// $arguments["Body"]= "0 admin admin 'TimeZone' 100000 1 1\r\n";
// $error=$http->PutData( $arguments["Body"]);
if($error=="")
{
echo "<H2><LI>Request:</LI</H2>\n<PRE>\n".HtmlEntities($http->request)."</PRE>\n";
echo "<H2><LI>Request headers:</LI</H2>\n<PRE>\n";
for(Reset($http->request_headers),$header=0;$header<count($http->request_headers);Next($http->request_headers),$header++)
{
$header_name=Key($http->request_headers);
if(GetType($http->request_headers[$header_name])=="array")
{
for($header_value=0;$header_value<count($http->request_headers[$header_name]);$header_value++)
echo $header_name.": ".$http->request_headers[$header_name][$header_value],"\r\n";
}
else
echo $header_name.": ".$http->request_headers[$header_name],"\r\n";
}
echo "</PRE>\n";
echo "<H2><LI>Request body:</LI</H2>\n<PRE>\n".HtmlEntities($http->request_body)."</PRE>\n";
flush();

$headers=array();
$error=$http->ReadReplyHeaders($headers);
if($error=="")
{
echo "<H2><LI>Response headers:</LI</H2>\n<PRE>\n";
for(Reset($headers),$header=0;$header<count($headers);Next($headers),$header++)
{
$header_name=Key($headers);
if(GetType($headers[$header_name])=="array")
{
for($header_value=0;$header_value<count($headers[$header_name]);$header_value++)
echo $header_name.": ".$headers[$header_name][$header_value],"\r\n";
}
else
echo $header_name.": ".$headers[$header_name],"\r\n";
}
echo "</PRE>\n";
flush();

echo "<H2><LI>Response body:</LI</H2>\n<PRE>\n";
for(;;)
{
$error=$http->ReadReplyBody($body,1000);
if($error!=""
|| strlen($body)==0)
break;
echo HtmlSpecialChars($body);
}
echo "</PRE>\n";
flush();
}
}
$http->Close();
}
if(strlen($error))
echo "<CENTER><H2>Error: ",$error,"</H2><CENTER>\n";
?>
</UL>
<HR>
</BODY>
</HTML>


Here is the server side
#! c:/www/php/php.exe

<?php


Error_log(" I am here");
foreach($_FILES as $key => $value)
{
error_log("$key $value");
}

$err = $_FILES['first']['error'];
error_log("error : $err");
if(is_uploaded_file($_FILES['first']['tmp_name']))
{
error_log(" Moving files");
move_uploaded_file($_FILES['first']['tmp_name'], "c:\Newfiles.zip");
}
print("0");
?>

  4. Re: http client   Reply   Report abuse  
Picture of John Czenze John Czenze - 2005-12-06 21:49:26 - In reply to message 3 from John Czenze
thought this would help. Here the debug info from the test program.
I am getting a reply from the script with an OK as well as the 0 I send back but again the $_Files array is empty


Connecting to HTTP server IP 192.168.0.213...
Connected to 192.168.0.213
C POST /cgi-bin/custodian/uploadfiles.php HTTP/1.1
C Host: 192.168.0.213
C User-Agent: httpclient (http://www.phpclasses.org/httpclient $Revision: 1.49 $)
C Content-Type: multipart/form-data; boundary=--b8720574ac4f8aeb7326726b260e805e
C Content-Length: 6372
C
C ----b8720574ac4f8aeb7326726b260e805e
Content-Disposition: form-data; name="first"; filename="archive.zip"
Content-Type: application/x-zip-compressed

  5. Re: http client   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2005-12-07 04:00:40 - In reply to message 3 from John Czenze
I tried this example and it works well as expected.

Maybe if you could put your script in a public server that I can access, then I can try and see what you get.

  6. Re: http client   Reply   Report abuse  
Picture of John Czenze John Czenze - 2005-12-07 14:07:34 - In reply to message 5 from Manuel Lemos
Unfornutely I do not access to a public server.
But here is what I have found so far.
I wrote a simple script to send a file.
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="sendfile.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

<?php

if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
move_uploaded_file($_FILES['userfile']['tmp_name'], "c:\Newfiles.zip");

}

?>

When I use this script all works as expected.

Using your class it seems that the data of the file is written as content to the script and not as a file downloadable by the web server.
I know this because using $_ENV['CONTENT_LENGTH'] using you class has the file data as well as header info etc.
While using the above script the $_ENV['CONTENT_LENGTH'] is 0 and the $_FILES contains the file info for the downloaded file.
If your class works where by it is necessary to get the file data as content that is fine but can you let me know if that is the intent or am I doing something wrong.
Thanks...


  7. Re: http client   Reply   Report abuse  
Picture of John Czenze John Czenze - 2005-12-07 18:41:05 - In reply to message 6 from John Czenze
I would really like PHP to handle the file download vs. gettng it a content.

One other thing I forgot to mention. The use of this script is really to get a file from 1 server to another. I am not sure if this is the best way.
Any suggestions would be greatlt appreciated.

  8. Re: http client   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2005-12-07 20:31:05 - In reply to message 6 from John Czenze
This suggests that the problem is that your PHP environment does not allow files as large as you want to send unless the MAX_FILE_SIZE parameter is passed.

In that case you only need to add something like this to the upload script as demonstrated in the test_http_post.php example.

$arguments["PostValues"]=array(
"MAX_FILE_SIZE"=>"30000"
);

  9. Re: http client   Reply   Report abuse  
Picture of John Czenze John Czenze - 2005-12-07 21:16:20 - In reply to message 8 from Manuel Lemos
I have used your test_http_post.php with the same results. Again the $_FILES varaible is empty while the $_ENV['CONTENT_LENGTH'] is the length of the data passed. So again all dat is passed as content instead as a file. Since I am using your test script. The only change I made is the file name. I can not understand what I amy be doing wrong.
Below is the result from you script with debugging:
Test for Manuel Lemos' PHP HTTP class to simulate a HTTP POST form submission

--------------------------------------------------------------------------------

Opening connection to:
192.168.0.213
Connecting to HTTP server IP 192.168.0.213...
Connected to 192.168.0.213
C POST /cgi-bin/custodian/uploadfiles.php HTTP/1.1
C Host: 192.168.0.213
C User-Agent: httpclient (http://www.phpclasses.org/httpclient $Revision: 1.49 $)
C Content-Type: multipart/form-data; boundary=--c0d30e1d18e9f45c063275ab8d83ec51
C Content-Length: 2999
C
C ----c0d30e1d18e9f45c063275ab8d83ec51
Content-Disposition: form-data; name="file"; filename="log.txt"
Content-Type: text/plain


C

ODBCConf called with arguments: '/A {REGMDACVERSION 2.8} /ld log.txt'
Data Struct:
Reboot First : 0
Use Response File : 0
Response File : '(null)'
Erase Response File: 0
Silent : 0
Continue on Error : 0
Log Mode : 3
Log File : 'log.txt'
Actions:
7, 'REGMDACVERSION', '2.8', '(null)', '2.8'
EXECUTING ACTIONS

Executing Action: REGMDACVERSION
arg1: '2.8'
arg2: '(null)'
args: '2.8'
REGMDACVERSION Version: '2.8'
REGMDACVERSION DO NOT update FullInstallVer value from '2.80.1022.3' to '2.8'
Return HR: 0x0


ODBCConf called with arguments: '/A {REGMDACVERSION 2.80.1022.3} /Ld log.txt'
Data Struct:
Reboot First : 0
Use Response File : 0
Response File : '(null)'
Erase Response File: 0
Silent : 0
Continue on Error : 0
Log Mode : 3
Log File : 'log.txt'
Actions:
7, 'REGMDACVERSION', '2.80.1022.3', '(null)', '2.80.1022.3'
EXECUTING ACTIONS

Executing Action: REGMDACVERSION
arg1: '2.80.1022.3'
arg2: '(null)'
args: '2.80.1022.3'
REGMDACVERSION Version: '2.80.1022.3'
REGMDACVERSION Update FullInstallVer value from '2.80.1022.3' to '2.80.1022.3'
REGMDACVERSION Setting FullInstallVer key to '2.80.1022.3'
Return HR: 0x0


ODBCConf called with arguments: '/a {REGSVR sqlsrv32.dll} /Ld c:\log.txt'
Data Struct:
Reboot First : 0
Use Response File : 0
Response File : '(null)'
Erase Response File: 0
Silent : 0
Continue on Error : 0
Log Mode : 3
Log File : 'c:\log.txt'
Actions:
3, 'REGSVR', 'sqlsrv32.dll', '(null)', 'sqlsrv32.dll'
EXECUTING ACTIONS

Executing Action: REGSVR
arg1: 'sqlsrv32.dll'
arg2: '(null)'
args: 'sqlsrv32.dll'
REGSVR About to CoLoadLibrary 'sqlsrv32.dll'
REGSVR HINSTANCE: 530317312
REGSVR Unable to get DLLRegisterServer address in 'sqlsrv32.dll': 203
REGSVR 'sqlsrv32.dll' Freed using CoFreeLibrary
Return HR: 0x0


ODBCConf called with arguments: '/a {REGSVR sqora32.dll} /Ld "c:\log.txt"'
Data Struct:
Reboot First : 0
Use Response File : 0
Response File : '(null)'
Erase Response File: 0
Silent : 0
Continue on Error : 0
Log Mode : 3
Log File : 'c:\log.txt'
Actions:
3, 'REGSVR', 'sqora32.dll', '(null)', 'sqora32.dll'
EXECUTING ACTIONS

Executing Action: REGSVR
arg1: 'sqora32.dll'
arg2: '(null)'
args: 'sqora32.dll'
REGSVR About to CoLoadLibrary 'sqora32.dll'
REGSVR HINSTANCE: 79691776
REGSVR Unable to get DLLRegisterServer address in 'sqora32.dll': 0
REGSVR 'sqora32.dll' Freed using CoFreeLibrary
Return HR: 0x0

C
C ----c0d30e1d18e9f45c063275ab8d83ec51
Content-Disposition: form-data; name="MAX_FILE_SIZE"


C 1000000
C
C ----c0d30e1d18e9f45c063275ab8d83ec51--

Request:
POST /cgi-bin/custodian/uploadfiles.php HTTP/1.1
Request headers:
Host: 192.168.0.213
User-Agent: httpclient (http://www.phpclasses.org/httpclient $Revision: 1.49 $)
Content-Type: multipart/form-data; boundary=--c0d30e1d18e9f45c063275ab8d83ec51
Content-Length: 2999

Request body:

S HTTP/1.1 200 OK
S Date: Wed, 07 Dec 2005 21:01:51 GMT
S Server: Apache/2.0.52 (Win32) mod_ssl/2.0.49 OpenSSL/0.9.7d PHP/5.0.2
S Transfer-Encoding: chunked
S Content-Type: application/x-httpd-php
S

Response headers:
http/1.1 200 ok:
date: Wed, 07 Dec 2005 21:01:51 GMT
server: Apache/2.0.52 (Win32) mod_ssl/2.0.49 OpenSSL/0.9.7d PHP/5.0.2
transfer-encoding: chunked
content-type: application/x-httpd-php

Response body:
S 1
S 0
S 0
0
Disconnected from 192.168.0.213


--------------------------------------------------------------------------------


  10. Re: http client   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2005-12-07 21:42:08 - In reply to message 9 from John Czenze
I realized that MAX_UPLOAD_FILE value must be sent before any files being uploaded.

I just uploaded a fixed version of the class that sends data by the correct order. Please try it now.

 
  1 - 10   11 - 13