Home arrow PHP arrow Page 3 - File Version Management in PHP

File Version Management in PHP

PHPWe can find many articles related to uploading, viewing, and downloading files. Murali's latest article is written on the basic concept of uploading and managing files, and shows us how to create a magic with a little PHP and MySQL. Learn how to properly manage duplicate files through versioning.

Author Info:
By: Murali Dharan
Rating: 4 stars4 stars4 stars4 stars4 stars / 51
December 08, 2003
  1. · File Version Management in PHP
  2. · Program Plan
  3. · Let's Build It
  4. · Let's Build It, Cont'd.
  5. · Conclusion

print this article

File Version Management in PHP - Let's Build It
(Page 3 of 5 )

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)

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 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 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 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";


         $Last_Version = $Last_Version > $ver ? $Last_Version : $ver;
      $new_file_name =$file_name."_VERSION".++$Last_Version;
      return $new_file_name;

  if($Last_Version !=0){
      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 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.

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";
    }elseif($size > 1024){
        return $return_size=sprintf("%01.2f",$size / 1024)." Kb";
        return $return_size=$size." Bytes";


Display_form() Function:

This function is used to prompt the user to select the file from your local machine.

function display_form($errMsg){
    global $dir_path;

<head><title>File Manager</title></head>
<body bgcolor="#E5E5E5">
<div align="center">
<h4>File Manager</h4>

<? if($errMsg){
?><font face="verdana, helvetica" size="2" color="red"><? echo $errMsg ?></font>
<form action="<? echo $PHP_SELF; ?>"  enctype="multipart/form-data" method="POST">
<table border="1">
<tr><th>File Location:</th><th><input type="file" name="up_file" /></th></tr>
<tr><th colspan="2"><br><input type="checkbox" name="replace" value="1">Replace Exiting File*</th></tr>
<tr><th colspan="2"><br><input type="submit" name="upload" value="Upload File !" /></th></tr>

<br>* - Clicking this option will replace the existing file
<br><a href="file_display_manager.php">File Manager</a>

<br><? if ($status){ echo "Result  :".$status;  }?>

<p><br><b>Folder Location:</b><? echo $dir_path ?>
<br>You can change this location by editing the file</font>

<? } ?>

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials