2013-04-03 289 views
-1

我的图片上传使用下面的代码ENCTYPE卷曲PHP

<form target="my_iframe" action="http://imagezilla.net/api.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file" accept="image/x-png, image/gif, image/jpeg" /> 
    <input type="hidden" name="apikey" value="" /> 
    <input type="hidden" name="testmode" value="1" /> 
    <input type="submit" value="Upload Image" /> 
</form> 

这工作很好,但我没有得到结果返回由于跨域规则的方式来imagezilla.net,所以我试图把它变成卷曲PHP

<?php 
$ch = curl_init("http://imagezilla.net/api.php?file='C:\Anti-Backlash-Nut.jpg'&apikey=''&testmode=1"); 
$header = array('Content-Type: multipart/form-data'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch);  
curl_close($ch); 
echo $output; 
?> 

(第二批的代码已经只是封闭的快速测试文件)
我不能在PHP代码的MIME类型工作正确(行$ header = ...),因为它只是没有文件上传。我究竟做错了什么?

回答

0

您正在使用GET方法上载文件...但文件始终得到使用后.. 对于你必须把@前上传您要发送的文件名称。

文件路径之前发送@确保了卷曲的文件发送为“多/表单数据”后的部分

试试这个

<?php 
$ch = curl_init("http://imagezilla.net/api.php?apikey=''&testmode=1"); 
$post = array(
    "file"=>"@C:\Anti-Backlash-Nut.jpg", 
); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch);  
curl_close($ch); 
echo $output; 
?> 
+0

我不得不将CURLOPT_HTTPHEADER,$标题更改为CURLOPT_HEADER, 0,因为它回来了一个错误,但现在当其成功输出为空时,如果有问题,则显示结果 – user170324

+0

@ user170324这可能是因为api可能不会返回任何成功上传 – alwaysLearn

+0

当我使用表单方法时,它会返回图像的服务器文件路径,但它确实将它作为xml返回,是否需要将标题更改回httpheader并将内容类型添加到xml? – user170324

1

这是你如何上传文件:

<?php 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    $post = array(
     "file"=>"@/path/to/myfile.jpg", 
    ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch); 
?> 
+0

如果我没”我会使用此代码t限制在我自己的服务器上的带宽,因此,为什么我使用imagezilla,感谢您的意见,将它作为一个储备,当这不是一个问题 – user170324