Home  >  Article  >  Backend Development  >  Why does php use curl to upload files and it cannot be executed after deleting a sentence?

Why does php use curl to upload files and it cannot be executed after deleting a sentence?

WBOY
WBOYOriginal
2016-08-18 09:16:291138browse

<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>

The code is as above, why can’t I post after deleting ?

Reply content:

<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>

The code is as above, why can’t I post after deleting ?

Is there any error message? Post it and take a look

. . . There should be an error. Does the execution return nothing? Then we can only interrupt and see which step causes the problem

The uploaded file information should be stored in $_FILES and not in $_POST. Then there will be nothing in your post. Type $_POST in advance.
Why does php use curl to upload files and it cannot be executed after deleting a sentence?
I didn’t find the document description. I just tried it and set one for submit. name

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn