Php5.5.9curl

Source: Internet
Author: User
I found that my personal website has opened curl but cannot connect to any port externally. So there is still a way to use vps to build a temporary server, deprecated: curl_setopt (): Theusageofthe @ filenameAPIforfileuploadingisdeprec

I found that my personal website has opened curl but cannot connect to any port externally. So there is still a method for http: // to use vps to build a temporary server, deprecated: curl_setopt (): The usage of the @ filename API for file uploading is deprec

I found that my personal website has opened curl but cannot connect to any external port, so there is still a method of http: //, use vps to build a temporary server, use php curl to upload files to the server

Use the example on the Internet

Deprecated: Curl_setopt (): The usage of the @ filename API for file uploading is deprecated. Please use the CURLFile class instead in/Usr/local/apache2/htdocs/T1/php_curl_up1_1.phpOn line41

Okay, this is caused by version issues.

So I found the latest online document descriptions and examples.

Http://www.php.net/manual/zh/function.curl-setopt.php

Example?

Example #1 initialize a new cURL session and obtain a webpage

// Create a new cURL Resource
$ Ch = curl_init ();

// Set the URL and corresponding options
Curl_setopt ($ ch, CURLOPT_URL, "http://www.example.com /");
Curl_setopt ($ ch, CURLOPT_HEADER, false );

// Capture the URL and pass it to the browser
Curl_exec ($ ch );

// Disable cURL resources and Release System Resources
Curl_close ($ ch );
?>

Example #2 upload a file


/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/

$ch =curl_init();

$data = array('name'=>'Foo','file'=>'@/home/user/test.png');

curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

curl_exec($ch);
?>

The above routine will output:

Array(    [name] => Foo)Array(    [file] => Array        (            [name] => test.png            [type] => image/png            [tmp_name] => /tmp/phpcpjNeQ            [error] => 0            [size] => 279        ))

Comment?

Note:

Pass an arrayCURLOPT_POSTFIELDS, CURL will encode the data into multipart/form-data. When the operator passes a URL-encoded string, the data will be encoded into application/x-www-form-urlencoded.


As a result, I painted the following:

File to be uploaded

// Path to the file we want to upload
$ Uploaddir = getcwd ();
$ File = $ uploaddir. "/9N0CB31K28JU0007.jpg"; // This is very important! It must be an absolute address.


// Create a cURL handle
$ Ch = curl_init ('HTTP: // 192.168.5.41/T1/up2.php ');


// Create a CURLFile object
$ Cfile = curl_file_create ($ file );


// Assign POST data
$ Data = array ('fff' => $ cfile );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );

Curl_setopt ($ ch, CURLOPT_INFILESIZE, filesize ($ file); // This sentence is very important to tell the remote server, file size, found is the previous article http://blog.csdn.net/cyuyan112233/article/details/21015351


// Execute the handle
Curl_exec ($ ch );




?>






Content of the received File

Print_r ($ _ FILES );
$ Uploaddir = getcwd (). '/tmp/'; // a directory inside
Echo $ uploaddir ."
";
Echo $ _ FILES ["fff"] ["name"]."
";
$ File_name = basename ($ _ FILES ["fff"] ["name"]);
Echo $ file_name ."
";
Move_uploaded_file ($ _ FILES ['fff'] ['tmp _ name'], $ uploaddir. $ file_name );


// Test whether the write permission exists.
// $ Fp = fopen ("tmp.txt", "wb ");
// Fwrite ($ fp, "abc \ r \ n ");
// Fclose ($ fp );
?>



That's all you need !~~ Haha ~

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.