Reputation: 41
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.
Upvotes: 2
Views: 177
Reputation: 41
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
"srcfile" => "@".$_FILES['file']['tmp_name'].";filename=".$_FILES['file']['name'].";type=".$_FILES['file']['type']
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.
"srcfile" => "@".temp_file_path
Upvotes: 2
Reputation: 4456
You should read the file, encode it and assign the encoded value to the XML element. For example you can use something like:
$xml->file = base64_encode( file_get_contents( $_FILE['file'][ 'tmp_name' ] ) );
Of course in a real code you should add error checking etc.
Upvotes: 1
Reputation: 160923
$_FILE['file']
is an array, which means complex types
for SimpleXMLElement
's properties.
Upvotes: 1