I am submitting a file with the following html code to a php file. What I want to do is that get that file in php and then set the file as a parameter's value for cURL as postfields and then execute the url, How can I do that?
Here is the html :
<form name="frm" id="frm" method="post" action="fileSubmit.php" enctype="multipart/form-data">
<input type="file" name="file" id="file"/>
<input type="submit" name="submit" value="submit" />
</form>
Here is the php
<?php
if(isset($_REQUEST['submit'])) {
$curl = curl_init("myDomain/submitFile";);
$file = "file=".file_get_contents($_FILES["file"]["name"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
$resp = curl_exec($curl);
curl_close($curl);
}
?>
Thanks in advance!
try
$_FILES["file"]["tmp_name"]
instead, until you move the uploaded file it is stored in a temporary location
There are several things here that need fixing... Change your code to:
$postParams = array(
"@file" => $_FILES["file"]["tmp_name"],
"name" => $_FILES["file"]["name"]
);
$curl = curl_init("http://remote-site.com/upload-script.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
// curl_setopt($curl, CURLOPT_TIMEOUT, 60); // yes, this must be commented out. It will stop the upload otherwise.
curl_setopt($curl, CURLOPT_POST, 1); // post must be enabled.
curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams);
$resp = curl_exec($curl);
curl_close($curl);
Tested here and works perfectly.