Forums / Craftwork / Server-side / Sending a PHP generated image as multipart/form data

GHengeveld
#1 2009-03-13 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.)
Kilandor
#2 2009-03-13 04:17
Include the web URL of the image, but the script your posting to would have to allow for remote uploads.
GHengeveld
#3 2009-03-13 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.
Orkan
#4 2009-03-14 01:51
cURL is your friend ;-)
Perl - the only language that looks the same before and after RSA encryption.
GHengeveld
#5 2009-03-14 05:58
Please explain, I have no idea how to use this.

I've searched a bit and found the following example:
<?php
// 
// 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

   $uploadfile="/tmp/mydog.jpg"; 
   $ch = curl_init("http://formsite.com/somewhere.cgi");  
   curl_setopt($ch, CURLOPT_POSTFIELDS,
               array('sampfile'=>"@$uploadfile",
                     'shoesize'=>'9',
                     'filename'=>"fake name for file"));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $postResult = curl_exec($ch);
   curl_close($ch);
   print "$postResult";
}

?>

Could this be what I'm looking for?
oc
#6 2009-03-14 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.
Kilandor
#7 2009-03-14 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.

@kordahil

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.
Orkan
#8 2009-03-14 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.
Trustmaster
#9 2009-03-15 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!
GHengeveld
#10 2009-03-15 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 
title=".escapeshellarg($title)."
-Fsubmit=Verstuur"
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:
<?php
$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); 
curl_exec($ch);
curl_close($ch);
?>

For more information read the Hyves API MediaUpload documentation (english).
This post was edited by Koradhil (2009-03-15 07:09, 15 years ago)