10 Replies - 1143 Views - Last Post: 04 April 2014 - 02:36 AM Rate Topic: -----

#1 Wolverine89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 14-January 14

Major problem images

Posted 03 April 2014 - 01:53 PM

Hi guys,

When someone fills in the form, data is stored in mysql database. but, if both images (input fiels type file) are empty it works fine. but when i select both images or one of them i get this error:

Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\workspace\webshop\view\administrator\product\Insert_Product_Admin.php on line 141

Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\workspace\webshop\view\administrator\product\Insert_Product_Admin.php on line 144

WHY?

if (!empty($_POST['button'])){
	
	if(!isset($_FILES['image_small']['name']) && isset($_FILES['image_large']['name'])){
		
		$imagetypelarge = $_FILES['image_large']['type'];
		$imagedatalarge = file_get_contents($_FILES["image_large"]["tmp_name"]);
		
		$productc->insert($_POST['title'], $_POST['price'], $_POST['description'], $_POST['summary'], $_POST['isbn'], $_POST['publisher'],
				$_POST['pages'], $_POST['language'], $_POST['quantity'], $imagedatalarge, NULL, $_POST['category'], $_POST['author']);
		
		echo "small empty";
	}
	else if(isset($_FILES['image_small']['tmp_name']) && !isset($_FILES['image_large']['tmp_name'])){
		
		$imagetypesmall = $_FILES['image_small']['type'];
		$imagedatasmall = file_get_contents($_FILES["image_small"]["tmp_name"]);
		
		$productc->insert($_POST['title'], $_POST['price'], $_POST['description'], $_POST['summary'], $_POST['isbn'], $_POST['publisher'],
				$_POST['pages'], $_POST['language'], $_POST['quantity'], NULL, $imagedatasmall, $_POST['category'], $_POST['author']);
		echo "large empty";
	}
	else if(!isset($_FILES['image_large']['tmp_name']) && !isset($_FILES['image_large']['tmp_name'])){
	
		$productc->insert($_POST['title'], $_POST['price'], $_POST['description'], $_POST['summary'], $_POST['isbn'], $_POST['publisher'],
				$_POST['pages'], $_POST['language'], $_POST['quantity'], NULL, NULL, $_POST['category'], $_POST['author']);
		echo "both images empty";
	}
	else if(isset($_FILES['image_large']['name']) && isset($_FILES['image_large']['name'])){
	
		$imagetypelarge = $_FILES['image_large']['type'];
		$imagedatalarge = file_get_contents($_FILES["image_large"]["tmp_name"]);
		
		$imagetypesmall = $_FILES['image_small']['type'];
		$imagedatasmall = file_get_contents($_FILES["image_small"]["tmp_name"]);
		
		$productc->insert($_POST['title'], $_POST['price'], $_POST['description'], $_POST['summary'], $_POST['isbn'], $_POST['publisher'],
				$_POST['pages'], $_POST['language'], $_POST['quantity'], $imagedatalarge, $imagedatasmall, $_POST['category'], $_POST['author']);
		echo "both images filled";
	}
}



Databases contains two fields, image_large = longblog, image_small = blob

Is This A Good Question/Topic? 0
  • +

Replies To: Major problem images

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Major problem images

Posted 03 April 2014 - 02:05 PM

Which are lines 141 and 144 in your posted code? But
else if(isset($_FILES['image_large']['name']) && isset($_FILES['image_large']['name'])){

you are checking image_large twice, but then attempt to use image_small, which may not exist (be set).

 else if(!isset($_FILES['image_large']['tmp_name']) && !isset($_FILES['image_large']['tmp_name'])){

This statement is flawed as well.

It will be easier to code, and to read, if you first use two booleans to determine if either image has been uploaded.
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: Major problem images

Posted 03 April 2014 - 02:10 PM

You might have to use empty instead of isset. I believe it's going to be set regardless of whether or not a file was uploaded. Instead of checking if the name is empty though, you should check the error code for the file. There is a specific code that says whether or not a file was uploaded.

This post has been edited by ArtificialSoldier: 03 April 2014 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 Wolverine89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 14-January 14

Re: Major problem images

Posted 03 April 2014 - 02:10 PM

okay maybe this is much clearer. I have to input fields, named: image_large and image_small and a button.

But the output is always "both filled"

if (isset($_POST['button'])){
	
	$file_large = $_FILES['image_large']['tmp_name'];
	$file_small = $_FILES['image_small']['tmp_name'];
	
	if(!isset($file_large) && !isset($file_small)){
		echo "both empty";
		return;
	}
	else if(isset($file_large) && !isset($file_small)){
		echo "small empty";
		return;
	}
	else if(!isset($file_large) && isset($file_small)){
		echo "large empty";
		return;
	}
	else if(isset($file_large) && isset($file_small)){
		echo "both filled";
		return;
	}

}


Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: Major problem images

Posted 03 April 2014 - 02:12 PM

Yes, those variables are always set, you set them right on the top.
Was This Post Helpful? 0
  • +
  • -

#6 Wolverine89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 14-January 14

Re: Major problem images

Posted 03 April 2014 - 02:17 PM

Ok one problem solved! i need to use empty. Question how can i store those variables in my database

$file_large = $_FILES['image_large']['tmp_name'];
$file_small = $_FILES['image_small']['tmp_name'];

next thing, when i am going to insert the data i use this

	else if(!empty($file_large) && !empty($file_small)){
		
		$image_large = file_get_contents($_FILES["image_large"]["tmp_name"]);
		$image_small = file_get_contents($_FILES["image_small"]["tmp_name"]);
		
		$productc->insert($_POST['title'], $_POST['price'], $_POST['description'], $_POST['summary'], $_POST['isbn'], $_POST['publisher'],
			$_POST['pages'], $_POST['language'], $_POST['quantity'], $image_large, $image_small, $_POST['category'], $_POST['author']);
		
		echo "both filled";
		return;
	}



But i get always this error:

Warning: mysqli::query(): MySQL server has gone away in C:\xampp\htdocs\workspace\webshop\model\Product.php on line 13

Warning: mysqli::query(): Error reading result set's header in C:\xampp\htdocs\workspace\webshop\model\Product.php on line 13
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: Major problem images

Posted 03 April 2014 - 02:21 PM

That error has several causes. I would guess that the query is too long.

https://www.google.c...=UTF-8&oe=UTF-8

You should also validate the uploads, there are several possible error conditions that you're not checking for, such as the file being too large. If someone uploaded a file that was too large you would just assume they didn't upload anything and tell them it worked.

http://www.php.net/m...file-upload.php
Was This Post Helpful? 0
  • +
  • -

#8 Wolverine89   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 14-January 14

Re: Major problem images

Posted 03 April 2014 - 02:34 PM

the query works fine when both images are empty

$file_large = $_FILES['image_large']['tmp_name'];
$file_small = $_FILES['image_small']['tmp_name'];

if(empty($file_large) && empty($file_small)){
		
		$productc->insert($_POST['title'], $_POST['price'], $_POST['description'], $_POST['summary'], $_POST['isbn'], $_POST['publisher'],
			$_POST['pages'], $_POST['language'], $_POST['quantity'], NULL, NULL, $_POST['category'], $_POST['author']);
		
		echo "both empty";
		return;
	}



but when both images or one of them needs to be upload, its not working. No data in mysql database

$image_large = file_get_contents($_FILES["image_large"]["tmp_name"]);
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Major problem images

Posted 03 April 2014 - 02:44 PM

Inserting two NULL values is a lot simpler than two large objects, so the fact that that statement works is, I'm afraid, of limited use.

Did you read the link that ArtificialSoldier provided?

Quote

You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server's max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section C.5.2.10, “Packet Too Large”.


Hint: Concentrate on getting a single, small, image saved firsts.
Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: Major problem images

Posted 03 April 2014 - 03:15 PM

Quote

the query works fine when both images are empty

Wow. It's almost like...

Quote

I would guess that the query is too long.

Was This Post Helpful? 0
  • +
  • -

#11 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Major problem images

Posted 04 April 2014 - 02:36 AM

out of curiousity, do you really need to save the images in the DB? doesn’t it suffice to save the images on the file system and only save their location in the DB?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1