Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Daf
    Daf is offline
    Regular Coder
    Join Date
    Feb 2005
    Location
    East Texas
    Posts
    140
    Thanks
    32
    Thanked 2 Times in 2 Posts

    Image upload - renaming to prevent overwrite

    Hi all!

    We have a simple mailer script we use to allow folks to upload a photo with their lost/found pet submission. It's a standard form mailer with an image uploader. The problem we have is that most folks don't rename their images so we get a lot of image.jpg or similar straight from the camera. This causes problems as the uploader overwrites and we lose the original.

    My question is would one of you be so kind as to show me how to set it up to rename the uploaded photo? I'll put the image bit we have below. I'm hoping this can be done. Everything below the code below is standard form mailer. I can attach the entire script if necessary.

    Thank you!!

    Code:
    /* 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();
        }
    }

  • #2
    Daf
    Daf is offline
    Regular Coder
    Join Date
    Feb 2005
    Location
    East Texas
    Posts
    140
    Thanks
    32
    Thanked 2 Times in 2 Posts
    Anyone? Maybe it can't be done? Thanks anyway.

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You may append the user_id & timestamp with each image name to make them unique.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Daf (11-13-2013)

  • #4
    Daf
    Daf is offline
    Regular Coder
    Join Date
    Feb 2005
    Location
    East Texas
    Posts
    140
    Thanks
    32
    Thanked 2 Times in 2 Posts
    Thank you, abduraooft! I don't know much about coding PHP - would you please give me some terms or keywords so I can search how to do that? I would appreciate any help in getting this done.

    Thanks again!

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Try
    PHP Code:
    $uploadFile $uploadDir "/".$_SESSION['user_id'] .time(). basename$_FILES["attach"]["name"] ); 
    Refer http://php.net/manual/en/function.uniqid.php too.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Daf (11-14-2013)

  • #6
    Daf
    Daf is offline
    Regular Coder
    Join Date
    Feb 2005
    Location
    East Texas
    Posts
    140
    Thanks
    32
    Thanked 2 Times in 2 Posts
    Thank you again, abduraooft!

    I was able to get the uniqid(True) to work in that the images are now being uploaded with a unique file name - that is good! An example is:

    15284b6f52d875image.jpg

    A bit long but it will get the job done.

    Okay next step is to get that new filename e-mail to us. It's still e-mailing the original file name - not the one that PHP is renaming.

    Here is the new code I used:
    Code:
    $uploadFile = $uploadDir . "/" . uniqid(True) . basename( $_FILES["attach"]["name"] );
        $attachUrl = $baseUrl . "/" . basename( $_FILES["attach"]["name"] );
    $attachUrl is what get's attached to the e-mail back to us. How can I modify that to send the modified filename?

    Below is the entire script in case you need it.

    Thanks!

    Code:
    <?php
    
    /* 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 uploaded 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 . "/" . uniqid(True) . basename( $_FILES["attach"]["name"] );
        $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();
        }
    }
    
    /* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. In the section below you can change out who the form will be sent to in $toemail = 'contact@ktbb.com';, below that you can change who the confirmation email is coming from and what you would like the confirmation email to say. For multiple forms doing different things you should probably save this page over many times like mail.php, mail1.php, mail2.php et cetera. If you add extra fields you would add them below. Copy and paste a line like $message = $HTTP_POST_VARS['message']; and change it to something like... $website = $HTTP_POST_VARS['website']; At the bottom of this section you'll see a web address this is the where you'll change the address for the thank you page you want to use. */
    
    $pfw_ip= $_SERVER['REMOTE_ADDR'];
    
    $toemail = 'lostandfound@petsfurpeople.org';
    
    $fromemail = $_POST['fromemail'];
    
    $name = $_POST['name'];
    
    $phone = $_POST['phone'];
    
    $type = $_POST['type'];
    
    $area = $_POST['area'];
    
    $date = $_POST['date'];
    
    $breed = $_POST['breed'];
    
    $sex = $_POST['sex'];
    
    $altered = $_POST['altered']; 
    
    $collarcolor = $_POST['collarcolor'];
    
    $tag = $_POST['tag'];
    
    $petname = $_POST['petname'];
    
    $description = $_POST['description'];
    
    
    $headers = 'From: lostandfound@petsfurpeople.org' . "\r\n" .
    
    
       'Reply-To: noreply@petsfurpeople.org' . "\r\n" .
    
       'X-Mailer: PHP/' . phpversion();
    
    $headers2 = 'From: ' . $_POST['fromemail'] . "\r\n" .
    
       'Reply-To: ' . $_POST['fromemail'] . "\r\n" .
    
       'X-Mailer: PHP/' . phpversion();
    
    $c_message = "Thank you for submitting your lost or found pet information. If you have any questions please use our <a href=\"http://www.petsfurpeople.org/contact/\">contact page</a> to send us a note or addtional information. ";
    
    $c_url = "http://www.petsfurpeople.org/thanks.php";
    
    //echo "".$toemail."<br>".$fromemail."<br>".$name."<br>".$message."<br>".$headers."<br>".$c_message."<br>".$c_url."";
    
    /* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. In the section below, always make the from email first and do not edit the preg_match line at all. To make the fields from the form to link up with this php file edit below. $name is for the name field et cetera. If you create a field on the form you would want to copy a section below and rename it to match the field here. */
    
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $fromemail)) {
    
      echo "<h4>Invalid or missing email address</h4>";
    
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
    } elseif ($name == "") {
    
      echo "<h4>Please fill out the Name field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
    } elseif ($phone == "") {
    
      echo "<h4>Please fill out the Phone field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
      } elseif ($type == "") {
    
      echo "<h4>Please fill out the Type field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
      } elseif ($area == "") {
    
      echo "<h4>Please fill out the Area field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
      } elseif ($date == "") {
    
      echo "<h4>Please fill out the Date field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
      } elseif ($sex == "") {
    
      echo "<h4>Please fill out the Sex field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
      } elseif ($petname == "") {
    
      echo "<h4>Please fill out the Pet's Name field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
      } elseif ($description == "") {
    
      echo "<h4>Please fill out the Description field</h4>";
    
      echo "<a href='javascript:history.back(1);'>Back</a>";
    
    }
    
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. Below where it says "Contact Form Email, this is the subject line for the confirmation email. Most importantly, always save backups and name pages as tests until they are working how you wish. DO NOT edit anything you are unsure of. */
    
    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>";
    
    }
    
    ?>

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,859
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Change
    PHP Code:
    $uploadFile $uploadDir "/" uniqid(True) . basename$_FILES["attach"]["name"] );
        
    $attachUrl $baseUrl "/" basename$_FILES["attach"]["name"] ); 
    to
    PHP Code:
    $uiduniqid(True);
    $uploadFile $uploadDir "/" $uid basename$_FILES["attach"]["name"] );
        
    $attachUrl $baseUrl "/" .$uidbasename$_FILES["attach"]["name"] ); 
    PS: the filenames may look a bit better if you add a hyphen or an underscore after the $uid variable, like
    PHP Code:
    $uploadFile $uploadDir "/" $uid .'_'basename$_FILES["attach"]["name"] ); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Daf (11-15-2013)

  • #8
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    I store the original file name in a table called userImages so they recognize it but I have the uploader create a filename for the image that is their userName plus a timestamp. This keeps all filenames unique, leaves the user the option to search for images under the original file name, and allows us to sort images based on user. A very simple table is all that is required.

    id(primaryKey)|date(timestamp for internal record keeping|userID(to keep track of who submitted the image)|originalFileName|newFileName

    Hope this helps.

  • Users who have thanked rgEffects for this post:

    Daf (11-15-2013)

  • #9
    Daf
    Daf is offline
    Regular Coder
    Join Date
    Feb 2005
    Location
    East Texas
    Posts
    140
    Thanks
    32
    Thanked 2 Times in 2 Posts
    abduraooft - thank you! That works perfectly!

    rgEffects - thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •