I had a php script made for me but it seems a bit bugy with large pictures, It is supposed to take any size picture (in res or Mb) upload it and auto resize it to 640*X if the width is larger than 640 pixels, if under 640 it is left alone and just uploaded and the user is given the link.
I keep getting errors with the larger pics:
Code: Select all
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 2592 bytes) in /home/httpd/vhosts/cloverleaf4.co.uk/httpdocs/upload.php on line 76This is my php.ini file:
php_value upload_max_filesize = 8M
php_value memory_limit = 25M
You can test it here:
http://www.cloverleaf4.co.uk/upload.php
I would really appreciate any help with this as it is driving me up the wall!
Cheers
Jack
This is my code:
Code: Select all
<!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_METHOD']!='POST') {
?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="form1">
<table width="350" style="background-color:#F6F6F6;border:1px solid black;" border="0" cellspacing="2" cellpadding="2">
<tr>
<td style="font-weight:bold;background-color:#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($sentFileExtension, $validExtensions)) {
$message = "Your file has <span style=\"color:red\">NOT</span> 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('uploadCount.txt'))+1;
fclose($handle);
$handle = fopen('uploadCount.txt','w');
fwrite($handle,$newFileNumber.'');
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($nieuwebreedte, $nieuwehoogte);
imagecopyresampled($destination, $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($destination, $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($destination, $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($_FILES['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/".$uploadfile."[/img]</b>";
} else {
$message = "There was an error uploading the file, please try again!";
}
?>
<table width="350" style="background-color:#F6F6F6;border:1px solid black;" border="0" cellspacing="2" cellpadding="2">
<tr>
<td style="font-weight:bold;background-color:#DDD;">File upload result </td>
</tr>
<tr>
<td style="text-align:center"><? echo $message ?></td>
</tr>
</table>
<?
}
?>
</body>
</html>feyd | Please use
Code: Select all
tags when posting php code, especially large chunks such as this.[/color]