Jump to content

change the name of uploaded files


janim

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/64075-change-the-name-of-uploaded-files/
Share on other sites

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".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.