We can find many articles related to file upload, view, download, etc. This article is written on this basic concept of uploading and managing files. File upload is a process of copying the file from your machine to the remote server. Other users of the system then share this file by viewing / downloading. What happens when I upload a file that already exists in the remote folder to which you are uploading? While discussing this problem, various other issues arises, such as:
Should the program allow the user to restore this file or retain the existing file?
If the file exists, then should I copy the file as a new version?
How do I manage files if the user uploads files of same name and type more than once?
This article gives you a solution for all the above problems and also I hope this article will be useful for you to integrate in your projects.
This article is developed with the following versions of Apache, PHP and MySQL:
Apache - 1.3.23
MySQL - 3.23.48
PHP - 4.1.1
Operating System: Windows Professional
To understand this article, you should have a fair knowledge of PHP. To run examples given in your machine, you need Apache, PHP, and MySQL software installed and configured.
I drafted a plan on what is needed for this file version management program. Below are some features planned:
When a user uploads a file for the first time, the system should upload the file in the specified folder and insert the file details like file name, size, type in the database table.
When the user uploads a file for the second time (the filename has to be the same), the system will compare the size, date and time of existing file and file being uploaded, and automatically assign a new version for the uploaded file if there is a difference. If there is no difference, then the existing file will be retained and a message will be displayed for the user.
A check box allows the user to replace the existing file. Clicking this option will replace the existing file. Note that only the latest version of the file can be replaced in this case, and not an older one.
The database will maintain file list and version details. User should be able to view all the versions of a file and download the required version.
While displaying the file list, the program displays all the versions of file under one name. For example, if the file name is xyz.doc and its other versions are xyz_VERSION1.doc, xyz_VERSION2.doc, xyz_VERSION3.doc, xyz_VERSION4.doc, xyz_VERSION5.doc and so on. The program will display filename as xyz.doc for all the versions.
The program allows user to delete the file. Clicking on delete will ask for confirmation before proceeding.
The file manager program is designed to upload and manage files. The files are stored in a predefined folder. There are two files: file_upload_manager.php and file_display_manager.php, which performs the file operations. The former handles the file uploading and the later displays the file in the folder and also lets the user to delete the file. The source code is tested on Windows systems. Linux users, please change the folder path accordingly.
The sql query in the beginning of the function will fetch the file names of previous versions.
If the sql query returns record sets, it means the file has previous versions. The while loop is executed to store version number generated, and the value obtained is stored in $Last_Version. Otherwise the new file name will be generated as file-name_VERSION1.
Next, if statement checks for $Last_Version != 0, if true, $Last_Version is incremented by 1 and new file name is assigned.
The return statement will return a new file name generated to the called statement.
This function returns the file size in terms of Bytes, Kb or Mb.
<p><br><font face="verdana, helvetica" size="2" color="#808080"><b>Folder Location:</b><? echo $dir_path ?></font>
<br><font face="verdana, helvetica" size="1" color="#808080">You can change this location by editing the file</font>
The Main Program
When you execute file_upload_manager.php, the following code is executed first and other function described above are called in the middle of the program.
Once the user selects the file and clicks upload, the main program part is executed. Here the program gets the upload file information like file size, file type, date modified and time stamp.
Next, it checks for the file existence in the destination folder. If the file is present the if part of the program gets executed otherwise the file is copied to the destination folder and the file information is inserted in the database.
If the file exists, and if the file size and time stamp differ, Get_New_File_Name() function is called to generate a new file name. Then the file is copied to the destination folder and the file information is stored in the database.
If the file exists and the user had checked the "Replace Existing File" option, the file is replaced and the file information is updated in the database accordingly. Otherwise the user is redirected to file_display_manager.php page and a message ï¿½A previous version of this file existsï¿½ is displayed.