我正在尝试将 pdf 或 jpg、jpeg 文件上传到一个文件夹,代码如下:
//Get the uploaded file information
if(!$_FILES['medreport']['error'])
{
$medreport = basename($_FILES['medreport']['name']);
$medreport_extn = substr($medreport, strrpos($medreport, '.') + 1);//get the file extension of the file
$medreport_size = $_FILES["medreport"]["size"]/1024;//size in KBs
$tmp_path = $_FILES["medreport"]["tmp_name"];
$report_folder = "../reports/";
//Settings
$max_allowed_file_size = 200; // size in KB
$allowed_extensions = array("jpg", "jpeg", "pdf");
//Validations
}
if($medreport_size > $max_allowed_file_size )
{
$error[] = "Size of the report file should be less than $max_allowed_file_size KB";
}
//Validate the file extension
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$medreport_extn) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$error[] = "The uploaded report file is not a supported file type. "."Only pdf, jpg and jpeg report file types are supported. ";
}
//replace filename with unixtime
$unixtime =time();
$medreport = $unixtime.mt_rand(0,9).'.'.$medreport_extn;
$report_path = $report_folder . $medreport;
if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$report_path))
{
$error[] = 'Error while copying the uploaded report file';
}
}
在尝试上传具有正确扩展名和大小的文件时,我可以上传它。
但是,如果我尝试上传过大或格式不正确的文件,它会显示我的错误消息,但该文件始终会上传到该文件夹。
为什么会这样??请问,我的代码有什么问题??
方式,我这样做足够安全吗?该文件夹归 www-data 所有,权限为 755。我在文件上传文件夹中也有一个 .htaccess 文件,以防止可执行文件如下:
SetHandler none
SetHandler default-handler
Options -ExecCGI
php_flag engine off
总是上传的文件让我很困惑。
您没有使用刚刚发现的错误来检查是否需要继续。
这:
if(is_uploaded_file($tmp_path))
应该是这样的:
if(count($error) === 0 && is_uploaded_file($tmp_path))
$error
如果你还没有这样做,你应该在开始时将你的数组初始化为一个空数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句