All pastes #180294 Raw Edit

Miscellany

public text v1 · immutable
#180294 ·published 2006-09-23 00:20 UTC
rendered paste body
<?php
require_once("config.php");

// include a trailing '/'
$directory_to_upload = "images/";
// in bytes, this is 10kb
$max_file_size = (100*1024);
// max image width, in pixels
$max_width = 300;
//max image height, in pixels
$max_height = 100;

if(!isset($_POST['do_upload']))
{
	echo "
<form action='upload_image.php' method='post' enctype='multipart/form-data'>
	Image Name:
	<br />
	<input type='text' name='image_name' />
	<br />
	Image Path:
	<br />
	<input type='file' name='image_to_upload' />
	<br />
	<input  type='submit' name='do_upload' value='Upload Image' />
</form>
	";
}
"<title>Image Upload</title><form action='upload_image.php' method='post' enctype='multipart/form-data'>;"
{
	// 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($img_name) | | empty($image_dimensions))
	{
		$errors[] = 'You did not enter an Image Name!';
	}
	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 maximum Size is <b>'. $max_file_size . '</b> bytes.';
	}
	// check the image type, gif or jpg/jpeg/psd/png is allowed
	if(!exif_imagetype($_FILES['image_to_upload']['tmp_name']) == IMAGETYPE_GIF || !exif_imagetype($_FILES['image_to_upload']['tmp_name']) == IMAGETYPE_JPEG || !exif_imagetype($_FILES['image_to_upload']['tmp_name']) ==IMAGETYPE_PSD || !exif_imagetype($_FILES['image_to_upload']['tmp_name']) == IMAGETYPE_PNG)
	{
		$errors[] = 'Your Image must be a JPEG, PSD, PNG, or GIF!';
	}
	// check the Image 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.';
		}
	}
}