upload file from form

Discussion in 'PHP' started by LongBeachIsland, Apr 8, 2011.

  1. #1
    Ok I have 2 question I am not real familiar with Php, but I am getting the general hang of it very slowly. I am basically trying to upload a file via a php form. The code gets the file to the server in the specified folder. However it keeps the same name as the uploaded file. Is there any easy way to amend the file name while it is being copied out of the temporary folder. I would imagine affixing a time stamp into the name should clear up any chances that the file would be overwritten. Secondly I would like in the form to be able to send in the email the attached file location. So it can be easily viewed, basically how would I declare the filename and path into a variable. So that I can call it in in the second part of the script.
    anyway here is the code I am using
    
    <?php 
     $target = "upload/"; 
     $target = $target . basename( $_FILES['uploaded']['name']) ; 
     $ok=1; 
     if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
     
     if (!($uploaded_type=="image/gif")) {
     echo "You may only upload GIF files.<br>";
     $ok=0;
     } 
     {
     echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
     } 
     else {
     echo "Sorry, there was a problem uploading your file.";
     }
     ?>
    
    
    Code (markup):
    
    $file= $_REQUEST['path_of_uploaded_file'] ;
    $email = $_REQUEST['email'] ;
    $firstname = $_REQUEST['firstname'] ;
    $lastname = $_REQUEST['lastname'] ;
    $companyname = $_REQUEST['companyname'] ;
    $phone = $_REQUEST['firstname'] ;
    $website = $_REQUEST['website'] ;
    $subject = "Message from: $name";
    $message = $_REQUEST['message'] ;
    $headers = "noreply@emailaddress.com";
    $body = "\n\n From: $firstname      $lastname \n\n Company Name: $companyname     Email: $email \n\n Website: $website \n\n Phone: $phone     Fax: $fax \n\n Message: $message \n\n  $file";
    $sent = mail($to, $subject, $body, $headers) ;
    Code (markup):

     
    LongBeachIsland, Apr 8, 2011 IP
  2. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think I finally got everything running the way I want. There is one small detail I could use some help with however.
    The part where it renames the file if it exists.
    if (file_exists($target)) {
    $target .= "_".time();
    //echo "File already exists, will be uploaded as ".$target; // Comment this out (put // before it) if you don't want the user to know what is being said.
    }
    Code (markup):
    Is there an easy way to use the time in front of the .png extension. For example if I upload image.gif and it already exists it will rename it to something like image.gif_012091209 I tried a couple different scenarios which keep producing errors.
     
    LongBeachIsland, Apr 8, 2011 IP
  3. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want it real simple try this:
    
    <?php 
     $target = "upload/".time()."_"; 
     $target = $target . basename( $_FILES['uploaded']['name']) ; 
     $ok=1; 
    .
    .
    .
     ?>
    
    PHP:
    this will add time to beginning of all the files. so there won't be any duplicate filenames, you'll have the files sorted by the date of upload, also if you need to access the actual filename all you need to do is skip the first 11 chars and it works on all filetypes!
     
    Sepehr, Apr 9, 2011 IP
  4. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I know the .time was the key to making it work I just don't have that good of an understanding to know where to put it.
     
    LongBeachIsland, Apr 9, 2011 IP