Jump to content

Check to see if a field is empty before continuing the script


son.of.the.morning

Recommended Posts

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

 

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. 

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

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.