I am asking the user to upload a file, through the following form making a POST Request to another page Upload.php
<form name="input" action="upload.php" method="post" enctype="multipart/form-data">
Username: <input type="text" name="username" /><br/>
<input type="file" name="file" /> <br/>
<input type="submit" value="Submit" />
</form>
At Upload.php, I use the data filled in the previous form to make an POST Request using Curl function for which I have written the following code:
$data = array(
"username" => $username,
"password" => $password,
"title" => $title,
"srcfile" => "@".$_FILES['file']['tmp_name']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response=curl_exec($ch);
echo $response;
This way I am able to upload the file successfully but the file uploaded has a name without any extension which is set by this
$_FILES[‘file’][‘tmp_name’]
and thus I am not able to use the file on the server.
How should I use the file obtained from the POST Request to make another POST Request?
Thanks in advance.
The problem in the above case is that though we can have the file using $_FILES[‘file’][‘tmp_name’], however, the file extension is not the same as that of original file and even the following code does not work which should run as filed in this bug https://bugs.php.net/bug.php?id=48962 in PHP 5.0
I have finally used the following solution to solve the above problem:
STEP 1: Save the file sent through the form’s POST request in a folder temporarily on the server or your machine.
STEP 2: Now use the path where file has been saved temporarily to make a POST request using CURL for which the code is used same as above mentioned with changes in file location.