No, your logic is wrong. Look at the URL of your profile picture, either here, either on facebook or twitter ... do you see that they use a fixed predictable name? They do not, and there is a very good reason for this; you need unique, unpredictable file names.
Try the following:
$file = hash('sha256', openssl_random_pseudo_bytes(8)) . 'yourallowedextension';
Then request the name of the old image from your database, then upload a new image, if it succeeds, update the user profile image in the database and disconnect () the old file using the information obtained earlier, if any.
Make sure that you do not allow php files or any other unpleasant things to be downloaded, for which you can use the php fileinfo extension.
source share