如何使用curl上传远程文件

时间:2014-04-24 15:24:50

标签: php file curl

我想知道我在PHP的以下脚本中做错了:

    <!DOCTYPE html>
<html>
<head>
    <title>TEST</title>
    <meta charset="utf-8">
</head>
<body>
<?php
    set_time_limit(0);
    $url = "http://www0.xup.in/exec/xupload.php?uid=&key=";
    $user_agent = "Mozilla/5.0 (Windows NT 6.1; rv:28.0) Gecko/20100101 Firefox/28.0";
    $referer = "http://www.xup.in/";
    if(isset($_FILES['f1'])){
        move_uploaded_file($_FILES['f1']['tmp_name'],"files/".$_FILES['f1']['name']);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        //curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array("name"=>"F1","filename"=>"@"."files/".$_FILES['f1']['name']));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        //unlink("files/".$_FILES['file']['name']);
        echo $result;
    }else{
        echo '
            <form action="upload_curl.php" method="post" enctype="multipart/form-data">
                <label for="f1">Filename:</label>
                <input type="file" name="f1" id="f1"><br>
                <input type="submit" name="submit" value="Submit">
            </form>

        ';  
    }
?>
</body>
</html>

我花费更多,我尝试它给我没有结果应该,我想要的是使用curl上传本地图像到该网站并显示结果,但它显示空白而不是我不我知道为什么,我认为我的逻辑是好的,我希望有人可以解决一些问题并告诉我错误,问候和提前感谢所有人。

1 个答案:

答案 0 :(得分:0)

您永远不会设置要进行curl调用的URL,要么使用curl_setopt()来执行此操作,要么将其作为第一个参数curl_init()传递。在您的情况下:curl_init($url);

最后,您应该检查curl_error($ch)是否有错误并对其进行处理(另外,它可能会帮助您在开发代码时告诉您代码的错误。)

相关问题