如何上传文件使用curl与php [英] how to upload file using curl with php
问题描述
我想知道如何使用cURL或PHP中的任何其他内容上传文件。我已经在Google中搜索了很多次但没有结果。
I want to know how to upload file using cURL or anything else in PHP. I have searched in google many times but no results.
我有这个代码接收文件并上传
I have this code to receive the file and upload it
code:
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>';
}
我希望代码将文件发送到接收器文件。
I want the code to send the file to receiver file.
推荐答案
使用:
if (function_exists('curl_file_create')) { // php 5.6+
$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);
您也可以参考:
a href =http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/ =noreferrer> http:// blog .derakkilgo.com / 2009/06/07 / send-a-file-via-post-with-curl-and-php /
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
PHP 5.5 +的重要提示:
Important hint for PHP 5.5+:
现在我们应该使用 https://wiki.php.net/rfc/curl-file-upload ,但如果您仍想使用此已弃用的方法,则需要设置 curl_setopt($ ch,CURLOPT_SAFE_UPLOAD,false);
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);
这篇关于如何上传文件使用curl与php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!