我有一個腳本,我使用PHP函數copy()
,以便從URL圖像保存到我的服務器的工作:copy('http://si.com/guitar.jpg', 'guitar1213.jpg')
PHP複製() - 設置文件大小限制?
什麼我不知道是如果有任何辦法,我可以簡單地設置一個最大文件大小調用此函數時限制?或者真的是.htaccess
我唯一的選擇來快速解決這個問題?
在此先感謝
我有一個腳本,我使用PHP函數copy()
,以便從URL圖像保存到我的服務器的工作:copy('http://si.com/guitar.jpg', 'guitar1213.jpg')
PHP複製() - 設置文件大小限制?
什麼我不知道是如果有任何辦法,我可以簡單地設置一個最大文件大小調用此函數時限制?或者真的是.htaccess
我唯一的選擇來快速解決這個問題?
在此先感謝
只能獲取文件大小,一旦該文件是您的服務器上,我建議下載文件到臨時文件夾,然後你可以輕鬆地檢查文件的大小,並移動到正確的如果它符合要求,則位置。
$original_path = 'http://si.com/guitar.jpg';
$temp_location = 'guitar1213.jpg';
$handle = fopen($temp_location, "w+");
fwrite($handle, file_get_contents($original_path));
fclose($handle);
if (filesize($temp_location) < 1024000){
rename($temp_location, 'xxx');
}
那麼這可能會訣竅,是的,歡呼。 –
$limit = 1024; //1KB
$fr = fopen($filePath, 'r');
$limitedContent = fread($fr, $limit);
$fw = fopen($filePath, 'w');
fwrite($fw, $limitedContent);
與查找文件的大小,然後再進行復制的想法玩弄:
<?php
if (false !== ($f = fopen($url, 'rb'))) {
// read the meta data from the file, which contains the response headers
$d = stream_get_meta_data($f);
// find the Content-Length header
if ($headers = preg_grep('/^Content-Length: /i', $d['wrapper_data'])) {
$size = substr(end($headers), 16);
// if the size is okay, open the destination stream
if ($size <= 10000 && false !== ($o = fopen('destination.jpg', 'wb'))) {
// and perform the copy
stream_copy_to_stream($f, $o);
fclose($o);
}
}
fclose($f);
}
買者
它不會工作如果服務器不返回Content-Length
標題;這是一個可能需要處理的可能性。
php.ini比.htaccess更容易編輯,但我知道這不會回答你的問題,它是否可以在代碼本身完成。 – Scott