About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > PHP File Manager...

   Browse by title articles:
   What is hot:

Quick E-Commerce with PHP and ...

PHP for Dummies

Reading a Delimited File using...

PHP File Manager

Protecting your PHP and HTML S...

Dealing with cannot modify hea...

Faster Loading Graphics
Arrays Intro
Steps To Optimizing Your HTML Codes
Metacharacters
Prev articles123 4 56789101112Next articles



PHP File Manager


Computer & I'net articlesPHP File Manager

by Murali Dhar    



This article comes with a zip folder containing source code and supplimentary information.

Download supplimentary files (7.03KB)

Background

We can find many articles related to file upload, view, download etc… This article is written on this basic concept on 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 is 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 for more than once?

This article gives you a solution for all the above problems and also I hope this article will be useful for u 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

I 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 software installed and configured.

Program Plan

I drafted a plan on what is needed for this file version management program. Below are some features planned:

  1. 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.

  2. 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.

  3. A check box allows the user to replace the existing file. Clicking this option will replace the exiting file. Note that only the latest version of the file can be replaced in this case, and not an older one.

  4. 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.

  5. 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.

  6. 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.

Database Design:

Database name: upload


CREATE TABLE file_manager (
  file_id mediumint(9) NOT NULL auto_increment,
  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,
  PRIMARY KEY  (file_id),
  KEY file_name (file_name),
  KEY file_image_name (file_image_name)
) TYPE=MyISAM;
File Upload Manager:

This program displays a menu to select the file in your system, a check box and Upload button.

Once when the user clicks the upload button, the program checks the file for existence, and undergoes series of tests as described in the plan.

Now lets look upon the code snippets used in the program.

$dir_path Variable

This variable is the destination folder path.

$dir_path= "C:\\apache\\htdocs\\filemanager\\";

This path is given for a Windows based system. 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";

  $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 executed to store version number generated, and the value obtained is stored in $Last_Version. Else will 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.

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

<br><font face="verdana, helvetica" size="1" color="#808080">* - Clicking this option will replace the existing file</font>
<br><font face="verdana, helvetica" size="1" color="#808080"><a href="file_display_manager.php">File Manager</a></font>

<br><font face="verdana, helvetica" size="2" color="#4A4A4A"><? if ($status){ echo "Result :".$status; }?></font>

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

</form>
</div>
</body>
</html>
<? } ?>
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, 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 else 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, time stamp differs, Get_New_File_Name() function is called to generate 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. Else the user is redirected to file_display_manager.php page and a message “A previous version of this file exists” is displayed.

File Display Manager

This php program handles displaying of files present in the folder. As described in the plan the program will display the file name, size, type and an option to delete file.

dir_display() Function:

This function displays the files available in the folder in a table. The template looks like this

File Manager

File Size Type Modified  
abc.doc 25 Kb doc Oct 01, 2003

Delete

xyz.txt 24 Bytes txt Oct 03, 2003

Delete

abc.doc 48 Kb doc Oct 05, 2003

Delete

abc.doc 67 Kb doc Nov 05, 2003

Delete

xyz.txt 58 Bytes txt Oct 23, 2003

Delete

You can see the file names are repeated. But you can see the file size and modified differs. To view / download the file, you can click the hyperlink of the file name. This will show you the file name of different version. You can customize the program to include another field “Version” to track the file version.

To delete a file, click on ‘Delete’ hyperlink, this will prompts you for confirmation.

Conclusion

Hope this technique would be useful to integrate in your projects. I welcome your comments and suggestions.

Happy Programming :-)

This article comes with a zip folder containing the two PHP files and the text to create the database.

Download supplimentary files (7.03KB)




-----------------

Murali has 2 years experience in IT and 1 year with PHP. His interests include designing, programming and learning new technologies. He is presently working for a private organization in Chennai, India.







Life insurance




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com