Php+curl uploading files to a remote server

Source: Internet
Author: User
Tags curl upload file
One, window under the installation of Php_curl support

1. Php_curl.dll the three files under the PHP installation folder (possibly in the Ext folder), Libeay32.dll,ssleay32.dll copy to%windir%/system32;
2. Open PHP.ini (may be in the installation directory of the PHP environment, by default in the C:\WINDOWS directory), the Extension=php_curl.dll head of the semicolon removed;

3. Restart the PHP service (Apache or IIS), such as restarting IIS and running IISRESET.

Second, attention matters.

Note the permissions for the server folder,

PHP.ini Upload_tmp_dir setting up a new directory also gives everyone read and write access.

The target directory for uploading files also gives everyone read and write access.

Third, directly on the code

1. File curltest.php:

  0, ' message ' = ' OK ');        Ini_set (' upload_max_filesize ', ' 20M ');        Ini_set (' post_max_size ', ' 20M ');        Ini_set (' Memory_limit ', ' 128M '); Receive the uploaded remote address $url = isset ($_post[' url ')?               Trim ($_post[' url '): ';        $url = "www.123.com/k1/upload.php";            if (empty ($url)) {$result [' code '] = 40001;            $result [' message '] = ' url cannot be empty ';        Helper_http::writejson ($result);        } $file = $_files[' file '];        Print_r ($_files);            if ($file [' ERROR ']! = 0) {$result [' code '] = 40001;            $result [' message '] = ' upload error ';            Helper_http::writejson ($result);        Echo 1;            } else {$filename = $file [' name '];            $tmpfile = $file [' Tmp_name '];            $filetype = $file [' type '];            $data = Upload_file ($url, $filename, $tmpfile, $filetype);            Echo $data;            Print_r ($url);        Die }}/** * Curl upload file *     * @param unknown $url * @param unknown $filename * @param unknown $path * @param unknown $type * *    function Upload_file ($url, $filename, $path, $type) {echo 3;        echo $path; PHP 5.5 Usage if (class_exists (' \curlfile ')) {$data = array (' file ' = ' = ' = ' = ' = ' = ' = ') ' (Realpath ($path)        , $type, $filename)); } else {$data = array (' file ' = ' @ '. Realpath ($path). "; Type= ". $type.";        Filename= ". $filename);        } echo ' data: ';        Print_r ($data);        $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);    echo $return _data; }?>

2, file upload. php:

 
  "; Print_r ($_files);  $uploaddir = ' d:/temp/caches/'; $uploadfile = $uploaddir. basename ($_files[' file ' [' name ']);  Echo '
'; if (move_uploaded_file ($_files[' file '] [' tmp_name '], $uploadfile) {echo "file is valid, and was successfully uploaded.\n "; } else {echo "Possible file Upload attack!\n";}  Echo ' Here's some more debugging info: ';?>


Reference links



http://bbs.csdn.net/topics/300018585

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.