我们有一个表格邮件,可以将丢失的和发现的宠物信息发送到我们的电子邮件中。我们还有一个图像上传功能,用于发送丢失的宠物的图像。一名员工(早已离开)为我们编码。有时我们会使用相同的文件名(直接来自我怀疑的相机)获取多个图像上传,例如“image.jpg” - 因此在我们添加到列表之前覆盖之前的图像 - 我会感谢一些有关修改现有图像上传代码的帮助(下面)使用时间和日期后缀重命名重复文件。这有可能与我们有什么关系吗?我担心我们不是编码员,所以可能比其他人更依赖于细节。
非常感谢你!
/* Set upload directory */
$uploadDir = "/home/hsoet/public_html/uploads/attach";
$baseUrl = "http://www.petsfurpeople.org/uploads/attach";
$uploadFile = "";
/* Setup acceptable attachment types */
$acceptableTypes = array(
"image/gif",
"image/jpeg",
"image/jpg"
);
/* There was a file uplaoded but an error occured.. */
if( $_FILES["attach"]["error"] > 0 && $_FILES["attach"]["error"] != UPLOAD_ERR_NO_FILE ) {
echo "<h4>Error uploading attachment (file size too large possibly!)</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
exit();
}
/* File was uploaded, handle... */
else if( $_FILES["attach"]["error"] == UPLOAD_ERR_OK ) {
/* Check to make sure that the file is an acceptable type */
if( !in_array( $_FILES["attach"]["type"], $acceptableTypes ) ) {
echo "<h4>Unacceptable attachment type. Please try again!</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
exit();
}
$uploadFile = $uploadDir . "/" . basename( $_FILES["attach"]["name"] );
$attachUrl = $baseUrl . "/" . basename( $_FILES["attach"]["name"] );
if( !move_uploaded_file( $_FILES["attach"]["tmp_name"], $uploadFile ) ) {
echo "<h4>Error uploading attachment. Please try again!</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
exit();
}
}
并通过电子邮件和其他表单信息向我们发送文件链接。
elseif (mail($toemail,"HSOET Lost Pet Form Submission","\nEmail: ".$fromemail."\nName: ".$name."\nPhone: ".$phone."\nType of Animal: ".$type."\nArea Lost: ".$area."\nDate Lost: ".$date."\nBreed: ".$breed."\nSex: ".$sex."\nAltered: ".$altered."\nCollar Color: ".$collarcolor."\nTag Number: ".$tag."\nPet's Name: ".$petname."\nDescription: ".stripslashes($description)."\nFile Attachment: ".$attachUrl."\nIP Address: ".$pfw_ip."",$headers2)) {
mail($fromemail,"HSOET Lost Pet Form Submission Confirmation",$c_message,$headers);
echo "<meta http-equiv=\"Refresh\" content=\"0;url=".$c_url."\">";
echo "<h4>If you are not redirected automatically, <a href=\"".$c_url."\">click here</a>.";
} else {
echo "<h4>Can't send email to $email</h4>";
}
答案 0 :(得分:0)
快速而又脏的修复方法是为每个图像名称添加时间戳。只需更改以下行:
$uploadFile = $uploadDir . "/" . basename( $_FILES["attach"]["name"] );
为:
$uploadFile = $uploadDir . "/" . `date + "%d%m%y_%H%M%S"` . basename( $_FILES["attach"]["name"] );
这假设您在Linux上运行您的应用程序。