• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
File Version Management in PHP
We can find many articles related to uploading, viewing, and downloading files.Murali's latest article is written on the basic concept of uploading and managingfiles, and shows us how to create a magic with a little PHP and MySQL. Learn howto properly manage duplicate files through versioning.We can find many articles related to uploading, viewing, and downloading files. This article is writtenon the basic concept of uploading and managing files. File uploading is the process of copying the filefrom your machine to the remote server. Other users of the same system then share this file byviewing or downloading it. What happens when you upload a file that already exists in the remotefolder to which you are uploading? While discussing this problem, various other issues arises, suchas:
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 for more than once?This article gives you a solution for all the above problems. I also hope that this article will be usefulfor you to integrate into your projects.This article was 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 ProfessionalI welcome your comments to improve the efficiency of the code.
Prerequisites:
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 installed and configured.
File Version Management in PHP - ProgramPlan
I drafted a plan on what is needed for this file version management program. Below are some features:1.When a user uploads a file for the first time, the system should upload the file in the specifiedfolder and insert the file details like file name, size, and type in the database table.2.When the user uploads a file for the second time (the filename has to be the same), the systemwill compare the size, date and time of the existing file and of the file being uploaded, andautomatically assign a new version for the uploaded file if there is a difference. If there is nodifference, then the existing file will be retained and a message will be displayed for the user.3.A check box allows the user to replace the existing file. Clicking this option will replace theexiting file. Note that only the latest version of the file can be replaced in this case, and not anolder one.4.The database will maintain file list and version details. Users should be able to view all theversions of a file and download the required version.
 
5.While displaying the file list, the program displays all the versions of files 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 andso on. The program will display filename as xyz.doc for all the versions.6.The program allows users to delete the file. Clicking on delete will ask for confirmation before proceeding.
File Version Management in PHP - Let's BuildIt
Building the Database:
Database Name:
upload
The database contains one table, ‘
 file_manager 
’ that manages file information.The sql statement for creating the table:
CREATE TABLE file_manager (file_name varchar(50) default NULL,file_type varchar(20) NOT NULL default '',file_size varchar(20) NOT NULL default '',file_modified varchar(20) NOT NULL default '',file_parent_id mediumint(9) default NULL,file_image_name varchar(50) default NULL,KEY file_name (file_name),KEY file_image_name (file_image_name)) TYPE=MyISAM;
File Management:
I’ve written two programs to manage this file version. The first to upload the file(file_upload_manager.php), the second is to display the file (file_display_manager.php). The sourcecode 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 aseries of tests as described in the plan. Now let’s look upon 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 file exists and differencein 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 WHEREfile_name LIKE '$file_name%' AND file_parent_id=1";$fResult=mysql_query($sqlQuery);$Last_Version=0;$ver=0;if(mysql_num_rows($fResult)){while($fRow=mysql_fetch_array($fResult)){ list($junk,$ver)=explode("_VERSION",$fRow['file_image_name']);list($ver,$extn)=explode(".",$ver);$Last_Version = $Last_Version > $ver ? $Last_Version :$ver;}}else{$new_file_name =$file_name."_VERSION".++$Last_Version;return $new_file_name;}if($Last_Version !=0){$new_file_name=$file_name."_VERSION".++$Last_Version;return $new_file_name;}}
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 executedto 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 anew file name is assigned.The return statement will return a new file name generated to the called statement.
File_Size() Function:
This function returns the file size in terms of Bytes, Kb or Mb.
<?function File_Size($size){if($size > 104876){return $return_size=sprintf("%01.2f",$size / 104876)." Mb";
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...