0. References
1. Download Remote Files
1190000003109808
2. Determine if a remote file exists
Http://www.manongjc.com/article/1415.html
3. Get the file encoding
http://blog.csdn.net/zhezhebie/article/details/72732453
4. File encoding
http://blog.csdn.net/u014231144/article/details/76077353
1, PHP upload files
PHP backstage Get upload file, use global variable $_files get upload file.
$schemaTmpFile = $_FILES[‘owndata_schema‘];# 文件属性array(5) { ‘name‘ => string(12) "desc-foo.csv" ‘type‘ => string(24) "application/vnd.ms-excel" ‘tmp_name‘ => string(19) "/data/tmp/php8zDKJi" ‘error‘ => int(0) ‘size‘ => int(180)}
File properties:
- 1, name: subscript refers to the file name of the upload file.
- 2,type: subscript refers to the type of upload file.
- 3, Tmp_name: subscript refers to the uploaded file on the server side of the temporary file name.
- 4, Error: The subscript is the upload file error representation, can be used to detect whether the file upload, in order to find the cause of the error.
- 5,Size: subscript is the size of the upload file, in bytes.
2. PHP Pull Remote Files
If the file is a remote file, you first need to determine if the remote file really exists.
① to determine if a remote file exists
Curl is proven to be the most reliable method.
/** * 判断远程文件是否存在 * @param $remoteFile * @return bool */ public function remoteFileExists($remoteFile) { $curl = curl_init($remoteFile); // CURLOPT_NOBODY设置为true,因为不需要真正的读取文件内容,只需要判断是否存在 curl_setopt($curl, CURLOPT_NOBODY, true); $result = curl_exec($curl); $ret = false; //if request did not fail if ($result !== false) { //if request was ok, check response code $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($statusCode == 200) { $ret = true; } } curl_close($curl); return $ret; }
② getting the remote file size
/** * 获取远程文件大小 * @param $remoteFile * @return mixed */ public static function getRemoteFileSize($remoteFile) { $headerInfo = get_headers($remoteFile,true); return $headerInfo[‘Content-Length‘]; }
Get_headers (): Returns an array that contains the headers sent by the server in response to an HTTP request. You can get the file size from the Content-length field of the header information.
The demo sample is as follows:
$remoteFile = "http://km.oa.com/files/photos/pictures/201801/1516586415_55_w157_h211.jpg";$headerInfo = get_headers($remoteFile,true);var_dump($headerInfo);// 打印结果如下:array(11) { [0] => string(15) "HTTP/1.1 200 OK" ‘Server‘ => string(12) "nginx/1.10.1" ‘Date‘ => string(29) "Wed, 24 Jan 2018 09:04:03 GMT" ‘Content-Type‘ => string(10) "image/jpeg" ‘Content-Length‘ => string(5) "15893" ‘Last-Modified‘ => string(29) "Mon, 22 Jan 2018 02:00:14 GMT" ‘Connection‘ => string(5) "close" ‘ETag‘ => string(15) ""5a6545ae-3e15"" ‘Expires‘ => string(29) "Fri, 23 Feb 2018 09:04:03 GMT" ‘Cache-Control‘ => string(15) "max-age=2592000" ‘Accept-Ranges‘ => string(5) "bytes"}
3. Download Remote Files
Download remote file ideas:
- 1. Get the contents of the file and write to the server local address.
- 2, large files, need to read the Shard.
/** * Download File & Save file to disk * @param string $FILEURL * @return bool|string */protected function Downfil E ($fileUrl = ") {$time = time (); $taskDir = File_upload_base_path. '/owndata_schema '; if (!is_dir ($taskDir)) {if (mkdir ($taskDir)) {log::info ("Create dir succ:". $taskDir); } else {log::info ("Create dir fail:". $taskDir); $this->err_msg = "File path creation failed"; return false; }} $pathParts = PathInfo ($FILEURL); $fileName = $pathParts [' basename ']; $fileName = $time. ‘_‘ . $fileName; $SAVEFILEURL = $taskDir. '/'. $fileName; The following core code//fopen () can be read into large files and can be specified to read a portion of the content at a time. It is also useful when working with large files. $RFP = fopen ($FILEURL, "RB"); $WFP = fopen ($saveFileUrl, "A +"); while (!feof ($RFP)) {$tmpContent = Fread ($RFP, 1024*8); Fwrite ($WFP, $tmpContent); } fclose ($RFP); Fclose ($WFP); Return disk temporary storage file path return $SAVEFILEURL; }
Summary: When using PHP to download files, you should pay attention to the scene. If it is only a few small files are downloaded, it is better to use PHP download, but if PHP to withstand a large number of download requests, then download the file should not be given to PHP, should be taken asynchronously to download.
4. File encoding
When getting an upload file or remote file, there is a time when you need to process the encoding of the file.
① getting the file encoding function
Mb_detect_encoding ()
② file BOM header for UTF-8 files
/** * UTF8 去掉文本中的 bom头 * @param $contents * @return mixed */ private function clearBom($contents) { $bom = chr(239) . chr(187) . chr(191); return str_replace($bom, ‘‘, $contents); } /** * 文件编码格式处理 * @param $fileUrl */ private function clearFileBom($fileUrl) { $encodeList = [‘GBK‘, ‘UTF-8‘]; $contents = file_get_contents($fileUrl); $encode = mb_detect_encoding($contents, $encodeList); if ($encode == ‘UTF-8‘) { $contents = $this->clearBom($contents); file_put_contents($fileUrl, $contents); } else { $contents = StringUtil::gbkToutf8($contents); file_put_contents($fileUrl, $contents); } }
PHP files that stuff