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
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:
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:
See http://www.codestore.net/store.nsf/unid/DOMM-4Q8H9E for an example of how to do this in Javascript. That will stop the upload before it occurs if the filetype is wrong.