File upload does not work with cURL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

File upload does not work with cURL

Post by raghavan20 »

I am trying to upload a file using cURL.
but it did not seem to work. it calls the target file but does not send the file contents I suppose.
I have now included the file upload cURL script, file handler script, file to be uploaded and resultant string

of the operation.

cURL script: fileupload.php[EDITED]

Code: Select all

<?php

echo "\n current working directory of php: ".shell_exec('pwd');

$url = "http://suse/test/tempUpload.php";

$ch = curl_init();    // initialize curl handle
$fileToUpload =  "uploadthisfile.txt";
$fp = fopen( $fileToUpload, "r+");
echo "\n contents of file to be uploaded: ".file_get_contents( "uploadthisfile.txt" );


curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_INFILE, $fp );
curl_setopt($ch, CURLOPT_INFILESIZE, filesize( $fileToUpload ));
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "submit=upload&MAX_FILE_SIZE=40000000" );
curl_setopt($ch, CURLOPT_UPLOAD, 1);

$return = curl_exec( $ch  );
echo "\n error: ".curl_error( $ch );
echo "\n return from target file: ".$return;


curl_close( $ch );

?>
target file: tempUpload.php

Code: Select all

<?php

echo "i will upload your file";
print_R( $_FILES );
print_r( $_POST );


?>
file to be uploaded: uploadthisfile.txt

Code: Select all

hello

output:

Code: Select all

Suse:/opt/lampp/bin # ./php phpdocs/fileupload.php

 current working directory of php: /opt/lampp/bin

 contents of file to be uploaded: hello

 error:
 return from target file: i will upload your fileArray
(
)
Last edited by raghavan20 on Fri Aug 25, 2006 2:51 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't see CURLOPT_INFILESIZE...
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I have now included the CUROPT_INFILESIZE in the original code but result is the same.
You would be my best companion now if any of you can give me a working file upload example script?
Post Reply