Upload File, Renaming

Discussion in 'PHP' started by scottlpool2003, Jan 22, 2013.

  1. #1
    I'm using a basic file uploader and using time in the naming process. This isn't too much of an issue though because all uploaded items will be stored in a corresponding users folder so there's not much chance of a naming clash.

    However, I think I've gone wrong somewhere with the script and its storing it under a temp name as the outputted file name went from:
    To
    Ideally, I'd like the name to be time()_$title and output like:
    Not entirely sure how to set this in the code though.

    if(isset($_POST['addissue'])) {
     
    // use date and time
     
        $random_digit=time();
     
        $target='../publication/1/36/'.basename($_FILES['pdf']['tmp_name'].$random_digit.'.pdf');
        if(move_uploaded_file($_FILES['pdf']['tmp_name'],$target)){
     
     
       
     
    /* 
           echo "<input type='text' value='".basename( $_FILES['pdf']['tmp_name']). "$random_digit.pdf' name='pdf'>";
    */
     
    }else{
        //There's an error
    }
     
        }
    PHP:
    What is the best way to move it from a temp name into a custom name? I thought simply removing the ['tmp_name'] would do the trick but it didn't.

    Thanks
     
    Solved! View solution.
    scottlpool2003, Jan 22, 2013 IP
  2. #2
    To get a name as you want just change on line no. 7
    It will be something like

    $target='../publication/1/36/'.$random_digit.'_'.$_FILES['pdf']['name'];
    PHP:
     
    phpexperts, Jan 22, 2013 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Fantastic thank you.
     
    scottlpool2003, Jan 23, 2013 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    Just another query. This is all working, but now I need to limit the type to PDF only. While I've got this working, its uploading the PDF file before checking/displaying the error and then dropping the file. Is there a way to check before the file uploads? The reason I need to do this is because the files could be onwards and upwards of 50mb so it would obviously take time to upload.

    //Add issue
     
     
    if(isset($_POST['addissue'])) {
     
        if ($_FILES['pdf']['type'] != "application/pdf") {
     
    // Type is not PDF display error
     
        $dialog = '<font color="red"><strong>Error</strong> We can only accept PDF files at this time.
     
    } else {
     
     
     
     
        // use timestamp to create unique title
     
        $random_digit=time();
     
        // declare where to upload the file
     
        $target='../publication/1/36/'.$random_digit.'_'.$_FILES['pdf']['name'];
     
        // move the file
        if(move_uploaded_file($_FILES['pdf']['tmp_name'],$target)){
     
     
        // file has been uploaded, set message
            $dialog = '<font color="green"><strong>Success</strong></font> Your publication has been uploaded and is now awaiting moderation.';
     
    }else{
        //There's an error
            $dialog = '<font color="red"><strong>Success</strong></font> There was a problem uploading your publication.';
     
        }
     
      }
    }
    PHP:
     
    scottlpool2003, Jan 23, 2013 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    Rukbat, Jan 23, 2013 IP