janim Posted August 9, 2007 Share Posted August 9, 2007 i want to upload files in folder called upload so the name of this file stores in DB with 'file' field ok ? the name of the file uploaded could be similar to another one uploaded before so the sys. gonna overwrite it automatically right ? i want check from DB in 'file' field if the file is exist or not if yes change this file name to (A+original name) and so on so if the of file was "mypicture.jpg" then with if statement it will be "Amypicture.jpg" so in the upload folder now exist two files mypicture.jpg & Amypicture.jpg if another one uploaded new file named "mypicture.jpg" i want to rename it's to AAmypicture.jpg so my if statement convert the first file just and don't continue why this is my code $checkfilename = "select id from $table where file = '".$_POST['file']."';"; $qryname=mysql_query($checkfilename) or die ("Could not match data because ".mysql_error()); $num_file= mysql_num_rows($qryname); $target_path = "upload/"; $target_path = $target_path . basename( $_FILES['file']['name']); $_FILES['file']['tmp_name']; $target_path = "upload/"; $oldfile = basename($_FILES['file']['name']); $pos = strpos($oldfile,".",0); $ext = trim(substr($oldfile,$pos+1,strlen($oldfile))," "); if ($num_file == $oldfile ){ $newfile = "A" . $oldfile ."" ; } else { $newfile=$oldfile; } $target_path = $target_path . basename($newfile); . . . . . . what is the problem here i posted this problem but have no answer i think i wasn't clear enough any comment now thanks for all Quote Link to comment https://forums.phpfreaks.com/topic/64075-change-the-name-of-uploaded-files/ Share on other sites More sharing options...
Daniel0 Posted August 9, 2007 Share Posted August 9, 2007 Just give them names like this: $name = md5(microtime()).$_FILES['file']['name']; And store it like that on the disk and store the original and new name in the database. Quote Link to comment https://forums.phpfreaks.com/topic/64075-change-the-name-of-uploaded-files/#findComment-319328 Share on other sites More sharing options...
janim Posted August 9, 2007 Author Share Posted August 9, 2007 thank you Daniel0 it's work 100% but why to store original name to DB i want to use the same name from DB and then download the file using $_GET why i need the original file name Quote Link to comment https://forums.phpfreaks.com/topic/64075-change-the-name-of-uploaded-files/#findComment-319340 Share on other sites More sharing options...
Daniel0 Posted August 9, 2007 Share Posted August 9, 2007 but why to store original name to DB Because when downloading the file again (I assume that that's the purpose), then you can name the original name like this: header("Content-Disposition: attachment; filename={$filename}"); So they'll e.g. download "file.txt" instead of "2203632f7d8f2c8168fc2f30217bde35file.txt". Quote Link to comment https://forums.phpfreaks.com/topic/64075-change-the-name-of-uploaded-files/#findComment-319347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.