ckrudelux Posted October 5, 2009 Share Posted October 5, 2009 Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 2304 bytes) in /home/virtual/equmeniasvestavik.se/public_html/upload2.php on line 103I guess this means what the file is to big or something. Link to comment Share on other sites More sharing options...
Frieling Posted October 5, 2009 Share Posted October 5, 2009 I think so. You need to check to see how much your server is allowed to have a file upload size of. Then you can change that to what you put your default max upload size. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 That error means your script is using too much memory, that doesn't have anything to do with the file upload size. Your script has a memory limit of 20MB and it's trying to use more than that. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 That error means your script is using too much memory, that doesn't have anything to do with the file upload size. Your script has a memory limit of 20MB and it's trying to use more than that.Okay the server php is set to only allowes 6mb at my own server I had 20mb as a limit this was changed by the .htaccess file but I don't use this on the new server. I don't have any other script saying 20mb is the limit. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 20MB is definitely the limit, because your script is showing an error when it tries to allocate more than 20MB. The memory limit can either be set in php.ini, htaccess, or your script, but you can't set it higher than the value from php.ini or htaccess in a script. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 20MB is definitely the limit, because your script is showing an error when it tries to allocate more than 20MB. The memory limit can either be set in php.ini, htaccess, or your script, but you can't set it higher than the value from php.ini or htaccess in a script.Odd couse this is not my server and they say the limit is 6mb. Still I can't upload a file on 2mb. I can't use .htaccess files to change the limit of the size it just ends up with an 500 error. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 Again, the memory limit has nothing to do with the max upload size. Your error is a memory limit error, not an upload size error. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 Again, the memory limit has nothing to do with the max upload size. Your error is a memory limit error, not an upload size error.Okay I see, I have no clue what so ever on what you are trying to say is wrong, I think you are saying that the settings for the php ini file is wrong? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 Nothing is "wrong", it's just that your script is trying to use more RAM than it's allowed to. Why that is I couldn't say without seeing what your code is trying to do, but the server you're running that on is limiting PHP scripts to use 20MB of RAM only. Any variable you create uses RAM. If you're reading a bunch of data into variables that's probably the issue. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 Nothing is "wrong", it's just that your script is trying to use more RAM than it's allowed to. Why that is I couldn't say without seeing what your code is trying to do, but the server you're running that on is limiting PHP scripts to use 20MB of RAM only. Any variable you create uses RAM. If you're reading a bunch of data into variables that's probably the issue.I think I know what you mean.. what the page is doing when this error appers is resizing images.First it save the image to 3 diffrent locations after that the code resize 2 of them to smaller versions of the image.So can you give me some clues on what I have to change to make this work? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 The basic idea is to delete things from memory once you're done using them. If you've resized one image, there's no reason to keep it in memory while you're resizing the other two. Process each image individually and remove things from memory once you're done using them. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 The basic idea is to delete things from memory once you're done using them. If you've resized one image, there's no reason to keep it in memory while you're resizing the other two. Process each image individually and remove things from memory once you're done using them.How do I do what? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 You can use unset to remove a variable from memory, or set it to null. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 You can use unset to remove a variable from memory, or set it to null.I used unset to delete files on the server but to delete variables.. Cool! So you mean I can do this:$test = "Some Context";echo $test;unset($text); Or in this case unset the " $_FILES['file']['name'] " Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 I used unset to delete files on the serverUnlink deletes a file, unset deletes a variable from memory.It's not going to help a lot to unset $_FILES['file']['name'], the only thing that's going to remove from memory is the name element of that array. If it's a string, that will free up 1 or 2 bytes per character. If the filename has 50 characters, at most you're only freeing up 100 bytes of RAM. That's probably not going to help much. For images, if you're using one of the imagecreate functions to return a handle resource to an image, unset the handle after you're finished resizing the image. The imagedestroy function will also free up memory associated with an image handle resource. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 Unlink deletes a file, unset deletes a variable from memory.Sorry I was reading too fast.It's not going to help a lot to unset $_FILES['file']['name'], the only thing that's going to remove from memory is the name element of that array. If it's a string, that will free up 1 or 2 bytes per character. If the filename has 50 characters, at most you're only freeing up 100 bytes of RAM. That's probably not going to help much. For images, if you're using one of the imagecreate functions to return a handle resource to an image, unset the handle after you're finished resizing the image. The imagedestroy function will also free up memory associated with an image handle resource.Well the function resizing the image ends with imagedestroy. But seens I upload the image to the server at the begining of the script and move the image to some locations and after that I resize them but I guess the temp file on the server is still there so how do I clear the temp file? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 The temp file is not in memory. Variables are in memory. Is the temp file a variable, did you read the data into a variable?You might just want to show your code. There are a million ways to use memory. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 5, 2009 Author Share Posted October 5, 2009 The temp file is not in memory. Variables are in memory. Is the temp file a variable, did you read the data into a variable?You might just want to show your code. There are a million ways to use memory.Okay...$file = $_FILES["file"]["type"];$fileext = strtolower(array_pop(explode('.', $_FILES["file"]["name"])));....}else if(substr($file, 0, 5) == "image" or $fileext == "jpg" or $fileext == "jpeg" or $fileext == "png" or $fileext == "gif") { function resize_image($opts) { $src = isset($opts['source']) ? $opts['source'] : ''; $dest = isset($opts['dest']) ? $opts['dest'] : ''; $w = isset($opts['w']) ? intval($opts['w']) : 0; $h = isset($opts['h']) ? intval($opts['h']) : 0; if ($src == '') { return; } if ($w == 0 && $h == 0) { return; } if ($dest == '') $dest = $src; // resize in place // open the image $ext = strtolower(array_pop(explode('.', $src))); switch ($ext) { case 'jpg': case 'jpeg': $i = imagecreatefromjpeg($src); break; case 'gif': $i = imagecreatefromgif($src); break; case 'png': $i = imagecreatefrompng($src); break; default: return; } $new_w = imagesx($i); $new_h = imagesy($i); if (($w != 0 && $new_w < $w && $h == 0) || ($w == 0 && $h != 0 && $new_h < $h) || ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h)) { // image is small enough if ($dest != $src) copy($src, $dest); return; } // determine new size if ($w != 0 && $new_w > $w) { $new_h = ($w / $new_w) * $new_h; $new_w = $w; } if ($h != 0 && $new_h > $h) { $new_w = ($h / $new_h) * $new_w; $new_h = $h; } // resize $new = imagecreatetruecolor($new_w, $new_h); imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i)); imagedestroy($i); // save the image switch ($ext) { case 'jpg': case 'jpeg': imagejpeg($new, $dest, 100); break; case 'gif': imagegif($new, $dest); break; case 'png': imagepng($new, $dest); break; } imagedestroy($new); } $ext = strtolower(array_pop(explode('.', $_FILES["file"]["name"]))); $status = false; while($status == false){ $newimage = rand(001,999).date("ymd").'.'.$ext; if(!file_exists("big/".$newimage) and !file_exists("small/".$newimage) and !file_exists("thumb/".$newimage)){ move_uploaded_file($_FILES["file"]["tmp_name"], "big/" . $newimage); $status = true; } } resize_image(array( 'source' => 'big/'.$newimage, 'dest' => 'small/'.$newimage, // optional, leave off to overwrite source 'w' => 600, 'h' => 600 )); resize_image(array( 'source' => 'big/'.$newimage, 'dest' => 'thumb/'.$newimage, // optional, leave off to overwrite source 'w' => 150, 'h' => 150 )); $thumb = "thumb/".$newimage; $small = "small/".$newimage; $big = "big/".$newimage; $category = mysql_real_escape_string(htmlspecialchars($_POST['category'])); if(!isset($_POST['category']) or $category == ""){ $category = mysql_real_escape_string(htmlspecialchars($_POST['select'])); } $date = date("Y-m-d"); mysql_query("INSERT INTO images (thumb, small, big, category, date) VALUES ('$thumb', '$small', '$big', '$category', '$date')"); if($_SERVER['HTTP_REFERER'] != ''){ header('Location: '. $_SERVER['HTTP_REFERER']); exit; }else{ header('Location: index.php'); exit; }} Link to comment Share on other sites More sharing options...
justsomeguy Posted October 5, 2009 Share Posted October 5, 2009 Which line is it saying the error happened on? What's the filesize of the image you're uploading? Link to comment Share on other sites More sharing options...
ckrudelux Posted October 6, 2009 Author Share Posted October 6, 2009 $file = $_FILES["file"]["type"];$fileext = strtolower(array_pop(explode('.', $_FILES["file"]["name"])));....}else if(substr($file, 0, 5) == "image" or $fileext == "jpg" or $fileext == "jpeg" or $fileext == "png" or $fileext == "gif") { function resize_image($opts) { $src = isset($opts['source']) ? $opts['source'] : ''; $dest = isset($opts['dest']) ? $opts['dest'] : ''; $w = isset($opts['w']) ? intval($opts['w']) : 0; $h = isset($opts['h']) ? intval($opts['h']) : 0; if ($src == '') { return; } if ($w == 0 && $h == 0) { return; } if ($dest == '') $dest = $src; // resize in place // open the image $ext = strtolower(array_pop(explode('.', $src))); switch ($ext) { case 'jpg': case 'jpeg': $i = imagecreatefromjpeg($src); <----- LINE 103 ----------------------------------------- break; case 'gif': $i = imagecreatefromgif($src); break; case 'png': $i = imagecreatefrompng($src); break; default: return; } $new_w = imagesx($i); $new_h = imagesy($i); if (($w != 0 && $new_w < $w && $h == 0) || ($w == 0 && $h != 0 && $new_h < $h) || ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h)) { // image is small enough if ($dest != $src) copy($src, $dest); return; } // determine new size if ($w != 0 && $new_w > $w) { $new_h = ($w / $new_w) * $new_h; $new_w = $w; } if ($h != 0 && $new_h > $h) { $new_w = ($h / $new_h) * $new_w; $new_h = $h; } // resize $new = imagecreatetruecolor($new_w, $new_h); imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i)); imagedestroy($i); // save the image switch ($ext) { case 'jpg': case 'jpeg': imagejpeg($new, $dest, 100); break; case 'gif': imagegif($new, $dest); break; case 'png': imagepng($new, $dest); break; } imagedestroy($new); } $ext = strtolower(array_pop(explode('.', $_FILES["file"]["name"]))); $status = false; while($status == false){ $newimage = rand(001,999).date("ymd").'.'.$ext; if(!file_exists("big/".$newimage) and !file_exists("small/".$newimage) and !file_exists("thumb/".$newimage)){ move_uploaded_file($_FILES["file"]["tmp_name"], "big/" . $newimage); $status = true; } } resize_image(array( 'source' => 'big/'.$newimage, 'dest' => 'small/'.$newimage, // optional, leave off to overwrite source 'w' => 600, 'h' => 600 )); resize_image(array( 'source' => 'big/'.$newimage, 'dest' => 'thumb/'.$newimage, // optional, leave off to overwrite source 'w' => 150, 'h' => 150 )); $thumb = "thumb/".$newimage; $small = "small/".$newimage; $big = "big/".$newimage; $category = mysql_real_escape_string(htmlspecialchars($_POST['category'])); if(!isset($_POST['category']) or $category == ""){ $category = mysql_real_escape_string(htmlspecialchars($_POST['select'])); } $date = date("Y-m-d"); mysql_query("INSERT INTO images (thumb, small, big, category, date) VALUES ('$thumb', '$small', '$big', '$category', '$date')"); if($_SERVER['HTTP_REFERER'] != ''){ header('Location: '. $_SERVER['HTTP_REFERER']); exit; }else{ header('Location: index.php'); exit; }} Marked the line 103 Link to comment Share on other sites More sharing options...
ckrudelux Posted October 6, 2009 Author Share Posted October 6, 2009 Here I think is the problem a 3072px * 2304px image will takes alot of space then it's raw file and I guess it's a raw file you create then you resize it in php. That size of an image would be huge. Just for testing I changed the image to bmp and the image became 20,2mb instead of 2,14 as jpg.Any truth in this hypothesis or am I totaly lost? Link to comment Share on other sites More sharing options...
Synook Posted October 6, 2009 Share Posted October 6, 2009 Yes, a 7 MP RAW image will tax the server a bit... I believe the entire image is loaded into RAM upon opening. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 6, 2009 Share Posted October 6, 2009 Get it to print the memory usage before you create the image to see if it's the image that's taking up everything or if it's something else. e.g.:echo 'memory used: ' . memory_get_usage(true);$i = imagecreatefromjpeg($src);If it prints a small number there (<1000000) then the image is taking up all of the memory, and you don't have enough memory allocated to PHP in order for PHP to open images that large. If it's a large number then something else is taking up a bunch of memory, and you may be able to open the image by removing whatever else is in memory. If you can't open the image, you may be able to use an external command to resize the image, although if you don't have control over the server you're going to be limited. Of course, if you do have control over the server you might as well just increase the memory limit for PHP. Link to comment Share on other sites More sharing options...
ckrudelux Posted October 6, 2009 Author Share Posted October 6, 2009 Get it to print the memory usage before you create the image to see if it's the image that's taking up everything or if it's something else. e.g.:echo 'memory used: ' . memory_get_usage(true);$i = imagecreatefromjpeg($src);If it prints a small number there (<1000000) then the image is taking up all of the memory, and you don't have enough memory allocated to PHP in order for PHP to open images that large. If it's a large number then something else is taking up a bunch of memory, and you may be able to open the image by removing whatever else is in memory. If you can't open the image, you may be able to use an external command to resize the image, although if you don't have control over the server you're going to be limited. Of course, if you do have control over the server you might as well just increase the memory limit for PHP.Okay so I got the number 262144, so if I understand you right it uses all the memory. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 6, 2009 Share Posted October 6, 2009 Yeah, that means the PHP script is only using about 250KB before opening the image, and opening the image requires at least 20MB. If you're in control of the server, you can probably just increase the memory limit. A 20MB limit is pretty small anyway, it defaults to 32MB. I've got a server where I set it to 512MB. If you can't change that, ask your host if there's an image resizing utility you can use from PHP. If you have PHP run a system command to resize the image it won't use PHP's memory space. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.