【发布时间】:2012-03-22 22:19:52
【问题描述】:
尝试通过 curl 发送文件
$file = array('file' => "@".realpath('my.jpg').";type=image/jpeg" );
$test = curlPOST($file, "http://site.ru/upload/", "http://site.ru", "c.txt" ,'', 60, 1);
echo $test;
function curlPOST($post_data, $url, $referrer='', $cookie_file='', $proxy='', $timeout=60, $header=0) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Opera/9.10 (Windows NT 5.1; U; en)");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_ENCODING,'gzip,deflate');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: image/jpeg'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
if($referrer) curl_setopt($ch, CURLOPT_REFERER, $referrer);
if($header) curl_setopt($ch, CURLOPT_HEADER, 1);
if($proxy) {curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass");
//curl_setopt($ch, CURLOPT_PROXYAUTH, 1);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if($cookie_file) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
}
$content=curl_exec($ch);
curl_close($ch);
return $content;
}
如果在没有 ;type=image/jpeg 的情况下发送,则会返回白屏,我会收到无法加载文件的错误消息。在加载站点通过 ajax 发生。该图像也是具有正确 cookie 的文件。我做错了什么?
【问题讨论】:
-
您删除 MIME 类型并收到未找到文件错误的事实表明您的文件名/文件路径有问题。仔细检查它是否在正确的位置
-
我试过指定完整路径,文件就是这样。例如: "@C:\\AppServ\\www\\test\\1.jpg" ,因此作为响应,如果您将 minetype 添加到文件 curl 错误-创建表单数据失败,我会收到文件未加载跨度>
-
我尝试复制您的代码并上传到我自己的网络服务器,它工作正常。我正在使用 OSX,但我会看看您的文件名的不同选项。也可能是您要上传到的服务器的问题?
-
在你的 linux 服务器上再试一次,还是不行。这里的 http 转储看起来像实时 http 标头中的查询 - pastebin.com/8ajmnWrx 我第一次得到的键的值
-
老实说,看起来它正在工作。它正在发送 9831 个字节并收到 200 个响应代码。是什么让你相信这行不通?