Home         Back

 

 

 

Saivaneri Web Tutor

CHAPTER 16

Hi Folks! LTNS? Isn’t it?. Anyhow, time is a great healer and teacher. I will discuss what it has taught me in the gap.

PHP file uploader

Okay u all have started a website and asked people to visit the site. Some body out there wants to send files/images to you at the site itself, not by email or other methods. So u need a FILE UPLOAD form at the site and the uploaded file should reach the /uploads folder of your website. How to do this?

PHP (recursive acronym PHP: Hyper Text Preprocessor) is a typical web language runs on both server and client side. It has grown powerful over years. Like any UNIX/*NIX based languages, this is also a bit difficult to master, but more useful for webmasters. It has got beautiful security, and puts MS ASP to shame by its simplicity and elegance.

 

THINGS TO DO ON THE SERVER

1.     INSTALL PHP AT C:\PHP (RECOMMDED)

2.     NOW GO TO >CONTROL PANEL> ADMINISTRATIVE SERVICES> IISERVICES >WEBSITES >DEFAULT WEBSITE(OR YOUR SITE NAME)>

3.     HERE, RIGHT CLICK ON YOUR SITE’S NAME AND GO TO PROPERTIES >DIRECTORIES THEN TICK MARK
A- SCRIPT SOURCE ACCESS
B- READ, WRITE ACCESS –CHECK THIS IF U WANT TO UPLOAD FILES HERE

4.     ON DIRECTORY SECURITY TAB-EDIT USERNAME PASSWD FOR ANONYMOUS ACCESS

5.     NOW EDIT THE PHP.INI FILE AT THE C:\WINDOWS\ DIRECTORY (IN XP) AS GIVEN BELOW
register_globals = On
file_uploads = On
upload_tmp_dir = C:\inetpub\wwwroot\uploads (OR YOUR WEBSITE DIR)
 upload_max_filesize = 2M
extension_dir = c:\PHP\extensions –THIS IS VERY IMPORTANT AS U WOULD BE INSTALLING PHP AT C:\PHP ONLY (RECOMMNDED)

THINGS TO DO ON THE .PHP SCRIPT FILE

1.              THESE FILES ARE FREELY AVALILABLE ON THE WEB. NO NEED TO PROGRAM ONE. I ALSO ADDED A SAMPLE UPDOWN.PHP FILE HERE. LITTLE EDITING REQD. IN THIS FILE TO SUIT UR NEEDS

Now the code updown.php

<?

$sizelimit = "yes"; //Do you want a size limit, yes or no?

$sizebytes = "200000"; //size limit in bytes-200KB

$dl = "d:\\inetpub\\wwwroot\\uploads\\"; //url where files are uploaded

$absolute_path = "d:\\inetpub\\wwwroot\\uploads\\"; //Absolute path to where files are uploaded

$websiteurl = "http://pasco/saivaneri/"; //Url to you website

$websitename = "Saivaneri.org"; 

switch($action) {

default:

echo"

<html>

<head>

<title>Upload Or Download</title>

</head>

<body>

<a href=$PHP_SELF?action=upload>Upload File</a>

 <a href=$PHP_SELF?action=download>Download File</a>

 <a href=$websiteurl>Return to $websitename</a>

<br><br>

Powered by <a href=http://www.zachwhite.com/>PHP Uploader Downloader</a>

</body>

</html>";

break;

case "download":

echo "

<html>

<head>

<title>File Download</title>

</head>

<body><a href=$PHP_SELF?action=upload>Upload File</a> <a href=$websiteurl>Return to $websitename</a>";

$list = "<table width=700 border=1 bordercolor=#000000 style=\"border-collapse: collapse\">";

$list .= "<tr><td width=700><center><b>Click To Download</b></center></td></tr>";

$dir = opendir($absolute_path);

while($file = readdir($dir)) {

if (($file != "..") and ($file != ".")) {

$list .= "<tr><td width=700><center><a href=$dl/$file>$file</a></center></td></tr>";

}

}

$list .= "</table>";

echo $list;

echo"

<br><br>

Powered by <a href=http://www.zachwhite.com/>PHP Uploader Downloader</a>

</body>

</html>";

break; 

case "upload":

echo"

<html>

 

<head>

<title>File Upload</title>

</head>

 

<body>

 

<form method=POST action=$PHP_SELF?action=doupload enctype=multipart/form-data>

<p>File to upload:<br>

<input type=file name=file size=30>

<p><button name=submit type=submit>

Upload

</button>

</form>

<br><br>

Powered by <a href=http://www.zachwhite.com/>PHP Uploader Downloader</a>

</body> 

</html>";

break; 

//File Upload

case "doupload":

$dir = "dir";

if ($file != "") { 

if (file_exists("$absolute_path/$file_name")) {

die("File already exists");

if (($sizelimit == "yes") && ($file_size > $sizebytes)) {

die("File is to big. It must be $sizebytes (200KB) or less.");

@copy($file, "$absolute_path/$file_name") or die("The file you are trying to upload couldn't be copied to the server"); 

} else {

die("Must select file to upload");

}

echo "

<html>

<head>

<title>File Uploaded</title>

</head>

<body>";

echo $file_name." was uploaded";

echo "<br>

<a href=$PHP_SELF?action=upload>Upload Another File</a>

<a href=$PHP_SELF?action=download> Download File</a>

<a href=$websiteurl> Return to $websitename</a><br><br>

Powered by <a href=http://www.zachwhite.com/>PHP Uploader Downloader</a>

</body>

</html>";

break;

 

}

?>

2.              C:\\inetpub\\wwwroot\\uploads\\ THIS IS THE ABSOLUTE PATH. NOTE THE TWO SLASHES. THIS IS FOR PHP NOT TO IGNORE THE SINGLE SLASH.

3.              C:\\inetpub\\wwwroot\\uploads\\ THIS IS ALSO SAME FOR TEMPORARY DIRECTORY BEFORE THE FILE BEING MOVED. KEEP IT SAME, DEAR!

4.              THIS SAMPLE PHP CAN UPLOAD OR DOWNLOAD ANY FILE UPTO THE LIMITS IMPOSED BY THE PHP.INI FILE.

5.              NOW EDIT THE FILE NOTEPAD AND PUT IT IN THE IIS ROOT FOLDER (WWWROOT) OR UR SITE’S ROOT FLDER

6.              USE IT AND CHECK IT ON IIS

7.              THE ABOVE EXAMPLE CAN BE USED ON PWS (PERSONAL WEB SERVER) ON WIN98,ME OR IIS 3 TO 5 ON NT-4,2000,XP.

THE ABOVE EXAMPLES ARE OPTMIZED FOR WIN XP & IIS ONLY. FOR OTHER O/S PLEASE ASK ME, I WILL HELP YOU

 

PREVIOUS CHAPTER                 NEXT CHAPTER

FOR FULL CHAPTER PLEASE CLICK HERE