100

How can I upload a file, either by using cURL or anything else, in PHP?

In other words, the user sees a file upload button on a form, the form gets posted to my PHO script, then my PHP script needs to re-post it to another script (eg on another server).

I have this code to receive the file and upload it:

echo"".$_FILES['userfile']."";
$uploaddir = './';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if ( isset($_FILES["userfile"]) ) {
    echo '<p><font color="#00FF00" size="7">Uploaded</font></p>';
    if (move_uploaded_file
($_FILES["userfile"]["tmp_name"], $uploadfile))
echo $uploadfile;
    else echo '<p><font color="#FF0000" size="7">Failed</font></p>';
}

How can I send the file to the receiver server?

3
  • 5
    not with "ftp" , i want to send the file with curl in $_FILES['userfile'] Commented Mar 4, 2013 at 11:44
  • Uhm...what now? Where do you want to send it? What's your target system? Commented Mar 4, 2013 at 11:47
  • to php file (source in question) - target system is linux Commented Mar 4, 2013 at 12:05

3 Answers 3

193

Use:

if (function_exists('curl_file_create')) { // php 5.5+
  $cFile = curl_file_create($file_name_with_full_path);
} else { // 
  $cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

You can also refer:

http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

Important hint for PHP 5.5+:

Now we should use https://wiki.php.net/rfc/curl-file-upload but if you still want to use this deprecated approach then you need to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

Sign up to request clarification or add additional context in comments.

14 Comments

Maybe, is a better way use the built-in feature from curl: php.net/manual/es/function.curl-file-create.php. Of course, you can use the way of POSTFIELDS and fill the value prepending with @. Anyway, the asnwer is copied from a custom usage of curl from a blog. The correct answer is say that the @ char defines it as a file, not a var. $post will contain @filename.jpg by example.
@erm3nda That's PHP 5.5+ only.
@fiXedd im currently using php 5.6, and using curl_file_create is required (the sollution provided by karthik is not working). So the code should be upgraded to something like this: if function_exists('curl_file_create')) { $cFile = curl_file_create($dest); } else { $cFile = '@' . realpath($dest); }
What is extra_info => 123456 used for?
this solution stopped working in php 5.6, the solution is in adding file as: new CURLFile(realpath($fileName));
|
11

For those using php >= 5.5, CURLFile can be used:

$curlFile = new \CURLFile('test.txt', 'text/plain', 'test.txt');

$ch = curl_init('http://example.com/upload.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'file' => $curlFile,
]);

$result = curl_exec($ch);

if ($result === false) {
    echo 'upload - FAILED' . PHP_EOL;
}

From php 8.1, the file can only reside in memory if desired using CURLStringFile:

$txt_curlfile = new \CURLStringFile('test content', 'test.txt', 'text/plain');

$ch = curl_init('http://example.com/upload.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'file' => $txt_curlfile
]);

$result = curl_exec($ch);

if ($result === false) {
    echo 'upload - FAILED' . PHP_EOL;
}

Reference: https://php.watch/versions/8.1/CURLStringFile

Comments

1
$filePath = 'your_file_full_path';
$image = fopen($filePath, "rb");
$ch = curl_init();

// If authorization is needed, then use CURLOPT_HTTPHEADER //
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer your_token'
]);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'upload_url');
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $image);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

Comments

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.