PHP Code:
<html>
<head>
</head>
<body>
<?php
// Check if page called itself or not
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// Will be a session var
$currentUser = "Nickycolgan";
// upload directory
$filedir = "./users/".$currentUser."/images/";
// temp file location
$tempfile = $_FILES['imagefile']['tmp_name'];
// Check the file is the one the user submitted
if (is_uploaded_file($tempfile)) {
// Store the image details in easy to remember variables
$filename = $_FILES['imagefile']['name'];
$filetype = image_type_to_mime_type(exif_imagetype($tempfile));
$imagesize = number_format($_FILES['imagefile']['size']/(1024*1024), 2, ".", "");
$imageattr = getimagesize($tempfile);
$imagewidth = $imageattr[0];
$imageheight = $imageattr[1];
// Check the file type is one that the website supports ie image files
if ($filetype == "image/gif" || $filetype == "image/jpeg" ||
$filetype == "image/bmp" || $filetype == "image/tiff" || $filetype == "image/png") {
// Move the file to the users directory
if(move_uploaded_file ($tempfile, $filedir.$filename)) {
// Print our some information about the file that was uploaded
echo "File uploaded successfully!<p />";
echo "File Name: ".$filename."<br />";
echo "File Type: ".$filetype."<br />";
echo "File Size: ".$imagesize."<br />";
echo "Image Width: ".$imagewidth."<br />";
echo "Image Height: ".$imageheight."<br />";
// Get the user to enter any additional details about the image
?>
<form action="uploaded.php" method="POST">
Image name: <input type="text" name="imagename" maxlength="40" /><br />
Description: <input type="text" name="imagedescription" /><br />
Price: £<input type="text" name="imagepricepounds" maxlength="4" />.<input type="text" name="imagepricepence" maxlength="2" /><br /><br />
<input type="hidden" name="filedir" value="<? echo $filedir ?>" />
<input type="hidden" name="filename" value="<? echo $filename ?>" />
<input type="hidden" name="filetype" value="<? echo $filetype ?>" />
<input type="hidden" name="imagesize" value="<? echo $imagesize ?>" />
<input type="hidden" name="imagewidth" value="<? echo $imagewidth ?>" />
<input type="hidden" name="imageheight" value="<? echo $imageheight ?>" />
<input type="submit" value="Add Image" />
</form>
<?php
} else {
// The file could not be moved
echo "There was an error uploading the file. Please try again later.<br />";
}
} else {
// The file type didn't meet any of the types listed above
echo "The file you uploaded was not a image file that we support.<br />";
}
} else {
switch($_FILES['tempfile']['error']){
case 0:
echo "The file your are trying to upload may not have been the same file that was submitted. Please try again later.";
break;
case 1:
case 2:
echo "The file you are trying to upload is too big.";
break;
case 3:
echo "The file you are trying upload was only partially uploaded. Please try again later.";
break;
case 4:
echo "You must select an image to be uploaded.";
break;
default:
echo "There was a problem with the file you tried to upload. Please try again later.";
break;
}
}
} else {
// The page did not call itself so display a form to upload an image
?>
<form enctype="multipart/form-data" action="<?$_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
Upload Image: <input type="file" name="imagefile" />
<input type="submit" value="Upload Image" />
</form>
<?php
}
?>
</body>
</html>