PHP copy()-设置文件大小限制?
我正在使用一个脚本,其中我使用PHP函数PHP copy()-设置文件大小限制?,php,file,copy,Php,File,Copy,我正在使用一个脚本,其中我使用PHP函数copy()将图像从URL保存到我的服务器:copy('http://si.com/guitar.jpg“,'guitar1213.jpg') 我想知道的是,在调用此函数时,是否有任何方法可以简单地设置最大文件大小限制?或者说,.htaccess真的是我唯一可以快速修复此问题的选项吗 提前感谢您只能在文件位于服务器上后获取文件大小,我建议您将文件下载到临时文件夹,然后您可以轻松检查文件大小,并在满足要求的情况下移动到正确的位置 $original_path
copy()
将图像从URL保存到我的服务器:copy('http://si.com/guitar.jpg“,'guitar1213.jpg')
我想知道的是,在调用此函数时,是否有任何方法可以简单地设置最大文件大小限制?或者说,.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');
}
$original\u path='1http://si.com/guitar.jpg';
$temp_location='guitar1213.jpg';
$handle=fopen($temp_位置,“w+”);
fwrite($handle,file_get_contents($original_path));
fclose($handle);
if(文件大小($temp_位置)<1024000){
重命名($temp_location,'xxx');
}
玩弄了先查找文件大小,然后执行复制的想法:
<?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);
}
php.ini比.htaccess更容易编辑,但我知道这并不能回答您关于是否可以在代码本身中进行编辑的问题。
<?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);
}