kev wood Posted May 21, 2009 Share Posted May 21, 2009 i have an image upload script for uploading images to a database which works well with images under 1Mb but when i try to upload images over this i get this error message Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 13056 bytes) in /home/acmeart/public_html/pip_new/cms/events_image.php on line 56 from this error message i can see that it is telling me that 16Mb of allowed memory has been used and it tried to allocate more. why is it using so much memeory when the image is only 3.25Mb here is the code i am using. <?php $id_num = $_POST['id_num']; set_time_limit(0); $link = mysql_connect(xxxxxx, xxxxxxxxxxx, xxxxxxxxxx) or die("Could not connect to host."); mysql_select_db(xxxxxxxx) or die("Could not find database."); //define a maxim size for the uploaded images define ("MAX_SIZE","28800"); // define the width and height for the thumbnail // note that theese dimmensions are considered the maximum dimmension and are not fixed, // because we have to keep the image ratio intact or it will be deformed define ("WIDTH","150"); define ("HEIGHT","120"); // this is the function that will create the thumbnail image from the uploaded image // the resize will be done considering the width and height defined, but without deforming the image function make_thumb($img_name,$filename,$new_w,$new_h) { //get image extension. $ext=getExtension($img_name); //creates the new image using the appropriate function from gd library if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext)) $src_img=imagecreatefrompng($img_name); if(!strcmp("gif",$ext)) $src_img=imagecreatefromgif($img_name); //gets the dimmensions of the image $old_x=imageSX($src_img); $old_y=imageSY($src_img); // next we will calculate the new dimmensions for the thumbnail image // the next steps will be taken: // 1. calculate the ratio by dividing the old dimmensions with the new ones // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable // and the height will be calculated so the image ratio will not change // 3. otherwise we will use the height ratio for the image // as a result, only one of the dimmensions will be from the fixed ones $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } // we create a new image with the new dimmensions $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); // resize the big image to the new created one imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // output the created image to the file. Now we will have the thumbnail into the file named by $filename if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); if (!strcmp("gif",$ext)) imagegif($dst_img,$filename); //destroys source and destination images. imagedestroy($dst_img); imagedestroy($src_img); } // This function reads the extension of the file. // It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } // This variable is used as a flag. The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an error occures. If the error occures the file will not be uploaded. $errors=0; // checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['news_image']['name']; // if it is not empty if ($image) { // get the original name of the file from the clients machine $filename = stripslashes($_FILES['news_image']['name']); // get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); // if it is not a known extension, we will suppose it is an error, print an error message //and will not upload the file, otherwise we continue if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension! Please use .gif, .jpg or .png files only.</h1>'; $errors=1; } else { // get the size of the image in bytes // $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which //the uploaded file was stored on the server $size=getimagesize($_FILES['news_image']['tmp_name']); $sizekb=filesize($_FILES['news_image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the 1MB size limit!</h1>'; $errors=1; } $rand= rand(0, 1000); //we will give an unique name, for example a random number $image_name=$rand.'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newsname="image/".$image_name; $newsname2="image/thumbs/thumb".$image_name; $newsname3="cms/image/thumbs/thumb".$image_name; $copied = copy($_FILES['news_image']['tmp_name'], $newsname); $copied = copy($_FILES['news_image']['tmp_name'], $newsname2); $sql="UPDATE news SET image= '$newsname3' WHERE id= '$id'"or die(mysql_error()); $query = mysql_query($sql)or die(mysql_error()); //we verify if the image has been uploaded, and print error instead if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { // the new thumbnail image will be placed in images/thumbs/ folder $thumb_name=$newsname2 ; // call the function that will create the thumbnail. The function will get as parameters //the image name, the thumbnail name and the width and height desired for the thumbnail $thumb=make_thumb($newsname,$thumb_name,WIDTH,HEIGHT); } } } } //If no errors registred, print the success message and show the thumbnail image created if(isset($_POST['Submit']) && !$errors) { echo "<h5>Thumbnail created Successfully!</h5>"; echo '<img src="'.$thumb_name.'">'; } echo "<form name=\"newad\" method=\"post\" enctype=\"multipart/form-data\" action=\"\">"; echo "<input type=\"file\" name=\"news_image\" >"; echo "<input type=\"hidden\" name=\"id_num\" value=\"$id_num\" />"; echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"250000\" />"; echo "<input name=\"Submit\" type=\"submit\" id=\"image1\" value=\"Upload image\" />"; echo "</form>"; echo "<br />"; echo "<br />"; echo "<h3>Please ensure that the images uploaded are under 650kb in size.</h3>"; echo "<h3>This is due to a size limit on the upload to prevent security issues.</h3>"; echo "<br />"; echo "<a href='index.php'> <h3><< Add Another Article</a></h3>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2009 Share Posted May 21, 2009 Becaise .gif, .jpg or .png files use compression to reduce the file size. When you use the GD functions it creates a uncompressed bitmap image so that it can operate on the actual image data. A 1MB .gif, .jpg or .png could easily be 50MB worth of raw image data. Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-839028 Share on other sites More sharing options...
kev wood Posted May 21, 2009 Author Share Posted May 21, 2009 is there not a way round this as other sites like facebook allow uploads and not that i have uploaded images to these sites but i have not heard of any problems with from anyone usiing those sites. i have just spoke to my hosting company and they said that the limit was set tto 8Mb for uploads. Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-839043 Share on other sites More sharing options...
unvoid Posted May 21, 2009 Share Posted May 21, 2009 you try increasing the maxsize... ;-) Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-839052 Share on other sites More sharing options...
kev wood Posted May 21, 2009 Author Share Posted May 21, 2009 i have not got access to the php.ini file on the server so i cannot do it that way. Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-839064 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2009 Share Posted May 21, 2009 If you read the documentation for what you are trying to do, you will learn that you can change the memory limit in your script - Name |Default |Changeable |Changelog memory_limit |"128M" |PHP_INI_ALL |"8M" before PHP 5.2.0, "16M" in PHP 5.2.0 Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-839074 Share on other sites More sharing options...
kev wood Posted May 26, 2009 Author Share Posted May 26, 2009 i have done some searching around an have seen in alot of places people saying that this can be done with a .htaccess file, which is something that i have not used before. from what i have read about i think all i would need to put in to this file would be the following code php_value memory_limit 96M this file should then go into the main folder for the site and it will give all folders and sub-folders from that site the memory limit of 96Mb for file uploads. is that correct or have i gone wrong somewhere along the line. Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-842154 Share on other sites More sharing options...
kev wood Posted May 26, 2009 Author Share Posted May 26, 2009 does anybody know if this code is correct i am trying to increase the memory limit made available for the uploading of files from a php script. i have spoke to my hosting company who said i cannot have access to the php.ini file but i can use .htaccess files to override this for my website. i have created this .htaccess file but it is still telling me that i have exceeded the allowed memory allocation of 16mb could someone please tell me where i am going wrong with this. here is what l have in the .htaccess file php_value post_max_size 128M php_value upload_max_filesize 128M php_value memory_limit 300M there is nothing else to the file just this, does it need anything else to make it work. i have never used these files before and would like some pointers on why it might not be working. the server has php 4.4.9 installed it says when i run phpinfo.php Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-842227 Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2009 Share Posted May 26, 2009 The syntax of those three lines is correct. If they do not result in changing the values that phpinfo(); reports, then either - a) The file is not actually .htaccess Either the host uses some other file name or the file you created is not exactly .htaccess b) The host has not enabled the setting that allows you to override php settings in a .htaccess file. c) Something else is overriding the values you are putting into the .htaccess file. d) Either the web server type or method that php is running does not support doing that. Is your web server Apache with php running as an Apache module? Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-842246 Share on other sites More sharing options...
kev wood Posted May 26, 2009 Author Share Posted May 26, 2009 i will try to answer your four points as best i can a) as far as i am aware the file is a .htaccess file inj the folder it is stored in the file type states HTACCESS FILE, is there not a standard naming convention for these files? b)when i spoke to the people from the hosting company about 20min ago they told me i dont have acess to the php.ini file but i should be able to override the the memory limit with a .htaccess file. c)how can i find out if something else is overriding the .htaccess file i have uploaded d)According to the person i spoke to i can override the phph.ini file with a .htaccess file. As for web server being Apache and running php as an Apache module i am unsure but i will past anything here which i thing might let you know (do not know to much about servers still on a very steep learning curve, this was not even looked at in uni with the course i done. as i am with .htaccess files). Linux rubidium.webfusion.co.uk 2.4.32-pipex.1.1smp #1 SMP Thu Feb 23 12:11:21 GMT 2006 i686 Apache/1.3.29 (Unix) (Red-Hat/Linux) Chili!Soft-ASP/3.6.2 mod_ssl/2.8.14 OpenSSL/0.9.6b PHP/4.4.9 FrontPage/5.0.2.2510 i could pm you with a link to the php info file if that would be easier for you as i know nothing on the server. Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-842268 Share on other sites More sharing options...
kev wood Posted May 26, 2009 Author Share Posted May 26, 2009 why is it that the simplest way to make things work is generally the last thing you find. i have spent a few days reading up on .htaccess files and speaking to the hosting company to find out if i could use them (dont get me wrong i like learning new things) but i have just found this piece of code which i think is what PFMaBiSmAd hinted at in an earlier post which does exactly what i need it to. ini_set('memory_limit', '128M'); all the searching i have done was only telling me that i could only change the memory limit by accessing the php.ini file or through the use of a .htaccess file. the line of code above will simply set the limit for that script only thus allowing larger files to uploaded when they are needed. thanks for any replies from anyone who has helped on this topic. Quote Link to comment https://forums.phpfreaks.com/topic/159097-solved-image-upload-priblems/#findComment-842372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.