<?php
function getImg($url, $fileName=''){
if($url=='') die('地址引數錯誤');
if($fileName == ''){
$ext_name = strrchr($url, '.');
$allow_ext = ['.gif', '.jpg', '.gif', '.jpeg'];
if(!in_array($ext_name, $allow_ext)) die('檔案格式不對');
$new_file_name = time().$ext_name;
}
ob_start(); //打開快取
readfile($url);//讀入一個檔案并寫入到輸出緩沖,
$img_file = ob_get_contents(); //獲取快取保存到變數
ob_end_clean();//關閉并清除快取
$local = fopen($new_file_name, 'ab');
fwrite($local, $img_file);
fclose($local);
return $fileName;
}
echo getImg("http://www.mysqltutorial.org/wp-content/uploads/2008/05/mysqltutorial.gif");
/* 還有其他函式,比如:
file_get_contents();
file_put_contents();
*/

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59838.html
標籤:PHP
上一篇:春招面試、直接被面試官給吊打了?
