I'm using this php code for an image upload. But I keep getting an error towards the end. The result "Error while uploading image. Please Try Again. I feel like I have checked everything but the dang image just wont copy into the uimages folder. Please help...
Here is the code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
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;
}
else if($height> $width){
$y =(($height - $width) /2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_height) or die('Cannot Initiatlize 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);
}
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;
}
function random_name($length){
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$name = "";
for($i = 0; $i < $length; $i++){
$name .= $characters[mt_rand(0,strlen($characters) - 1)];
}
return "image-".$name;
}
$images_location = "images";
$thumbs_location ="uimages/thumbs";
$thumb_width = 100;
$maximum_size = 5120;
$results = "Click to browse to locate the image you want to upload.";
if($_POST){
if($_FILES['image']['name'] == "")
{
$results="No Image Selected. Click Browse to find an image";
}
else{
$size=filesize($_FILES['image']['tmp_name']);
$filename = stripslashes($_FILES['image']['name']);
$extension = get_image_extension($filename);
if($size> $maximum_size){
$results="Your file exceeds the maximum size.";
}
else
if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
$results="Invalid image type. Must be jpg, jpeg, png, or a gif file.";
}
else{
$image_random_name = random_name(15).".".$extension;
$copy= @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
if(!$copy){
$results = "Error while uploading image. Please try again.";}
else{
create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name,$thumb_width);
$results="Image has been uploaded";
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5120" >
<input type="file" name="image" />
<input type="submit" value="Upload Image" />
</form>
<?php echo $results; ?>
</body>
</html>