问题描述
我有一个非常简单的 PHP 代码来将文件上传到远程服务器;我这样做的方式(正如在其他一些解决方案中所建议的那样)是使用 cUrl 上传文件.
I had a very simple PHP code to upload a file to a remote server; the way I was doing it (as has been suggested here in some other solutions) is to use cUrl to upload the file.
这是我的代码:
$ch = curl_init("http://www.remotesite.com/upload.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileupload' => '@'.$_FILES['Filedata']['tmp_name']));
echo curl_exec($ch);
服务器正在运行 PHP 5.5.0,并且 @filename 似乎已在 PHP >= 5.5.0 中被弃用,如here 在 CURLOPT_POSTFIELDS
描述下,因此,我收到此错误:
The server is running PHP 5.5.0 and it appears that @filename has been deprecated in PHP >= 5.5.0 as stated here under the CURLOPT_POSTFIELDS
description, and therefore, I'm getting this error:
Deprecated: curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead in ...
有趣的是,除了基本的类概述之外,php.net 上绝对没有关于这个类的内容.没有示例,没有方法或属性的描述.它基本上是空白的这里.我知道这是一个全新的类,几乎没有文档,也很少在现实世界中使用,这就是为什么在 Google 或 Stackoverflow 上的搜索中几乎没有出现任何相关的类.
Interestingly, there is absolutely nothing about this Class on php.net aside from a basic class overview. No examples, no description of methods or properties. It's basically blank here. I understand that is a brand new class with little to no documentation and very little real-world use which is why practically nothing relevant is coming up in searches on Google or here on Stackoverflow on this class.
我想知道是否有人使用过这个 CURLFile 类并且可以帮助我或给我一个例子来在我的代码中使用它代替 @filename.
I'm wondering if there's anyone who has used this CURLFile class and can possibly help me or give me an example as to using it in place of @filename in my code.
我也想添加我的upload.php"代码;这段代码适用于传统的@filename 方法,但不再适用于 CURLFile 类代码:
I wanted to add my "upload.php" code as well; this code would work with the traditional @filename method but is no longer working with the CURLFile class code:
$folder = "try/";
$path = $folder . basename( $_FILES['file']['tmp_name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['tmp_name']). " has been uploaded";
}
最终编辑:
想要为其他人添加最终/工作代码,以寻找几乎没有记录的 CURLFile 类的类似工作示例......
Wanted to add Final / Working code for others looking for similar working example of the scarcely-documented CURLFile class ...
curl.php(本地服务器)
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="Filedata" id="Filedata" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if ($_POST['submit']) {
$uploadDir = "/uploads/";
$RealTitleID = $_FILES['Filedata']['name'];
$ch = curl_init("http://www.remotesite.com/upload.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$args['file'] = new CurlFile($_FILES['Filedata']['tmp_name'],'file/exgpd',$RealTitleID);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
}
?>
upload.php(远程服务器)
$folder = "try/";
$path = $folder . $_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
}
推荐答案
RFC 中有一段代码:https://wiki.php.net/rfc/curl-file-upload
There is a snippet on the RFC for the code: https://wiki.php.net/rfc/curl-file-upload
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile('filename.png', 'image/png', 'filename.png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
如果您有创建对象的恐惧症,您也可以使用看似毫无意义的函数 curl_file_create( string $filename [, string $mimetype [, string $postname ]] )
.
You can also use the seemingly pointless function curl_file_create( string $filename [, string $mimetype [, string $postname ]] )
if you have a phobia of creating objects.
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = curl_file_create('filename.png', 'image/png', 'filename.png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
这篇关于谁能给我一个 PHP 的 CURLFile 类的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!