Форуми / Craftwork / Server-side / Sending a PHP generated image as multipart/form data

#1 13.03.2009 04:07
I'm working on a script that generates a jpeg image using GD2. After generating the image I want to 'submit' it to an upload script that takes a multipart/form data image.
Normally this script will only accept images that are submitted using <input type="file" />, but I want to use the PHP generated image, without having to save the file to disk and manually select it for upload.

Is there any way to trick the upload script to think it is receiving image data instead of the image url? I have no control over the upload script, I can only call it with an html form, by providing an upload token and form data in the POST.

(To me more precise, I want to upload something to Hyves.nl using it's API.)
#2 13.03.2009 04:17
Include the web URL of the image, but the script your posting to would have to allow for remote uploads.
#3 13.03.2009 04:26
The script I'm submitting to doesn't take URLs, only form data.

Perhaps it is possible by not building a jpeg in the generator script, but just send the raw data. I doubt it will work, it's just something I came up with.
#4 14.03.2009 01:51
cURL is your friend ;-)
Perl - the only language that looks the same before and after RSA encryption.
#5 14.03.2009 05:58
Please explain, I have no idea how to use this.

I've searched a bit and found the following example:
// This sample shows how to fill in and submit data to a form that looks like:
//   <form enctype="multipart/form-data"
//       action="somewhere.cgi" method="post">
//   <input type="file" name="sampfile">
//   <input type="text" name="filename">
//   <input type="text" name="shoesize">
//   <input type="submit" value="upload">
//   </form>
// Pay attention to:
//   #1 - the input field names (name=)
//   #2 - the input field type so that you pass the upload file to the right
//        name
//   #3 - what URL to send the POST to. The action= attribute sets it.
// Author: Daniel Stenberg

   $ch = curl_init("http://formsite.com/somewhere.cgi");  
   curl_setopt($ch, CURLOPT_POSTFIELDS,
                     'filename'=>"fake name for file"));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $postResult = curl_exec($ch);
   print "$postResult";


Could this be what I'm looking for?
#6 14.03.2009 06:56
You should go on a PHP forum, this is not the place.

But, cURL is a library so not usable on every server. And even you accomplish what you try, probably server will ban you after short time.
#7 14.03.2009 07:32
You should go on a PHP forum, this is not the place.

Not true thats exactly what this area of the froums is for.


To use that script above, as oc said you have to have curl library installed on your host. Actually you would likely only get bannned due to spamming of such attempts.

But to use that script you will have to look at the site, and find the inputs and fill in the form input names and stuff. And have it set to fill in the appropriate data into those fields.

Why can you not jsut upload the file yourself, i'm assuming its a large image? I mean you could do a localhost and send it that way and still generate the image how you need too.
#8 14.03.2009 12:36
@Koradhil: yes, thats the rough example. Just before going online, Im always making a "target" script to test everything, from headers, user-agent to final data integrity - if they are equal to the results returned by a browser. Just use print_r($_SERVER), print_r($_FILES) etc...
Perl - the only language that looks the same before and after RSA encryption.
#9 15.03.2009 01:53
You need implementing HTTP client with multipart/form-data POST request encoding. I'd recommend learning something like PEAR::HTTP_Client and PEAR::HTTP_Request - it has built-in support for POSTing files.
May the Source be with you!
#10 15.03.2009 06:13
I've made a script that uses cURL that does what I want. The fact that it uses cURL is not a problem, because the employer of my project, namely Hyves.nl uses cURL to process my upload. Like this:
"curl http://".escapeshellarg($oXml->ip)."/upload?token=
".escapeshellarg(urlencode($oXml->token))." -F 
file=@".escapeshellarg($filename)." -F 
The final script will likely (depending on success of the project) be hosted on the same servers (a cluster of about 1500 servers actually), so cURL is definately supported.
My current testing script looks like this:
$ch = curl_init("http://somesite.com/upload.php"); // Link to upload script
curl_setopt($ch, CURLOPT_POST, true); // Set method to POST
curl_setopt($ch, CURLOPT_POSTFIELDS, array('uploaded'=>"@005.jpg")); // Form fields are passed in the array, form data is prefixed with @
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

For more information read the Hyves API MediaUpload documentation (english).

Відредаговано: Koradhil (15.03.2009 07:09, 15 років тому)