2

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.

3
  • Hi, and welcome to StackOverflow! Looking at your code, you seem to have lacking understanding of how file uploads work in PHP. Take a look at the manual, try again and update your question if you still have difficulties achieving what you seek: php.net/manual/en/features.file-upload.post-method.php Commented Jan 24, 2012 at 10:56
  • Have also tried using "srcfile" => "@".$_FILES['srcfile']['tmp_name'].";type=".$_FILES['srcfile']['type'] but still not working Commented Jan 26, 2012 at 19:33
  • I came across this PHP bug though I am using PHP 5.3.1 bugs.php.net/bug.php?id=48962 Commented Jan 26, 2012 at 19:35

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

$_FILE['file'] is an array, which means complex types for SimpleXMLElement's properties.

Comments

1

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.

1 Comment

This answer won't work in my case as I have to also specify the file uploaded on the server as "srcfile" parameter in the POST Request accompained with the file name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.