0

I've a function that post a file:

function post_file($url, $file_path){
    $data['Filedata'] = "@".$file_path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $response = curl_exec($ch);
    return $response;
}

Instead of passing a file path, I need to create on-the-fly the "file" storing it in a dummy variable (so, without writing it on the filesystem) and then to pass this file to curl_setopt().

How?

Geltrude
  • 1,063
  • 3
  • 16
  • 34
  • [file_get_contents](http://php.net/file_get_contents) – Dale Dec 06 '12 at 14:21
  • curl doesn't directly support uploading from variables instead of files. probably easier to work around it by doing a file_put_contents with a stream wrapper. more work, but doable. – Marc B Dec 06 '12 at 14:41
  • I don't think you can do it directly from a variable. You can, however, make a temp file with `tempnam()`. – Ranty Dec 06 '12 at 14:46

0 Answers0