home

articles

broadband

browsers

fraud001

javascript001

javascript002

javascript003

links

mysql01

mysql02

mysql03

mysql04

php001

php002

php003

problem001

spyware

virus01

virus02

web_programming

Sponsor001

 

Page created 13 April 2005, updated 13 June 2008.

Update 19 April 2005, there is an improved upload script available, which includes a Javascript error handler. The script is located on the Javascript page.

PHP Programming

Page 2

 UPLOADING A FILE

This article is about uploading files from your computer, onto the server. The two files should be copied into the same directory on the server, and the file is stored in a folder named 'images'.

HTML FORM

<HTML>

 <HEAD>

 <TITLE>File Upload</TITLE>

 </HEAD>

 <BODY BGCOLOR="WHITE" TEXT="#0101BA" VLINK="WHITE" ALINK="RED">

 <H1>File Upload</H1>

 <H3>Select a file to upload:</H3>

 <FORM ACTION="uploader.php" METHOD="post" ENCTYPE="multipart/form-data">

 <P>

 <INPUT TYPE="file" NAME="file" SIZE=45></P>

 <P>

 <INPUT TYPE=SUBMIT VALUE="Upload File"></FORM>

 </BODY>

</HTML>

Save the file as ' uploader.htm '.

PHP FILE

<?php

if( $_FILES['file'] ['name'] != "" ){

copy( $_FILES['file'] ['tmp_name'],

"images/" . $_FILES['file'] ['name'] )

or die( "Not copied" );

}

else{

die( "No file entered" );

}

?>

<html><head><title>Upload Complete</title></head><body>

<h3>File Uploaded ok</h3>

<ul>

<li>Sent: <?php echo $_FILES['file'] ['name']; ?>

<li>Size: <?php echo $_FILES['file'] ['size']; ?> bytes

<li>Type: <?php echo $_FILES['file'] ['type']; ?>

</ul>

<a href= "<?php echo "images/" . $_FILES['file'] ['name']; ?>">Click here to view the file</a>

</body></html>

Save the file as ' uploader.php '.

MORE INFORMATION

I have used these two files successfully on my local host, and also on one of my remote servers. The two sets of code are an exact copy of the ones that I used, so there is no reason why they won't work. As always, please use the message board regarding any problems.

SECURITY

So far, I have only used these files to test the capability of using PHP to upload files. Without additional security safeguards, it would be foolish to allow people to upload files onto the server.

IMAGE FOLDER

You will have to create an image folder, which is in the same directory as the other two files. For example, on my local host, the web pages are located in the ' htdocs ' folder. So if you were to put the files in that folder, you would have the following files located in that directory:

  • images (folder)

  • uploader.htm

  • uploader.php

PERMISSION

If the images folder is located on a Unix/Linux server, you will have to set the permission for reading and writing.

VIEWING THE IMAGES

The following code will show the pictures, which are located in the images folder:

<?php

$dirname= "images"; $count = 0;

$dir = opendir( $dirname );

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

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

$file = "images/" . $file;

$file_list .= " <TD><p align=center><a href = $file>

<img src= $file width=\"60\"></a></td> ";

$count++;

if( $count == 10 ){

$file_list .= " </TR><TR> ";

$count = 0;

}

}

}

closedir( $dir );

?>

<HTML>

 <HEAD>

  <TITLE>Listing Directory</TITLE>

 <BODY>

  <P>Files in <?php echo( $dirname ); ?></p>

 <TABLE WIDTH="100%" CELLPADDING="5" CELLSPACING="5" BORDER="1">

<tr>

<?php echo( $file_list ); ?>

</tr>

  </TABLE>

 </BODY>

</HTML>

This code should be copied into the same directory as the other two files. If you save this file as ' viewimages.php ' into (for example) the htdocs folder, the folder will contain:

  • images (folder)

  • uploader.htm

  • uploader.php

  • viewimages.php

RUNNING THE FILES

These scripts will only work on a server! So if for example, you are using your local host, you will need to open your web browser, and type the following into the URL field:

  • http://localhost/uploader.htm

  • http://localhost/viewimages.php

The first link loads the upload form, which submits the entry to the ' uploader.php ' script. The second link loads the images viewer, so that you can see the images that you uploaded.

THE IMAGES

If the images folder contains files, which are not image files, you will only see blank box with a failed image icon.