Get the App
SLTechnology News&Howtos  ›  Development  › 

How to upload http Files with php

Shulou Source: shulou.com Published: 2022-06-03 14:15:25 11月14日 Update

This article will explain in detail how to upload http files on php. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Initialize the cURL session.

2. Get the CURLFile instance.

3. Execute the given cURL session.

4. Get an instance of CURLFile class according to the file path.

5. Just set up the saved folder.

Example

/ / upload / * push the xls file * @ Date 2019-4-29 * / public function putXls () {/ / destination interface $url = "http://xxx";" to the destination address / / initialize the cURL session, which will be set to the value $ch = curl_init ($url) if the url,CURLOPT_URL option is provided; / / get the CURLFile instance $xlsCurlFile = $this- > makeCurlFile (base_path (). "/ public/tby.xls"); $data = array ('xls' = > $xlsCurlFile) / / TRUE will send a POST request of type: application/x-www-form-urlencoded, which is the most common one when HTML forms are submitted. Curl_setopt ($ch, CURLOPT_POST, 1); / / since PHP 5.5.0, the @ prefix has been deprecated and files can be sent via CURLFile. Setting CURLOPT_SAFE_UPLOAD to TRUE disables sending files with the @ prefix to increase security. Curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); / / returns TRUE on success of the given cURL session / / or returns FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, the function returns the result of execution on success and FALSE on failure. $result = curl_exec ($ch); if (curl_errno ($ch)) {/ / returns an error code or 0 (zero) if no error occurs. / / returns an error message, or returns''(empty string) if no error occurs. $result = curl_error ($ch);} / / close cURL session curl_close ($ch) } / * get an instance of CURLFile class according to the file path * @ param string $file file path * @ return CURLFile * @ Date 2019-4-29 * / private function makeCurlFile (string $file) {/ * * .xls mime is application/vnd.ms-excel * .xlsx mime is Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet * refer to https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types * * Note: you can also use the finfo class to get dynamic data. But you need to install the fileinfo extension * demo: $result = new finfo () If (is_resource ($result) = true) {return $result- > file ($filename, FILEINFO_MIME_TYPE);} return false; * / $mime = "application/vnd.ms-excel"; $info = pathinfo ($file); $name = $info ['basename']; $output = new CURLFile ($file, $mime, $name); return $output } / / the folder saved by public function getFile () {/ / on the receiving end, you need to pay attention to whether the user group has write permission $uploads_dir = base_path (). "/ public/test"; $xlsFiles = $_ FILES ["xls"]? Null; if ($xlsFiles) {if ($xlsFiles ["error"] = = UPLOAD_ERR_OK) {$tmp_name = $xlsFiles ["tmp_name"]; $name = $xlsFiles ["name"]; move_uploaded_file ($tmp_name, "$uploads_dir/$name") } / / todo success} else {/ / todo fail}} this is the end of the article on "how to upload http files by php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: File instance error article path success prefix folder more target reference good practical security code information content function dynamic address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple Redmi Microsoft vpn