= After the front-end post form uploads the file, the back end receives the file and forwards the post to the picture server. Using curl Upload, upload with ' @ file path '
The code is as follows
<?php if ($_files[' video ' [' Size ']>0) { $data = array (' Video ' =>$_files[' video ' [' Tmp_name ']); $ch = Curl_init (); $url = "test.php"; Set the URL and the corresponding options curl_setopt ($ch, Curlopt_url, $url); When enabled, the information for the header file is output as a data stream. curl_setopt ($ch, Curlopt_header, 0); The information obtained by CURL_EXEC () is returned as a string instead of the direct output curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, $data); $res = curl_exec ($ch); Fetch the URL and pass it to the browser curl_exec ($ch); Close the Curl resource and release the system resource curl_close ($ch); }
The PHP version of the test machine is 5.2 This code is fine, but on the line, the $_files variable is not receiving the value. Then carefully observed, in the $_post received $_post[' video ']= ' @/tmp/phps12d63 '; it's strange that the first reaction is that content-type is not properly transmitted, The content-type of the uploaded file should be ' Multipart/form-data ', which should be ' application/x-www-form-urlencoded '. Then print the $_server[' http_content_type ' variable on the image server and discover that it is ' multipart/form-data '.
Check the manual and found that if the pass parameter of Curlopt_postfields is an array, the Content-type header will be set to Multipart/form-data.
Then there is a magic curlopt_safe_upload parameter, added in php5.5.0, the default value is false,5.6.0 default is ture,
Curlopt_safe_upload
TRUE to disable the @ prefix to send files in Curlopt_postfields. means that @ can be used safely in a field. can be used
Curlfile as a substitute for uploading.
and the Curlopt_postfields parameter description
Curlopt_postfields
All data is sent using the "POST" action in the HTTP protocol. To send a file, precede the file name with the @ prefix and use the full path. The file type can be specified in the format '; Type=mimetype ' after the file name. This parameter can be a string after urlencoded, similar to ' Para1=val1¶2=val2& ... ', or you can use an array with field names as key values and field data as values. If value is an array, the Content-type header will be set to Multipart/form-data. Starting with PHP 5.2.0, when you pass a file with the @ prefix, value must be a number of arrays. Starting with PHP 5.5.0, the @ prefix has been deprecated and the file can be sent via Curlfile. Set Curlopt_safe_upload to TRUE to disable the @ prefix to send files for added security.
Check the version, sure enough the test environment is php5.3, and the online test environment is 5.6, also means that curlopt_safe_upload default is true, disable the @ Upload, @ is the normal string. From 5.5 onwards the @ prefix upload file has been deprecated. Larger than 5.5 versions are required to use curlfile uploads. The last compatibility scheme is less than 5.5 using the @ prefix, greater than 5.5 using Curlfile, in less than 5.5 version is the @ upload can also add additional parameters; filename= file name; Type=mime type
<?php//curl_file_create is the alias of the function curlfile::__construct () if (!function_exists (' curl_file_create ')) {function Curl_file_create ($filename, $mimetype = ", $postname =") { return @ $filename; Filename= . ($postname?: basename ($filename)) . ($mimetype? "; type= $mimetype": ");} }
The final code is
<?php if ($_files[' video ' [' Size ']>0) { $data = array (' Video ' =>$_files[' video ' [' Tmp_name ']); $ch = Curl_init (); $url = "test.php"; Set the URL and the corresponding options curl_setopt ($ch, Curlopt_url, $url); When enabled, the information for the header file is output as a data stream. curl_setopt ($ch, Curlopt_header, 0); The information obtained by CURL_EXEC () is returned as a string instead of the direct output curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_post, 1); $data [' Video ']=curl_file_create ($_files[' video '] [' tmp_name '], ' video/mp4 ', $_files[' video ' [' Name ']); curl_setopt ($ch, Curlopt_postfields, $data); $res = curl_exec ($ch); Fetch the URL and pass it to the browser curl_exec ($ch); Close the Curl resource and release the system resource curl_close ($ch); }
Summary, encounter problems, we must first calmly analysis, and then read the manual carefully.
Friday encountered a problem when the front-end post form uploads files after the backend receives the file and forwards the post to the picture server. Using curl Upload, upload with ' @ file path '
The code is as follows
<?php if ($_files[' video ' [' Size ']>0) {$data = array (' Video ' =>$_fil es[' video ' [' Tmp_name ']); $ch = Curl_init (); $url = "test.php"; Set the URL and the corresponding options curl_setopt ($ch, Curlopt_url, $url); When enabled, the information for the header file is output as a data stream. curl_setopt ($ch, Curlopt_header, 0); The information obtained by CURL_EXEC () is returned as a string instead of the direct output curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields, $data); $res = curl_exec ($ch); Fetch the URL and pass it to the browser curl_exec ($ch); Close the Curl resource and release the system Resource Curl_close ($CH); }
The PHP version of the test machine is 5.2 This code is fine, but on the line, the $_files variable is not receiving the value. Then carefully observed, in the $_post received $_post[' video ']= ' @/tmp/phps12d63 '; it's strange that the first reaction is that content-type is not properly transmitted, The content-type of the uploaded file should be ' Multipart/form-data ', which should be ' application/x-www-form-urlencoded '. Then print the $_server[' http_content_type ' variable on the image server and discover that it is ' multipart/form-data '.
Check the manual and found that if the pass parameter of Curlopt_postfields is an array, the Content-type header will be set to Multipart/form-data.
Then there is a magic curlopt_safe_upload parameter, added in php5.5.0, the default value is false,5.6.0 default is ture,
Curlopt_safe_upload
TRUE to disable the @ prefix to send files in Curlopt_postfields. means that @ can be used safely in a field. can be used
Curlfile as a substitute for uploading.
and the Curlopt_postfields parameter description
Curlopt_postfields
All data is sent using the "POST" action in the HTTP protocol. To send a file, precede the file name with the @ prefix and use the full path. The file type can be specified in the format '; Type=mimetype ' after the file name. This parameter can be a string after urlencoded, similar to ' Para1=val1¶2=val2& ... ', or you can use an array with field names as key values and field data as values. If value is an array, the Content-type header will be set to Multipart/form-data. Starting with PHP 5.2.0, when you pass a file with the @ prefix, value must be a number of arrays. Starting with PHP 5.5.0, the @ prefix has been deprecated and the file can be sent via Curlfile. Set Curlopt_safe_upload to TRUE to disable the @ prefix to send files for added security.
Check the version, sure enough the test environment is php5.3, and the online test environment is 5.6, also means that curlopt_safe_upload default is true, disable the @ Upload, @ is the normal string. From 5.5 onwards the @ prefix upload file has been deprecated. Larger than 5.5 versions are required to use curlfile uploads. The last compatibility scheme is less than 5.5 using the @ prefix, greater than 5.5 using Curlfile, in less than 5.5 version is the @ upload can also add additional parameters; filename= file name; Type=mime type
<?php//curl_file_create is the alias of the function curlfile::__construct () if (!function_exists (' curl_file_create ')) {function Curl_file_create ($filename, $mimetype = ", $postname =") { return @ $filename; Filename= . ($postname?: basename ($filename)) . ($mimetype? "; type= $mimetype": ");} }
The final code is
<?php if ($_files[' video ' [' Size ']>0) {$data = array (' Video ' =>$_fil es[' video ' [' Tmp_name ']); $ch = Curl_init (); $url = "test.php"; Set the URL and the corresponding options curl_setopt ($ch, Curlopt_url, $url); When enabled, the information for the header file is output as a data stream. curl_setopt ($ch, Curlopt_header, 0); The information obtained by CURL_EXEC () is returned as a string instead of the direct output curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_post, 1); $data [' Video ']=curl_file_create ($_files[' video '] [' tmp_name '], ' video/mp4 ', $_files[' video ' [' Name ']); curl_setopt ($ch, Curlopt_postfields, $data); $res = curl_exec ($ch); Fetch the URL and pass it to the browser curl_exec ($ch); Close the Curl resource and release the system Resource Curl_close ($CH); }