We can find many articles related to uploading, viewing, and downloading files. This article is written on the basic concept of uploading and managing files. File uploading is the process of copying the file from your machine to the remote server. Other users of the same system then share this file by viewing or downloading it. What happens when you upload a file that already exists in the remote folder to which you are uploading?
File Management I've written two programs to manage this file version. The first uploads the file (file_upload_manager.php), the second displays the file (file_upload_manager.php). The source code is tested on a Windows system.
NOTE: Linux users, please change the folder path accordingly.
File Upload Manager: This program displays a menu to select the file in your system, a check box and an Upload button. Once the user clicks the upload button, the program checks the file for existence, and undergoes a series of tests as described in the plan.
Now let's look at the code snippets used in the program.
$dir_path Variable
:
This variable is the destination folder path.
$dir_path
= "C:apachehtdocsfilemanager";
This path is given for Windows-based systems. Please change your destination folder accordingly.
Get_New_File_Name
() Function:
This function is called from the main program when the program encounters files that exist and show a difference in size, date or time. This function will generate a new file name and return to the main function.
function Get_New_FIle_Name
($file_name) {
$sqlQuery="SELECT file_image_name FROM file_manager WHERE file_name LIKE '$file_name%' AND file_parent_id=1";
The sql query in the beginning of the function will fetch 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 the generated version number, and the value obtained is stored in $Last_Version. Otherwise, the new file name will be generated as file-name_VERSION1.
The next if statement checks for $Last_Version != 0, if true, $Last_Version is incremented by 1 and a new file name is assigned.
The return statement will return a new file name generated to the called statement.