<form action='image_upload.php' method='post' enctype='multipart/form-data'>
<?php
// include a trailing '/'
$directory_to_upload = "images/";
$max_file_size = 1024000000;
// max image width, in pixels
$max_width = 3000000;
// max image height, in pixels
$max_height = 1000000;
if(!isset($_POST['do_upload']))
{
echo "
<form action='upload_image.php' method='post' enctype='multipart/form-data'>
Image Path:
<br />
<input type='file' name='image_to_upload' />
<br />
<input type='submit' name='do_upload' value='Upload Image' />
</form>
";
}
elseif(isset($_POST['do_upload']))
{
// get the time for a random image name
$time = time();
// $img_name = mysql_real_escape_string($_POST['image_name']);
$image_dimensions = getimagesize($_FILES['image_to_upload']['tmp_name']);
$image_height = $image_dimensions[1];
$image_width = $image_dimensions[0];
$errors = array();
if(empty($_FILES['image_to_upload']['name']))
{
$errors[] = 'You did not give a file to upload.';
}
if(!is_uploaded_file($_FILES['image_to_upload']['tmp_name']))
{
$errors[] = 'Error while uploading file.';
}
if($_FILES['image_to_upload']['size'] > $max_file_size)
{
$errors[] = 'Your image size was too big. The max is <b>' . $max_file_size . '</b> bytes.';
}
// check the image type, gif or jpg/jpeg is allowed
if(!exif_imagetype($_FILES['image_to_upload']['tmp_name']) == IMAGETYPE_GIF || !exif_imagetype($_FILES['image_to_upload']['tmp_name']) == IMAGETYPE_JPEG)
{
$errors[] = 'Your image must be a JPEG or a GIF.';
}
// check the iamge dimensions
if($image_height > $max_width)
{
$errors[] = 'Your image height was too big.';
}
if($image_width > $max_width)
{
$errors[] = 'Your image width was too large.';
}
if(count($errors) > 0)
{
echo '<b>ERRORS:</b><br />';
foreach($errors as $err)
{
echo $err . '<br />';
}
}
else
{
$image_url = $directory_to_upload . $time . '_' . $_FILES['image_to_upload']['name'];
if(copy($_FILES['image_to_upload']['tmp_name'], $image_url))
{
// mysql_query("INSERT INTO uploaded_images (image_name, image_url) VALUES ('$img_name', '$image_url')");
echo 'Image uploaded successfully. <a href="' . $image_url . '">Click here to view your image</a>.';
}
else
{
echo 'The image upload process did not complete.';
}
}
}
?>