Assume that the server-side upload file processing script upload.php:
-
-
- print_r($_POST);
- print_r($_FILES);
Copy code
1. Use CURL default method
- //If the php file is utf8 encoded and the system is GBK encoded, then you need to change the encoding, otherwise Php cannot find this file in the system
- $ file = realpath(mb_convert_encoding('Test image.JPG','GBK','utf8'));
-
- $file = realpath('temp.jpg'); //File to be uploaded
- $fields['f' ] = '@'.$file; // Add the @ symbol in front to indicate uploading pictures
-
- $ch =curl_init();
-
-
- curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php'); ECurl_Setopt ($ ch, curlopt_post, true); Rue); c $ content = curl_exec ($ ch);
-
- echo $content;
-
-
- Copy code
-
-
-
-
2.
An alternative approach, sometimes we need to upload dynamically generated content to a remote server as a file, but we don’t want to build a temporary file in the local server. This way we have this alternative way of writing
$contents =<<< 'TEXT'This is the file content, or it can be a binary image. The image needs to be modified to upload the file type TEXT;
$varname = 'my';//Upload To the key in the $_FILES array $name = '3.txt';//File name $type = 'text/plain';//File type-
- $key = "$varname"; filename="$namernContent -Type: $typern";
- $fields[$key] = $contents;
-
-
-
- $ch =curl_init();
-
-
- curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php' );
-
- curl_setopt($ch,CURLOPT_POST,true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
-
-
- $content = curl_exec($ch);
-
- echo $content;
-
-
- Copy code
-
-
-
-
Upload files, php, curl
|