PHP Curl Proxy, how to define $_FILES['foo']['name']
I'm using CURL to make a PHP Proxy that forward a multipart form.
The form have an input type="file".The proxy receive the following data:
array
'foo' =>
array
'name' => string 'wt.jpg' (length=6)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string '/tmp/phpoIvraK' (length=14)
'error' => int 0
'size' =>开发者_StackOverflow int 7427
So I'm expecting the page at the end to receive the same array (except for the tmp_name) Right now I receive this:
'foo' =>
array
'name' => 'phpoIvraK'
'type' => 'image/jpeg'
'tmp_name' => '/tmp/php5ZhCwy'
'error' => 0
'size' => 7427
As you can see, the name is now the tmp_name that the proxy is receiving, so the original filename is lost.
Here's the PHP Code:
$arrFields = $_POST;
foreach($_FILES as $k => $file)
$arrFields[$k] = '@' . $file['tmp_name'] . ';type=' . $file['type'];
curl_setopt($ressource, CURLOPT_POSTFIELDS, $arrFields);
Ps.: I know I could make a new variable, but the goal here is to respect the variable format, and make the usage of the proxy as transparent as possible for the person that'll use it.
Is it me or the only solution would be to rename the tmp file in the /tmp before resending it ? That looks a bit silly to me ...
$arrFields = $_POST;
foreach($_FILES as $k => $file){
move_uploaded_file($file['tmp_name'], $file['name']);
$arrFields[$k] = '@' . $file['name'] . ';type=' . $file['type'];
}
curl_setopt($ressource, CURLOPT_POSTFIELDS, $arrFields);
I think you shouldn't use the tmp file but instead store the file with it's real name somewhere, pass it to the POSTFIELDS param and after executing remove it. That seems to easiest way to me. curl doesn't care about the original request, it just does what you tell it to do and that is ... send the temp file over.
Faced similar situation and this is how I solved it
Pass the actual file name along with the request
$arrInputs["uploadvia"] = 'curl';
$arrInputs["actualfilename"]= $_FILES["upfile1"]["name"];
$arrInputs["upfile1"] = "@".$_FILES["upfile1"]["tmp_name"].";type=".$_FILES["upfile1"]["type"].";";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrInputs);
And at the other end, put this block
if($_POST['uploadvia'] == "curl")
{
$_FILES["upfile1"]["name"] = $_REQUEST['actualfilename'];
}
精彩评论