Hi, I have had some great help here creating a picture upload form with automatic resize, But I have come accross a problem - it does not seem to like huge pictures.
Here is the previous thread:
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21521973.htmlWhen I was testing earlier I was only using 800x600 pics to test it, but now I am trying pictures that are about 1.5Mb + and a much higher res and I am getting this error:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 7768 bytes) in /home/httpd/vhosts/cloverl
eaf4.co.uk
/httpdocs/
upload.php
on line 76
Also when I tried a 2.4Mb picture I got the error message that was built in to the code: There was an error uploading the file, please try again!
Any ideas? I have adapted the code slightly, nothing major - just the text that users see, heres the latest:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload A File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
if($_SERVER['REQUEST_METHO
D']!='POST
') {
?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-da
ta" name="form1">
<table width="350" style="background-color:#F
6F6F6;bord
er:1px solid black;" border="0" cellspacing="2" cellpadding="2">
<tr>
<td style="font-weight:bold;ba
ckground-c
olor:#DDD;
">Upload a file </td>
</tr>
<tr>
<td style="text-align:center">
<input name="file" type="file" size="30"></td>
</tr>
<tr>
<td style="text-align:center">
<input type="submit" name="Submit" value="Upload"></td>
</tr>
</table>
</form>
<?
} else {
$message = "";
$validExtensions[] = "jpg";
$validExtensions[] = "jpeg";
$validExtensions[] = "gif";
$validExtensions[] = "png";
$sentFileSize = ceil($_FILES['file']['size
']/1024);
# this way is more accurate for the extension
$sentFileExtension = explode(".", $_FILES['file']['name']);
$number = count($sentFileExtension);
$sentFileExtension = $sentFileExtension[$number
-1];
if(!in_array($sentFileExte
nsion, $validExtensions)) {
$message = "Your file has <span style=\"color:red\">NOT</s
pan> been uploaded,";
$message .= " because of a bad extension. We only accept JPG and BMP files.";
} else {
// SAVE THE FILE
$uploaddir = 'images/uploads/';
if (file_exists('uploadCount.
txt')) {
$handle = fopen('uploadCount.txt','r
+');
$newFileNumber = fread($handle,filesize('up
loadCount.
txt'))+1;
fclose($handle);
$handle = fopen('uploadCount.txt','w
');
fwrite($handle,$newFileNum
ber.'');
fclose($handle);
} else {
$handle = fopen('uploadCount.txt','x
');
fwrite($handle,'1');
$newFileNumber = 1;
}
$uploadfile = $uploaddir . basename($newFileNumber . "." . $sentFileExtension);
$bron = $_FILES['file']['tmp_name'
];
$maxbreedte = 640;
}
if(!empty($bron)){
$dimensies = getimagesize($bron);
$breedte = $dimensies[0];
$hoogte = $dimensies[1];
if($breedte > $maxbreedte){
$nieuwebreedte = $maxbreedte;
$deelfactor = $breedte / $maxbreedte;
$nieuwehoogte = $hoogte / $deelfactor;
switch ($dimensies['mime']) {
case 'image/jpeg':
$image = imagecreatefromjpeg($bron)
;
$destination = imagecreatetruecolor($nieu
webreedte,
$nieuwehoogte);
imagecopyresampled($destin
ation, $image, 0, 0, 0, 0, $nieuwebreedte, $nieuwehoogte, $breedte, $hoogte);
imagejpeg($destination, $uploadfile);
imagedestroy($image);
imagedestroy($destination)
;
break;
case 'image/gif':
$image = imagecreatefromgif($bron);
$destination = imagecreate($nieuwebreedte
, $nieuwehoogte);
imagecopyresampled($destin
ation, $image, 0, 0, 0, 0, $nieuwebreedte, $nieuwehoogte, $breedte, $hoogte);
imagegif($destination, $uploadfile);
imagedestroy($image);
imagedestroy($destination)
;
break;
case 'image/png':
$image = imagecreatefrompng($bron);
$destination = imagecreate($nieuwebreedte
, $nieuwehoogte);
imagecopyresampled($destin
ation, $image, 0, 0, 0, 0, $nieuwebreedte, $nieuwehoogte, $breedte, $hoogte);
imagepng($destination, $uploadfile);
imagedestroy($image);
imagedestroy($destination)
;
break;
}
} else {
$uploadfile = $uploaddir . basename($newFileNumber. "." .$sentFileExtension);
if (move_uploaded_file($_FILE
S['file'][
'tmp_name'
], $uploadfile)) {
$message = "Your file has been successfully uploaded.<br>";
$message .= "Link to your file: [img]".$uploadfile."[/img]
";
} else {
$message = "There was an error uploading the file, please try again!";
}
}
}
if (is_file($uploadfile)) {
$message = "Your file has been successfully uploaded.<br>";
$message .= "Link to your file, copy bold text to display picture in forum: <b>[img]
http://www.cloverleaf4.co.uk/".$upload
file."[/im
g]</b>";
} else {
$message = "There was an error uploading the file, please try again!";
}
?>
<table width="350" style="background-color:#F
6F6F6;bord
er:1px solid black;" border="0" cellspacing="2" cellpadding="2">
<tr>
<td style="font-weight:bold;ba
ckground-c
olor:#DDD;
">File upload result </td>
</tr>
<tr>
<td style="text-align:center">
<? echo $message ?></td>
</tr>
</table>
<?
}
?>
</body>
</html>
Thanks for any help solving the error :)
Jack
Start Free Trial