alim1990
alim1990

Reputation: 4972

Encoding uploaded files name

I want to ask, if I have a web form, and people use it to upload something to my db. If 2 persons uploaded the same file with the same name, in the uploads directory, one of them will replace the other. So I need to change the name of every file uploaded to db to : filename201405051200.pdf or jpg or... Here we have the filename per example image1 and the numbers are the date and time of the uploads. So any help. I am using the code shown as an answer in the link below:

Uploading blob files/images into Mysql

I used this code:

$path = "../uploads/".$_FILES['file']['name'];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $path.'_'.time())){
...
}

but now the format type of the file is replaced by the time. So if it is img.jpg it is now img85890338jpg and wont open properly

Upvotes: 0

Views: 35

Answers (1)

mario.van.zadel
mario.van.zadel

Reputation: 2949

You can use pathinfo to extract the file extension:

$fileExt = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);  

After that you can create your new file name:

if(move_uploaded_file($_FILES["file"]["tmp_name"], $path.'_'.time().date().'.'.$fileExt)) {

}

Upvotes: 1

Related Questions