【发布时间】:2020-10-09 00:34:23
【问题描述】:
您好,有人可以帮助我使用这段代码来处理和测试我在 PHPMailer 中的文件上传吗?它基本上检查文件是否正确,然后使用用户名加上字段名和日期和时间重命名文件名。有多个文件,但我没有使用多上传器,而是使用单独的字段,以便我可以跟踪哪个文件是哪个。
脚本似乎可以工作,并且 php 错误日志中没有错误,但我被告知这是我上一篇文章中的一个安全漏洞,我的“pathinfo 调用应该测试实际文件的 tmp_name 的路径”而不是给定的原始名称。这是一个严重的安全漏洞。”
不幸的是,我不确定 pathinfo 的两种用法中的哪一种是错误的。如果我将 $file["name"] 更改为 $file["tmp_name"] for $imageFileExt 那么我没有得到文件扩展名,如果我将 $file["name"] 更改为 $file["tmp_name"] on $imageFileType 然后我得到了错误的文件错误。任何帮助将非常感激。谢谢。
foreach ( $_FILES as $key => $file ) {
//get the file extension
$imageFileExt = strtolower( pathinfo( $file["name"], PATHINFO_EXTENSION ) );
//change the name of each file in $_FILES array to
//the persons name plus file field plus date plus time plus file extension
//such as :joe_bloggs_bank_statement_1_9_10_21_10_55.jpg
//and joe_bloggs_pay_slip_1_9_10_21_10_55.jpg
$file['name'] = clean($_POST['appName']). "_" . $key . "_" . $dateKey . "." . $imageFileExt;
// get the file type
$target_file = $target_dir . basename($file["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// get file size
$check = getimagesize($file["tmp_name"]);
if($check === false) {
$fileMessage .= $key."=noimage,";
$uploadOk = 0;
}
// Allow certain file formats
else if($imageFileType !== "jpg" && $imageFileType !== "png" && $imageFileType !== "jpeg"
&& $imageFileType !== "gif" ) {
$fileMessage .= $key."=wrongfile,";
$uploadOk = 0;
}
// Check if file already exists
else if (file_exists($target_file)) {
$fileMessage .= $key."=fileexists,";
$uploadOk = 0;
}
// Check file size
else if ($file["size"] > 20000000) { //20mb
$fileMessage .= $key."=toobig,";
$uploadOk = 0;
}
// creates a set of links to the uploaded files on the server
// to be placed in the body of the email (the files themselves do not get attached in the email
$fileString .= strtoupper($key).": <a href='example.com/uploads/".$file['name']."'>".$file['name']."</a><br>";
}
【问题讨论】:
标签: php file-upload phpmailer