Page 1 of 1

Multipart file form upload with curl

Posted: Sun Jun 14, 2009 1:35 pm
by piedrata
Hi all, first of all i am new here and hope that i can learn/help php in this forum:)
Well my problem is this, i am coding an script with php and curl that submits a csv file, but the problem is that it doesnt works, i mean it submits the file but it takes no effect.
Basically i want to upload the file "name.csv" using curl.
I got the POST sintaxis with live HTTPD headers (firefox) to add it to my curl function, the sintaxis is something like this:

Code: Select all

POST file.php -----------------------------257941320228008
 Content-Disposition: form-data; name="SETTINGS_FILE"; filename="name.csv"
 Content-Type: text/csv
 
 $CONTENT OF THE CSV FILE HERE
 
 -----------------------------257941320228008
 Content-Disposition: form-data; name="SubmitSettings"
 
 Upload Settings File
 -----------------------------257941320228008--
So my curl code is this one:

Code: Select all

 
$cah = curl_init();
curl_setopt($cah, CURLOPT_RETURNTRANSFER,1);
curl_setopt($cah, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($cah, CURLOPT_URL,"http://url/file.php");
curl_setopt($cah, CURLOPT_POST, 1);
curl_setopt($cah, CURLOPT_POSTFIELDS, "-----------------------------257941320228008
 Content-Disposition: form-data; name=".SETTINGS_FILE."; filename=".name.csv."
 Content-Type: text/csv
 
 $CONTENT OF THE CSV FILE HERE
 
 -----------------------------257941320228008
 Content-Disposition: form-data; name=".SubmitSettings."
 
 Upload Settings File
 -----------------------------257941320228008--");
$r = curl_exec ($cah);
curl_close ($cah);
unset($cah);
 
So thats all i can say, the curl does the post correctly, but without effects. :banghead:
I wonder how could i submit the file name.csv(which is hosted on the same server) using curl.

Thanks!:)

Re: Submitting a file with curl

Posted: Sun Jun 14, 2009 2:47 pm
by miro_igov
Read the examples here: viewtopic.php?f=1&t=96312

Re: Submitting a file with curl

Posted: Sun Jun 14, 2009 5:51 pm
by piedrata
Thanks miro_igov for the info, now i have this code:

Code: Select all

 
   $samplefile = "/var/www/html/name.csv";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
   curl_setopt($ch, CURLOPT_URL,"URL/upload.php");
   curl_setopt($ch, CURLOPT_COOKIEFILE, "cooka.txt");
   curl_setopt($ch, CURLOPT_POSTFIELDS, array('SETTINGS_FILE'=>"@$samplefile"));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $postResult = curl_exec($ch);
   curl_close($ch);
   echo $postResult;
 
The upload multipart form is this one:

Code: Select all

 
<form action="upload.php" class="cool" method="post" enctype="multipart/form-data" name="SubmitFile">
    <label for="file">CSV file</label> 
    <input name="SETTINGS_FILE" type="file" id="settingsfile" size="35" maxlength="100" /><br />
 
Well, the same problem as before, i get the post done, but with no results, i dont know where's the mistake. :banghead: :banghead:
Thanks!

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 2:51 am
by miro_igov
Inside your upload.php file move the uploaded file into dir with move_uploaded_file() then set the value of $samplefile to the full path to the just uploaded and moved file and execute thte curl snippet. Of course you have to adjust the url of the 3rd party site where you send the file via curl and adjust the name of the file input field to match the 3rd party form.

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 3:55 am
by piedrata
Well, the main problem is that i cannot edit the file upload.php, its enrypted with zend and i cannot edit anyway.
I didn't understood very well what you told me on the last message miro_igov, but where do you think its the problem? i mean, the curl is good, the name of the variable too, and the name of the input field too, so i don't know whats wrong.
Thanks!:)

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 4:13 am
by miro_igov
Are you sure you execute the curl snippet? If the form posts to uploads.php and you are unable to edit it, then where you placed the curl code? It must be placed in the processing file which takes the input of the form.

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 4:23 am
by piedrata
I execute the curl code from another file.
I have the upload.php file, which contains this form:

Code: Select all

 
<form action="upload.php" class="cool" method="post" enctype="multipart/form-data" name="SubmitFile">
<label for="file">CSV file</label>
<input name="SETTINGS_FILE" type="file" id="settingsfile" size="35" maxlength="100" /><br />
 
and when i upload a csv file into it, the live httpd headers says:

Code: Select all

 
POST file.php -----------------------------257941320228008
Content-Disposition: form-data; name="SETTINGS_FILE"; filename="name.csv"
 Content-Type: text/csv
 
 $CONTENT OF THE CSV FILE HERE
  
 -----------------------------257941320228008
 Content-Disposition: form-data; name="SubmitSettings"
 
  Upload Settings File
  -----------------------------257941320228008--
 
Then i have the curlfile.php, which i have this code there:

Code: Select all

 
   $samplefile = "/var/www/html/name.csv";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
   curl_setopt($ch, CURLOPT_URL,"URL/upload.php");
   curl_setopt($ch, CURLOPT_COOKIEFILE, "cooka.txt");
   curl_setopt($ch, CURLOPT_POSTFIELDS, array('SETTINGS_FILE'=>"@$samplefile"));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $postResult = curl_exec($ch);
   curl_close($ch);
   echo $postResult;
 
So the plan is to process the file name.csv into upload.php from curlfile.php
Thanks!

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 4:58 am
by miro_igov
Then it should be working fine. Check the write permissions of the destination dir and set the url to the correct one , not URL/upload.php

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 5:28 am
by piedrata
I have checked everything, the destination folder is 777 chmod'd, i can upload files doing it by browser and works fine, but don't know why i cant do it thru the curl file..:S
What else can i look?

Re: Multipart file form upload with curl

Posted: Mon Jun 15, 2009 5:29 am
by miro_igov
Look in your scripts :)