son.of.the.morning Posted December 7, 2011 Share Posted December 7, 2011 Now this one is a bit of a shit head. I am using a really comprehensive class i found on on the web for upload / re-size / crop but i want to be able to override it's error handling and i am not good enough to start modifying the code in the class with out fucking it up. Firstly i will show you how i have invoked the class. if( isset($_FILES['image'] ) ) { //Class includes include("../scrips/php/cms/database.insert.class.php"); include ("../scrips/php/cms/img.upload.resize.crop.class.php.php"); //----------|Start: upload, resize & save $your_image = new _image; //----------| Upload orginal image $your_image->uploadTo = 'uploads/'; $upload = $your_image->upload($_FILES['image']); //----------| Resize and upload small thumbnail $your_image->newPath = 'thums/'; $your_image->newWidth = 50; $your_image->newHeight = 50; $resized = $your_image->resize(); //----------| Resize and upload medium thumbnail $your_image->newPath = 'thums2/'; $your_image->newWidth = 100; $your_image->newHeight = 100; $resized = $your_image->resize(); //----------| Getting the image name to insert into the database futher on in the code $name = $resized; $img_str = explode("/",$name); $final_img_name = $img_str['1']; echo "Article has been added!"; //----------------|end //----------------| Start database insert (class manipulation) $table = "blog_posts"; $title = "'".$_POST['ArticleTitle']."',"; $img = "'".$final_img_name."',"; $post = "'".$_POST['ArticleBody']."',"; $aurthor_id = "'1',"; $category_id = "'".$_POST['Category']."',"; $date_posted = "NOW()"; $values = array("$title","$img","$post","$aurthor_id","$category_id","$date_posted"); $fields = array('title,','img,','post,','aurthor_id,','category_id,','date_posted'); $obj= new DatabaseInsert; $obj->DatabaseConnectionRequire(); $obj->ArticleInsert($values,$fields,$table); //----------------|end } As you can see it's fairly basic. What i want to do is before it runs this script and starts including/invoking the class etc, i want to be able to check to see if there is a value been posted from a FILE FORM OBJECT and if there is to proceed with this script, alternatively i want it to execute another code which will handle it in regards to the concept of my page. A simple javascript alert with be ok providing it reloaded the page to it's default state. If any one can help me here i would appreciate it a lot. Thanks Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 if( isset($_FILES['image'] ) ) { // invoke class } else { // display error } Am I missing something? Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 8, 2011 Author Share Posted December 8, 2011 This was of course my first attempt but this message displays on the initial loading of the page before the form has been submitted . Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 I don't see a message. Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 8, 2011 Author Share Posted December 8, 2011 I meat the else event // display error Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Did you use enctype="multipart/form-data" in your <form> tag? Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 8, 2011 Author Share Posted December 8, 2011 Yes. The form works fine it's just if i submit it with no file in the FILE form object then it uses a error handler from the class. I don't mind the actual error that the class creates but oddly enough everything else on my page vanishes. That's why i want it to be checked and handed in the view point before the class is included to prevent it generating an error. Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Okay, now I see your problem. You can do one of two things. You can check the name of the image isn't empty, or you can process the $_FILES error code. if (!empty($_FILES['image']['name'])) { if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) { You can see all the possible error messages here: http://php.net/manual/en/features.file-upload.errors.php Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 8, 2011 Author Share Posted December 8, 2011 Firstly I would like to thank you for that it did work, however i want to be able to provide an error when the file form is empty. It has canceled out the the class error but i don't how to apply a new one. Sorry if i am not making to much sense here i am quite new at this. Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 if (!empty($_FILES['image']['name'])) { // invoke class } else { // display your error here } Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 8, 2011 Author Share Posted December 8, 2011 It works fine but it's displaying the error on the initial loading of the page before anything has been submited Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 if (!empty($_POST)) { if (!empty($_FILES['image']['name'])) { // invoke class } else { // display error } } Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted December 8, 2011 Author Share Posted December 8, 2011 YES! I have it working at last . I cant thank you enough for you help this has been head fucking my head all night! if(isset($_POST['sub'])){ if (!empty($_FILES['image']['name'])) { this is what i used in the end! 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.