how to upload file using curl with PHP(如何使用 curl 和 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.
换句话说,用户在表单上看到一个文件上传按钮,该表单被发布到我的 php 脚本,然后我的 php 脚本需要将其重新发布到另一个脚本(例如在另一个服务器上).
In other words, the user sees a file upload button on a form, the form gets posted to my php 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>';
}
我希望代码将文件发送到接收文件.
I want the code to send the file to receiver file.
推荐答案
使用:
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);
也可以参考:
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
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 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 curl 和 PHP 上传文件


基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01