CSC220 UploadImages.php
--D. Thiebaut 00:29, 1 November 2010 (UTC)
<?php
// uploadimage.php
// D. Thiebaut
//
// a Php program to upload images.
// The images are stored in a directory ./images that is
// read/write accessible by all.
//
// program states:
//
// (S0) display form
// |
// |
// V
// (S1) display result of upload
// |
// |
// V
// (S3) display all images
//
session_start(); // to keep track of the phases
//-----------------------------------------------------------------------------
// G L O B A L S
//-----------------------------------------------------------------------------
$DIR = "./images/"; // the directory where the pics are stored
//-----------------------------------------------------------------------------
// openPage: opens the html tags and display the menu
//-----------------------------------------------------------------------------
function openPage() {
?>
<html>
<head>
<title>Upload Image</title>
<meta http-equiv="Pragma" content="no-cache">
</head>
<body>
<?php
printf( "(<a href=\"%s\">Source</a>)", str_ireplace( ".php", ".txt", $_SERVER[ 'PHP_SELF' ] ));
print "<P><center>\n";
print "<a href=\"$thisPage?phase=one\">Upload Pics</a> |\n";
print "<a href=\"$thisPage?phase=three\">Show All Pics</a>\n";
print "</center>\n";
print "<P>";
}
//-----------------------------------------------------------------------------
// closePage: display the menu and closes the html tags
//-----------------------------------------------------------------------------
function closePage() {
$thisPage = $_SERVER[ 'PHP_SELF'];
print "<center>\n";
print "<a href=\"$thisPage?phase=one\">Upload Pics</a> |\n";
print "<a href=\"$thisPage?phase=three\">Show All Pics</a>\n";
print "</center>\n";
//displaySession();
?>
</body>
</html>
<?php
}
//-----------------------------------------------------------------------------
// displayArrays: debugging function
//-----------------------------------------------------------------------------
function displayArrays() {
print "<P><P><table bgcolor=\"#6699cc\"><tr><td>";
print "<P>Session = <PRE>";
print_r( $_SESSION );
print "</td></tr>";
print "<tr><td><P>REQUEST = <PRE>";
print_r( $_FILES );
print "</td></tr>";
print "<tr><td><P>REQUEST = <PRE>";
print_r( $_REQUEST );
print "</td></tr>";
print "</table></PRE><P>\n";
}
//-----------------------------------------------------------------------------
// Phase1: displays the upload form
//-----------------------------------------------------------------------------
function phase1() {
?>
<h1>Upload Images</h1>
<P><P><P><P>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?phase=two"
method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="125000">
Upload Image: <input type="file" name="imgfile">
<input type="submit" value="Upload Image">
</form>
<?php
$SESSION[ 'phase' ] = "two"; // to report status of upload
}
//-----------------------------------------------------------------------------
// Phase2: reports on status of upload and display picture
//-----------------------------------------------------------------------------
function phase2() {
global $DIR;
print "<h1>Upload Status</h1>\n";
print "<P><P><P><P>";
if ( empty( $_FILES[ 'imgfile' ] ) ) {
print "<font color=\"#ff0000\"><h2>Error uploading image</h2></font>";
return;
}
//--- get the image information from the global $_FILES array ---
$fileName = basename( $_FILES[ 'imgfile' ][ 'name' ] );
$fileType = $_FILES[ 'imgfile' ][ 'type' ];
$tempName = $_FILES[ 'imgfile' ][ 'tmp_name' ];
$error = $_FILES[ 'imgfile' ][ 'error' ];
$fileSize = $_FILES[ 'imgfile' ][ 'size' ];
//--- create the two image file names for the original and thumbnail ---
$destFileName = $DIR . $fileName;
//--- move temp file to our directory ---
$result = move_uploaded_file( $tempName, $destFileName);
//--- error? ---
if( empty($result) ) {
print( "<font color=\"#ff0000\"><h2>Error moving the uploaded file.</h2></font>" );
return;
}
//--- display file ---
print( "<P><img src=\"$destFileName\">" );
}
//-----------------------------------------------------------------------------
// phase3: display all the pics in the directory (just the images)
//-----------------------------------------------------------------------------
function phase3() {
global $DIR;
//--- open the directory ---
$dir_handle = opendir( $DIR ) or die("Unable to open $DIR");
//--- get all the files that are thumbnails ---
$files = array();
while ( ($file = readdir($dir_handle)) !== false ) {
if ( $file != "." && $file != ".." )
$files[] = $DIR . $file;
}
//--- close the directory
closedir($dir_handle);
//--- display the pics ---
print "<h1>All Uploaded Pictures</h1>\n";
print "<P><P><P><P><table>\n";
foreach ( $files as $file )
print "<tr><td><img src=\"$file\"></td><td>$file</td></tr>\n";
print "</table>\n";
}
//--------------------------------------------------------
// M A I N P R O G R A M
//--------------------------------------------------------
openPage();
if ( empty( $_SESSION[ 'phase' ] ) ) {
//print "<P>SESSION[phase] empty";
$phase = "one";
}
else {
//print "<P>Getting phase from SESSION";
$phase = $_SESSION[ "phase" ];
}
if ( !empty( $_REQUEST[ 'phase' ] ) ) {
//print "<P>Cetting phase from REQUEST";
$phase = $_REQUEST[ "phase" ];
}
switch ( $phase ) {
case "one":
//---------------------------------------------------
// P H A S E #1: upload image
//---------------------------------------------------
phase1();
$_SESSION[ 'phase' ] = "two";
break;
case "two":
//---------------------------------------------------
// P H A S E #2: report status of upload
//---------------------------------------------------
phase2();
$_SESSION[ 'phase' ] = "one";
break;
case "three":
//---------------------------------------------------
// P H A S E #3: display all the uploaded pics
//---------------------------------------------------
phase3();
$_SESSION[ 'phase' ] = "one";
break;
default:
print "<h3><font color=\"#ff000\">Error in phase initialization</font></h3>\n";
}
closePage();
?>