Question

My company has an html form to send variables and update image (jpeg) to the server, I tried to automate it using curl, but got stuck. I can send variables but not the image.

I used "tamper data" firefox and noticed that my form send the following variables to the server:

-----------------------------291231848620019\r\nContent-Disposition: form-data; name="pin"\r\n\r\ndf8794b1ec63c7094f6498f7a1322bcc\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="yourdata"\r\n\r\nonly test\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="your_file"; filename="chansy.jpg"\r\nContent-Type: image/jpeg\r\n\r\n{content-of-image-file-here}\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="birth_date[]"\r\n\r\n1\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="birth_date[]"\r\n\r\n1\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="birth_date[]"\r\n\r\n1970\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="your_email"\r\n\r\nkrunker@asia.com\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="code"\r\n\r\n\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="pass"\r\n\r\npass_sample\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="act"\r\n\r\nUpdate Changes\r\n-----------------------------291231848620019\r\nContent-Disposition: form-data; name="your_name"\r\n\r\njust me\r\n-----------------------------291231848620019--\r\n

here is the code i used on array postfields:

$img=file_get_contents($filename);    
$data = array('pin' => $pin, 'yourdata' => $_POST['yourdata'], 'your_file'=>$img, 'filename' =>$filename, 'Content-Type' => 'image/jpeg', 'birth_date[]' => $_POST['birth_date'], 'birth_date[]' => $_POST['birth_month'], 'birth_date[]' => $_POST['birth_year'], 'your_email' => $_POST['your_email'], 'pass' => $_POST['pass'], 'act' => 'Update changes', 'your_name' => $_POST['your_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch , CURL_HTTPHEADER , "Content-Type: multipart/form-data" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_REFERER, "http://internalweb.com/");
curl_setopt($ch, CURLOPT_URL, "http://internalweb.com/profile");
$page = curl_exec($ch);

On the result, when I run my script, it can update those variables, but no luck with the image.. i am so confuse... what should i do to put in variable?

I am learning everything from google, and now i am stuck since i dont have any knowledge in deep programming, can you please help me what to do to put image in $data array?

I tried to look on the thread of: multipart/form-data into array not processing multipart/form-data php curl Posting raw image data as multipart/form-data in curl

note: I tried to follow Hassan's suggestion, but not work still. Does anyone know how to convert this:

Content-Disposition: form-data; name="your_file"; filename="merc.jpg"
Content-Type: image/jpeg
$contentfile

into $data array for curl?

Était-ce utile?

La solution

When you upload a file to server using curl, you need to specify the @ sign before the file path. For example this image file:

$img = '/var/tmp/someimg.jpg';  // must be full path

Then in your post data, it should be:

'your_file' => '@' . $img,

Also, remove the following header from your curl code. Curl will automatically set this header with length based on your parameters.

curl_setopt($ch , CURL_HTTPHEADER , "Content-Type: multipart/form-data" );

Finally make sure your other parameter values are correct. And, also enable the verbose mode, so that you can see the output from curl curl_setopt($ch, CURLOPT_VERBOSE, 1);

Autres conseils

Ok ereveryone, I will close this issue and thank you so much for the help! Do really appreciate it :)

I tried to simulate it on my own server, it seem that i can successfully upload the file using curl *credit to hassan :) *

But dunno how my production's web / my company's website didnt receive the image file I sent via curl. Since the sourcecode of the destination is closed source, so i can not do much from my side except keep trying if I have something new come up lol.

so once again, thank you, you guys made me more understand what is curl.

Regards,

n3m0

I recently had a similar issue, I needed to upload files to this API :

http://www.sonicapi.com/docs/api/file-upload

Generally it is straight forward to upload but sometimes there are servers who need more information about what you're sending.

In my case it needed to have the ContentDisposition header Name property set to "file" and the FileName property set to the file name of the file being uploaded.

As soon as I've added the following code everything worked perfectly :

string name = Path.GetFileName(fileName);
streamContent.Headers.ContentDisposition = 
    new ContentDispositionHeaderValue("form-data")
{
    Name = "\"file\"",
    FileName = String.Format("\"{0}\"", name)
};
streamContent.Headers.ContentType = 
    new MediaTypeHeaderValue("application/octet-stream");

Now your problem is certainly not the same

Either you can get the docs about the service, or try the following :

Here's my approach on how I fixed the issue in my case.

  • I ran Fiddler in background while posting the form manually (from web browser).
  • I looked a the Raw tab in Inspectors to see what it was doing.
  • By comparing the same for my code and the web browser, I quickly figured out what was wrong

Just a guess it seems to be on an Intranet, we can't really take a look to tell what's wrong.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top