Jump to content

Uploading Images Help


andrew_biggart

Recommended Posts

Ok I have finally got rid of my syntax errors with this upload script. Now im getting the error uploading your image.

 

My folders are called project_files and project_thumbs.

 

This is my script

 

<?php
	$results = "";

	error_reporting(E_ALL);
    ini_set("display_errors", 1);

	//Auto Thumbnail Function
	function create_thumbnail($source,$destination, $thumb_width) {
	$size = getimagesize($source);
	$width = $size[0];
	$height = $size[1];
	$x = 0;
	$y = 0;
	if($width> $height) {
		$x = ceil(($width - $height) / 2 );
		$width = $height;
	} elseif($height> $width) {
		$y = ceil(($height - $width) / 2);
		$height = $width;
	}
	$new_image = imagecreatetruecolor($thumb_width,$thumb_width)
		or die('Cannot initialize new GD image stream');
	$extension = get_image_extension($source);
	if($extension=='jpg' || $extension=='jpeg')
		$image = imagecreatefromjpeg($source);
	if($extension=='gif')
		$image = imagecreatefromgif($source);
	if($extension=='png')
		$image = imagecreatefrompng($source);

	imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,							       
    $thumb_width,$width,$height);
	if($extension=='jpg' || $extension=='jpeg')
		imagejpeg($new_image,$destination);
	if($extension=='gif')
		imagegif($new_image,$destination);
	if($extension=='png')
		imagepng($new_image,$destination);

	}

	//Get Extension Function
	function get_image_extension($name) {
	$name = strtolower($name);
	$i = strrpos($name,".");
	if (!$i) { return ""; }
		$l = strlen($name) - $i;
	$extension = substr($name,$i+1,$l);
	return $extension; 
	}

	//Random Name Function
	function random_name($length) {
	$characters = "abcdefghijklmnopqrstuvwxyz01234567890";
	$name = "";

	for ($i = 0; $i < $length; $i++) {
		$name .= $characters[mt_rand(0, strlen($characters) - 1)];
	}
	return "image-".$name;
}



$images_location = "/project_files/";
$thumbs_location = "/project_thumbs/";
$thumb_width = "100";
$maximum_size = "5000000";			

//Check And Save Image			
if($_POST) {

if($_FILES['image']['name'] == ""){
	$results = "Please select the image you would like to upload by clicking browse";
}
else{
	$size=filesize($_FILES['image']['tmp_name']);
	$filename = stripslashes($_FILES['image']['name']);
	$extension = get_image_extension($filename);
	if($size > $maximum_size) {
		$results = "Your file size exceeds the maximum file size!";
	}
	else

	if (($extension != "jpg") &&
		($extension != "jpeg") &&
		($extension != "png") &&
		($extension != "gif")) {
		$results = "Only the following extensions are allowed. 'jpg', 'jpeg', 'png', 'gif'.";
	}
	else {
		$image_random_name=random_name(15).".".$extension;
		$copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
		if (!$copy) {
			$results = "Error while uploadin your image! Please try again!";
		}
		else{
		create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width);
			$results = "Your image has been uploaded!";
		}
	}
}
}
?>

 

and the form

<html>
<head>
<title>test</title>
</head>
<body>
<?php echo $results; ?>
<form action="#" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
    <input type="file" name="image" />
    <input type="submit" value="Upload Image" />
</form>
</body>
</html>

 

 

Link to comment
Share on other sites

The @ on the copy() statement is hiding any php errors that would tell you why it is failing.

 

Code should never have @ in it to hide errors (there's no need anyway because you would have display_errors set the way you want to show or not show error messages to the client.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.