ShibSta Posted September 29, 2006 Share Posted September 29, 2006 I need an image upload script that checks if a file uploaded is infact an image and not a script.Also, I'd like to be able to define allowed file types easily using (define("FILE_TYPES", "gif, jpeg, jpg, png");)I know this is a rather straight forward and "give me the code" question.However, I have tried making one for awhile now and finally gave in to asking for one.Also, is it possible to do checks before you move the file? (Ex. check width, height, and filesize)If no, is there a way to delete the file if a check fails? Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 30, 2006 Author Share Posted September 30, 2006 bump ^^Anyone? Please. Quote Link to comment Share on other sites More sharing options...
jeremywesselman Posted September 30, 2006 Share Posted September 30, 2006 Have you looked at the tutorial here at phpfreaks?[url=http://www.phpfreaks.com/tutorials/85/0.php]http://www.phpfreaks.com/tutorials/85/0.php[/url]It is tutorial on a file upload class with file extension validation, getting sizes and many other things.[color=maroon]Jeremy[/color] Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 30, 2006 Author Share Posted September 30, 2006 I tried that and I cant even get the file extention to verify...I made sure .gif was in the ext array and it returns false every time, I removed the ext array validation and started getting file already exists error...I can't get that class to work properly... Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 30, 2006 Author Share Posted September 30, 2006 bump ^^Anyone? I need this resolved asap if possible, kinda stuck on my project until this part is finished...I wouldnt be surprised if the answer is in the comments on the bottom of the tutorial's page but when I click on them it refreshes the page but does not display the comments... Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 30, 2006 Share Posted September 30, 2006 use the getimagesize() function to check width and height. It also checks the filetype so that even something named wombat.jpg will fail if it isn't a real jpg file (and similarly for any image type you want).http://fundisom.com/phparadise/php/image_handling/image_upload_and_resizeis lixlpixel's script that works 'straight out of the box'.That should get you moving in the right direction. Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 30, 2006 Author Share Posted September 30, 2006 Thing is I'm working with functions and classes already. If someone can help me determine the reason I am receiving errors on the class that was given to me above, I'd have everything working.ThanksAlso, I don't want a complex script that makes a thumbnail. I am only working with images/gif's of the size 100x100px Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 30, 2006 Share Posted September 30, 2006 OK, so in fact you don't want help with the things you asked about. You want someone to explain why the code we can't see doesn't work, right? Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 30, 2006 Author Share Posted September 30, 2006 The code you cant see is the code from the tutorial that was given in the link above. :)[quote]http://www.phpfreaks.com/tutorials/85/0.php[/quote]Anyway, here is my code that calls the classBasicly I have the form element -> process.php -> class_upload.phpForm POST's to process.phpprocess.php calls function addBanner()function addBanner:[code] /** * addBanner - Uploads the file and executes the MySQL Query to update the database */ function addBanner() { global $upload_files, $database, $session, $form; $upload_class =new Upload_Files; $upload_class->temp_file_name=trim($_FILES['upload']['tmp_name']); $upload_class->file_name =trim(strtolower($_FILES['upload']['name'])); $upload_class->upload_dir =IMG_PATH; $upload_class->upload_log_dir="" . IMG_PATH . "/upload_logs/"; $upload_class->max_file_size =MAX_IMG_SIZE; $upload_class->banned_array =array(""); $upload_class->ext_array =array(".gif",".jpg",".jpeg",".png"); $valid_ext =$upload_class->validate_extension(); $valid_size =$upload_class->validate_size(); $valid_user =$upload_class->validate_user(); $max_size =$upload_class->get_max_size(); $file_size =$upload_class->get_file_size(); $file_exists =$upload_class->existing_file(); $field ="userfile"; //Use field name for username if ($valid_ext) { $form->setError($field, "* Invalid file type"); $retval=1; } elseif(!$valid_size) { $form->setError($field, "* Invalid file size"); $retval=1; } elseif(!$valid_user) { $form->setError($field, "* Banned from uploading"); $retval=1; } elseif($file_exists) { $form->setError($field, "* File exists"); $retval=1; } else { $upload_file=$upload_class->upload_file_with_validation(); if (!$upload_file) { $form->setError($field, "* Error during upload"); $retval=2; } else { $retval=0; } } /* Upload Successful */ if ($retval == 0) { if ($database->addBanner($cid, $uid, $newfile)) { return 0; //Query Successfil $_SESSION['reguname'] =$_POST['file']; $_SESSION['regsuccess']=true; header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]"); } else { return 2; //Error with query $_SESSION['reguname'] =$_POST['user']; $_SESSION['regsuccess']=false; header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]"); } } /* Error found with form */ else if($retval == 1) { $_SESSION['value_array']=$_POST; $_SESSION['error_array']=$form->getErrorArray(); header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]"); } /* Upload attempt failed */ else if($retval == 2) { $_SESSION['reguname'] =$_POST['user']; $_SESSION['regsuccess']=false; header ("Location: " . BASE_URL . "/index.php?act=banners&sub=addbanner&cid=$_POST[cid]"); } }[/code] Quote Link to comment Share on other sites More sharing options...
printf Posted September 30, 2006 Share Posted September 30, 2006 Tell me exactly what you will use it for, and I will give a quick example! I have a bunch of ready made examples, so give me an idea of what one would best help you! What mean is, tell me what you need it to support, (ie: multi upload, restrict size, restrict type, ...)me! Quote Link to comment Share on other sites More sharing options...
ShibSta Posted September 30, 2006 Author Share Posted September 30, 2006 [quote author=printf link=topic=110025.msg444490#msg444490 date=1159657422]Tell me exactly what you will use it for, and I will give a quick example! I have a bunch of ready made examples, so give me an idea of what one would best help you! What mean is, tell me what you need it to support, (ie: multi upload, restrict size, restrict type, ...)me![/quote]Ok, lets see how I can best describe this.I am creating a BannerExchange that uses 100x100 images, but I want these variables to be easily changed in a config file.I want to upload images, again would be nice to have it as an easy access array in a config file with extensions allowed.I need it to verify that it is an image and not a script (Security measure)I need it to check that the size is less than a defined size in config. (51200bytes - 50kb/ default)I need it to then, after the checks, copy the temp file to a permanent location with a name I can easily specify.(ex. $id_$cid_$uid.$ext - BannerID_ChannelID_UserID.Extension)Last but not least I need to be able to implement it into my system.Form -> process.php -> funtion addBanner Quote Link to comment Share on other sites More sharing options...
ShibSta Posted October 1, 2006 Author Share Posted October 1, 2006 Ok, finally managed to debug each function 1 by 1 and fixed the errors.Thanks for the referral to that tutorial/class! Quote Link to comment 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.