PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

(Upload download files) Build PHP Database Driven Websites – Tutorial 4b

September15

Download

Upload files in form of games from CMS and download from the website.

Previously we had build the database structure, CMS and website interface for an online egames website and now we will add extra features to our website.
Recap:

  • We are adding files or games to our egames website
  • So far we have build a database structure, CMS and website interface
  • Download file 79.rar. “start” folder consist of the work done previously and “final” folder consist of the complete work done in this tutorial

Download “start” folder copy paste at c:/wwwroot/inetpub and rename it to “egames”. Create folder “file” in the “egames” folder.

Now we want to add games to our egames website. We will write scripts to upload files on the CMS and then simply download the file from the egames website. The coding will be similar to our Photo Gallery discussed before.

We have to make changes at 3 locations in order to upload files on our dynamic website:

  1. The database structure
  2. The CMS admin panel of the website
  3. The website itself

The database structure

Open PHPmyadmin and type the following code to change the table structure of the table “product”

ALTER TABLE 'product' ADD 'file' VARCHAR( 255 ) NULL AFTER 'photo' ;

The CMS admin panel of the website

I assume that you are following the flow of my tutorials and have the “egames” website installed and running on your computer. If you want click here to view the first CMS tutorial.

Extract the file and rename the folder “start” to “egames” and copy it to c:/wwwroot/inetpub.

Add File

Open file admin/addproduct.php and type the following below the “Photo” field.

<tr>
<td width="200" height="21" valign="middle" align="left"><font class=tablackb>File</font></td>
<td width="8"> : </td>
<td width="350" align="left"><font><input name="file" type="file" size="36"></font></td>
</tr>

Save File

Open admin/saveproduct.php; find <!– listing –> delete the existing text and type the following:

Modify File

Open file admin/modifyproduct.php and type the following below the “Photo” field.

Make sure the form declaration has <form method=post name=myform action=”updateproduct.php” enctype=”multipart/form-data”>

Update File

Open admin/updateproduct.php; find <!– listing –> delete the existing text and type the following:

Delete File

Open admin/deleteproduct.php find <!– listing –> delete the existing text and type the following:

Download File or Games on the Website

Open product.php find <!– listing –> delete the existing text and type the following:

Code Explanation
saveproduct.php
if(!empty($_FILES["file"])) Extracting the file from the previous page and checking if it exists
$uploaddir = "../file/"; assigning the folder for uploading images
if (copy($_FILES["file"]["tmp_name"], $uploaddir . $_FILES["file"]["name"])) copying image from existing folder to the assigned folder
if copying is true then follow further steps else $file=empty to be stored in DB
$a=$_FILES["file"]["name"]; $a = name of the file
$end=strstr($a,'.'); extracting the extension of the file
$file=$uploaddir.$productid.$end; $file = folder name productid and extension
rename($uploaddir.$a, $file); rename the file name with desired name which in this case is the productid
$file=$productid.$end; $file = productid + extension to be stored in the mysql table “product”
mysql_query("insert into product values('$productid','$product','$photo', '$file','$bdesc','$ddesc')") Inserting the file in table “product”
modifyproduct.php
<? if($row1["file"]!="") { ?><img src="../file/<? echo $row1["file"] ?>" width="100">
<? } else { ?> Picture not available<? } ?>
displaying the file if exit else display image
<?php
$my_file = "../file/".$row1["file"];
$file_size = filesize($my_file);

if ($file_size >= 1073741824) {
$show_filesize = number_format(($file_size / 1073741824),2) . ” GB”;
} elseif ($file_size >= 1048576) {
$show_filesize = number_format(($file_size / 1048576),2) . ” MB”;
} elseif ($file_size >= 1024) {
$show_filesize = number_format(($file_size / 1024),2) . ” KB”;
} elseif ($file_size >= 0) {
$show_filesize = $file_size . ” bytes”;
} else {
$show_filesize = “0 bytes”;
}

?>

PHP function filesize() to find the exact size of the file

Make the file size readable by using PHP number_format function

<input type="hidden" name="oldfile" value="<? echo $row1["file"] ?>"> passing the file name if not changed
<input type=file name="newfile" size=50 disabled=1><br>
<input type=radio name="pic" value="usecurrent" checked onclick=radval1();> Use Current
<input type=radio name="pic" value="newpic" onclick=radval2();>New Image</b>
using radio buttons to give choice of either to use the current image or new image
function radval3(){document.myform.newfile.disabled=1;}
function radval4(){document.myform.newfile.disabled=0;}
Javascript function for the above step
updateproduct.php
$pic=$_REQUEST["pic"]; request radio button “pic” to check if the user have the changed the image
if($pic=="usecurrent"){ $newfile=$_REQUEST["oldfile"];} $pic = current then $newfile=the oldfile ie no change
$oldfile=$_REQUEST["oldfile"];if($oldfile!="") unlink("../file/".$oldfile); else get the oldfile and delete it from website folder
if(!empty($_FILES["newfile"])) ... copying the file to the website folder like done in saveproduct.php
mysql_query("update product set product='$product',file='$newfile', bdesc='$bdesc',ddesc='$ddesc' where productid='$productid'") update the file in the table “product”
deleteproduct.php
$respic=mysql_query("select * from product where productid='$productid'");
$rowpic=mysql_fetch_array($respic);
Extracting row from table “product” where productid matches
if($rowpic["file"]!="") Checks if the file exist
$file=$rowpic["file"]; unlink("../file/".$file); Delete file

Dan dana dan DONE! Coding is very similar to our previous tutorial so you see how the same coding can do different things!!!! :)

posted under Development Guide

Email will not be published

Website example

Your Comment:

 

12,831 spam comments
blocked by
Akismet