blackMan
blackMan

Reputation: 15

How to replace an existing image file using PHP

When I upload an image file named boy.png, it doesn't replace the existing boy.png using move_uploaded_file().

The 2nd boy.png is a different image.

How can I upload and replace the file if it's already exist using PHP?

Upvotes: 1

Views: 1101

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

You can try this way to check first if the file with same name already exists or not? if that exits then remove the old one and upload the new one. Let's try like this way-

//checking if file with same name already exists
if(file_exists("pathtofile/boy.png")) unlink("pathtofile/boy.png");

//upload file now using the move_uploaded_file()
move_uploaded_file($fileLocTemporary, "pathtofile/boy.png"); # change as per your temp location

Upvotes: 1

Related Questions