Return to Snippet

Revision: 20613
at November 19, 2009 19:47 by benjaminpearson


Updated Code
<?php
$url = ''; // the url of the file processing script (ie, http://example.com/upload)

// Iterate through each file, check it has been stored in a tmp location, then post to another url
foreach ($_FILES as $file) {
  // tmp_name will look something like this /private/var/tmp/phpRDJDT92
  if ($file['tmp_name'] > '') {
    $params['file'] = '@'.$file['tmp_name']; // Its this @ symbol thats the key.
    $response = post($url, $params);
  }
}

function post($url, $params = array()) {
  try {
    $response = http_post($url, $params);
    return $response;
  } catch (Exception $e) {
    return null;
  }
}

// More REST methods available at http://snipplr.com/view/19781/php-rest/
// Nothing special about this cURL POST. You dont have to set any special headers for file uploading.
function http_post($url, $data) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, $url);
  curl_setopt($c, CURLOPT_POST, 1);
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($c, CURLOPT_POSTFIELDS, $data);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($c);
  curl_close($c);
  return $output;
}
?>

Revision: 20612
at November 19, 2009 19:46 by benjaminpearson


Initial Code
<?php
$url = ''; // the url of the file processing script (ie, http://example.com/upload)

// Iterate through each file, check it has been stored in a tmp location, then post to another url
foreach ($_FILES as $file) {
	// tmp_name will look something like this /private/var/tmp/phpRDJDT92
  if ($file['tmp_name'] > '') {
		$params['file'] = '@'.$file['tmp_name']; // Its this @ symbol thats the key.
		$response = post($url, $params);
  }
}

function post($url, $params = array()) {
	try {
		$response = http_post($url, $params);
		return $response;
	} catch (Exception $e) {
		return null;
	}
}

// More REST methods available at http://snipplr.com/view/19781/php-rest/
// Nothing special about this cURL POST. You dont have to set any special headers for file uploading.
function http_post($url, $data) {
	$c = curl_init();
	curl_setopt($c, CURLOPT_URL, $url);
 	curl_setopt($c, CURLOPT_POST, 1);
	curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
 	curl_setopt($c, CURLOPT_POSTFIELDS, $data);
	curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($c);
	curl_close($c);
	return $output;
}
?>

Initial URL

                                

Initial Description
The following are instructions to "re-post" files that have been already been POSTed to your php script. This can help if you want to have a form (with file upload) that POSTs its data to your own script where the text elements can be handled and then the script POSTs the file for processing/storage on another system.

Basically the key is that you only POST the `$_FILES['tmp_name']` with an "@" symbol prepended.

Spent a good hour trying to figure this out. Thanks to gordon who posted on 08-Sep-2004 10:08 at http://theserverpages.com/php/manual/en/ref.curl.php

Initial Title
PHP HTTP POST images & files

Initial Tags
curl, http, php

Initial Language
PHP