Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php使用curl上传文件为什么删掉一句无法执行

php使用curl上传文件为什么删掉一句无法执行

WBOY
WBOYasal
2016-08-18 09:16:291137semak imbas

<code>   <?php function upload_file($url,$filename,$path){
        $data = array('pic'=>'@'.realpath($path).";filename=".$filename);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $return_data = curl_exec($ch);
        curl_close($ch);
        $decode = json_decode($return_data);
        echo $decode->original_pic;      
   }

    if ($_POST) {
        $url = 'https://127.0.0.1/img.php';
        $tmpname = $_FILES['fname']['name'];
        $tmpfile = $_FILES['fname']['tmp_name'];
        upload_file($url,$tmpname,$tmpfile);
        exit;
    }
    echo '<form action="" enctype="multipart/form-data" method="post">
    <input type="text" name="fname">
  <input type="file" name="fname">
  <input type="submit" value="Submit">
</form>';
?>
</code>

代码如上,为什么删掉就无法执行post?

回复内容:

<code>   <?php function upload_file($url,$filename,$path){
        $data = array('pic'=>'@'.realpath($path).";filename=".$filename);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $return_data = curl_exec($ch);
        curl_close($ch);
        $decode = json_decode($return_data);
        echo $decode->original_pic;      
   }

    if ($_POST) {
        $url = 'https://127.0.0.1/img.php';
        $tmpname = $_FILES['fname']['name'];
        $tmpfile = $_FILES['fname']['tmp_name'];
        upload_file($url,$tmpname,$tmpfile);
        exit;
    }
    echo '<form action="" enctype="multipart/form-data" method="post">
    <input type="text" name="fname">
  <input type="file" name="fname">
  <input type="submit" value="Submit">
</form>';
?>
</code>

代码如上,为什么删掉就无法执行post?

有没有什么错误信息啊!贴出来看看

。。。应该有报错的啊,执行是空返回吗?那只能打断点看看那一步出问题

上传的文件信息应该是会存在放 $_FILES 而不会存在放$_POST里面 然后你的post里面不就没东西了提前打出$_POST
php使用curl上传文件为什么删掉一句无法执行
没有找到文档说明 只是试验了下 可以把submit设置一个name

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn