Monthly Archives: October 2012

File Version Management in PHP

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. While discussing this problem, various other issues arise, 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. This article is developed with the following versions:

Apache - 1.3.23

MySQL - 3.23.48

PHP - 4.1.1

Operating System: Windows Professional

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, and 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, not the 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 abc.doc and its other versions are abc_VERSION1.doc,abc_VERSION2.doc,abc_VERSION3.doc, and so on.  The program will display filename as abc.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.

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.

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

<?php

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

File_Size() Function

This function returns the file size in terms of Bytes, Kb or Mb.

<?php

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

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

<?php

function display_form($errMsg){

global $dir_path;

?>
<html>

<head><title>File Manager</title></head>

<body bgcolor="#E5E5E5">

<div align="center">

<h4>File Manager</h4>

<?php

if($errMsg){

?>

<font face="verdana, helvetica" size="2" color="red"><?php echo $errMsg ?></font>

<?php

}

?>

<form action="<?php echo $_SERVER['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>

<?php

}

?>

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/or time stamp differs, 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 the file_display_manager.php page and the message “A previous version of this file exists” is displayed.

Top Sites to Check the Price of your Website

Know your price of your website..

While you are developing any product , may be it is application , website  or blog you always want to know the worth value of your website. And you always think of where will I find the evaluation tool of my website. Actually , if your are familiar with the google analytics or if you have any mechanism to find out the daily unique visitor of your website you can estimate the worth of your website. If keep in mind , only incoming visitor to your site donot give the actual data , there are other factors like how they react to your blog or site , will they return  bla bla bla…

I have made some collection of website to know the worth value of your website i.e. with the help of these sites you will approximately know the price of your website.

Some collection of website to check the price of your website:

  1. http://www.websiteoutlook.com/
  2. http://sitevaluecheck.com/
  3. http://www.sitevaluecalculator.com/
  4. http://www.yourwebsitevalue.com/
  5. http://stimator.com/
  6. http://mysiteevaluate.com
  7. http://websiteworthcheckr.com
  8. http://websitevaluecheck.com
  9. http://bizinformation.com
  10. http://cubestat.com

 

Open the Websites , check the  worth of your website and Enjoy

Top Website to Evaluate your SEO, SPEED of website

How I started my blog?

It was my fresh start in PHP and at that time I used to develop  websites that were too messy , frankly speaking I was not satisfied with what I was developing . The most common reason lack of experience , not matured enough , programmatically not strong and about the design , ha ha I used to download the fee templates.

Then I started learning things from different websites. Suddenly , I though t shouldnot I start giving things to other that I have learnt . Then I start sharing my learning the things that I learnt through my first blog www.phpfresher.com

Build Blog was not only the solution ????????????

I build the blog ,It was my fresh blog with fresh knowledge where some contents were genuine and some contents copied ditoo (really) and some compiled version. Ok where I stuck .

This was my some stastitics :

Daily unique visitor: around 30 , which was really pity

PR:  0 ( at the start now 2)

This showed that I have no readers , if I don’t have readers than what was I blogging but I did not stop writing blogs.

The only solution that I found after a long study is :

  1. Quality Contents
  2. Quality Seo
  3. Contents in need

Ok lets come to the point, today here I want tell all the bloggers , content writer  that writing and blogging  is not the solution to convey your writing to the readers .But you need to evaluate your site frequently. But keep in mind quality content is the most.

Collection of Websites to Evaluate your Website for site promotion and Update:

  1. http://www.woorank.com
  2. http://www.getmysite.info
  3. http://www.alexa.com
  4.  http://www.prchecker.info/check_page_rank.php
  5. http://www.prchecker.info/
  6. http://www.seocentro.com/tools/search-engines/keyword-position.html
  7. http://www.iwebtool.com/rank
  8. http://www.seomoz.org/blog/how-to-check-which-links-can-harm-your-sites-rankings
  9. https://positionly.com/
  10. https://www.google.com/analytics/

Among the above sites , google analytics and alexa.com  is the always the best apart from it , I prefer the woorank.com and getmysite.info I prefer the best because they give the detail view of your website from where you can update your website.

 Some Website that will help to Check the Speed of your website, i.e. how fast your website will open:

  1. http://www.iwebtool.com/speed_test
  2. http://tools.pingdom.com/fpt/
  3. http://rapid.searchmetrics.com/en/seo-tools/site-analysis/website-speed-test,46.html
  4. http://www.websiteoptimization.com/services/analyze/
  5. http://gtmetrix.com/
  6. http://loadimpact.com/
  7. http://site-perf.com/
  8. https://developers.google.com/speed/pagespeed/insights
  9. http://www.webtoolhub.com/tn561353-website-speed-test.aspx
  10. http://www.vertain.com/?sst

Open the website and Check speed of your website , and make the changes . Most referred by me  google and tools.pingdom.com/fpt/

SVN : Quick Overview on SVN

SVN

SVN is Subversion and it is a source control system. It is a PHP web based tool to administer an Apache Subversion repository server. It allows PHP scripts to communicate with SVN repositories and working copies without direct command line calls to the svn executable. If you are not currently using some other source control system; you are wise to invest in one. SVN is rather easy to use, compared to some other open source control systems, and has some neat tools on top of the source repository to make things easier for you.

With this tool you can remotely:

  • Create, remove, load and dump repositories
  • Manage user accounts for access to the repositories
  • Manage groups for access to the repositories
  • Invite users by email to create an account on the server

In order to work on SVN, you must meet the following requirements:

  • A PHP-enabled web server
  • A Linux server with Subversion installed
  • PHP function shell_exec() must be allowed

 

Not all servers or hosting providers allow their users to execute direct shell commands.  Please confirm with your hosting provider that this is enabled and allowed by their terms of service.

SVN Checkout

Here we are going to show how to checkout files from an SVN repository.  If you are using a public repository, you probably will not need to provide a username or password to access the files. However if you are using a private repository, you will be required to provide a username and password. This is the SVN command you will need to use to check out a file:

svn checkout SVNURL PATH

Now let’s go ahead and explain what is going on here.  First off the command we are using in svn checkout to pull the files from the SVN repository. SVNURL should be replaced with the link to the SVN repository (for the example we are going to use “ hello”).

PATH should be replaced with the name of the folder you want the files to be placed in to.  You can use the path depending on the location you are executing this command at, or you could use the full path to the folder. If you are executing the command in /home/user/public_html/ and list the path as “hello”, then it will place the files in /home/user/public_html/hello/. You could also just use the full path (for example, /home/user/public_html/hello) to specify a path anywhere on the server.

As we mentioned, we are going to use the hello repository and put all the repository files in a folder named “hello”.

svn checkout http://hello-php.googlecode.com/svn/trunk/distribution/libs/hello

Now that we have our command, we can go ahead and begin putting this command in to a PHP script.

<?php
$cmd="svn checkout http://hello-php.googlecode.com/svn/trunk/distribution/libs/ hello";
$svn = shell_exec($cmd);
echo $svn;
?>

We are now ready to use PHP script to check out the latest version of hello. Obviously, you should replace the hello URL with the URL of the SVN repository you plan on using.  Also you can change the path to whatever you want your folder named.

 Quick Tips for SVN Checkout

If you are using a private repository, you will be forced to authenticate yourself with a username and password.  To authenticate, use the following script below. All you have to do is replace USERNAME with your username, PASSWORD with your password from your SVN account that has at least read-only access.  Also be sure to replace SVNURL with the SVN address and PATH with where you want the files placed.

<?php

$cmd = "svn --username USERNAME --password PASSWORD checkout SVNURL PATH";

$svn = shell_exec($cmd);

echo $svn;

?>

 

If you would like the SVN repository files to be placed within the current folder that the command is being executed within, you use a period in the place of PATH.


<?php

$cmd = "svn checkout SVNURL .";

$svn = shell_exec($cmd);

echo $svn;

?>

SVN Update

Now that we have our files checked out, we may need to update the files if they are changed on the repository.  Here we are showing you how to update your files with the latest ones from the SVN repository you used to check out the files.  Here is the shell command we are going to be using:

svn update PATH

So that is the command we are using, it is much more simple than the svn checkout function because all the variables such as the repository URL and username/password (if applicable) are already stored on the server.

The function we are looking at is svn update to pull the latest files from the SVN repository.  PATH should be replaced with the path of the folder where you checked the files out to.  If you placed the files in a folder named “hello” you can just replace PATH with “hello”.  If you are executing the command from a different folder, you should use the full path to the working folder (for example /home/user/public_html/hello).

Now let’s go ahead and pretend we are going to update a folder named “hello”, this will be the command you are going to use.

svn update hello

Now that we know how to structure the command, it is time to show this command being executed through PHP function shell_exec().

<?php

$cmd = "svn update hello";

$svn = shell_exec($cmd);

echo $svn;

?>

 

Quick Tips for SVN Update

If you are using a private SVN repository, when you are updating a working folder, you do not need to re-authenticate again as your credentials are already stored on the server.

If you are updating a working folder and are executing the command from within that folder, you do not need to enter the PATH of the working folder. Below is a simple example of how you can execute the command from within the working folder.

<?php

$cmd = "svn update";

$svn = shell_exec($cmd);

echo $svn;

?>

Conclusion

You now know the basics of performing an SVN checkout or update from a remote repository.  Please remember that not all hosting servers allow the PHP function shell_exec() to be executed for server security reasons. It is best to check with your hosting provider before attempting any of these commands.