0

我们有一个表格邮件程序,可以将失物招领的宠物信息发送到我们的电子邮件。我们还有一个图像上传功能,用于发送丢失宠物的图像。一位员工(早已离开)为我们编码。有时我们会使用相同的文件名(我怀疑是直接来自相机)上传多个图像,例如“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>";

    }
4

1 回答 1

0

一个快速而肮脏的解决方法是为每个图像名称添加一个时间戳。只需更改以下行:

$uploadFile = $uploadDir . "/" . basename( $_FILES["attach"]["name"] );

至:

$uploadFile = $uploadDir . "/" . `date + "%d%m%y_%H%M%S"` . basename( $_FILES["attach"]["name"] );

这假设您在 Linux 上运行您的应用程序。

于 2013-11-13T13:58:28.253 回答