In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how php implements the function of uploading http files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
How to upload php http files?
PHP uses CURL (HTTP) to upload files to another server.
The code is as follows:
/ / upload / * push the xls file * @ Date 2019-4-29 * / public function putXls () {/ / destination interface $url = "http://xxx"; / / initialize the cURL session. If the url,CURLOPT_URL option is provided, it will be set to the value $ch = curl_init ($url). / / get CURLFile instance $xlsCurlFile = $this- > makeCurlFile (base_path (). "/ public/tby.xls"); $data = array ('xls' = > $xlsCurlFile); / / TRUE sends 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 * @ param string $file file path * @ return CURLFile * @ Date 2019-4-29 * / private function makeCurlFile (string $file) {/ * * .xls mime is application/vnd.ms-excel * .xl sx 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 the receiver public function getFile () {/ / needs 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}} "how php implements the function of uploading http files" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.