File Upload Without Overwriting Existing Files of Same Name in PHP

Best WordPress Hosting

While creating a file upload function for a website, there is always a possibility that it will overwrite an existing file with same on the server. You can overcome this problem by checking if the file with the same name already exist or not. If File already exists with same name, you can then change the name of the file you are uploading. You can simply add number prefix for this.

In the below code snippet, we will see how to rename a file in case of another file with same name exists on the server.

HTML Code for File Upload

 uploadresults.php

Now this file upload and save file on server. To avoid overwriting of any existing file, you need to put a random name of this file which is almost impossible to be available as a file name in the server. Other way is to check if file is already available and then add a counter number in the file name. In this below code, we are checking the file name and then adding the counter in the file name.

You can also overcome this problem by adding timestamp in the name of each files before upload.

This was a very basic file upload example to avoid the overwriting of existing file.

advertisement

About The Author

  • ADD YOUR COMMENT

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Receive fresh web design resources and SEO tips in your email by our weekly newsletter

You have Successfully Subscribed!