6 Replies - 778 Views - Last Post: 18 November 2008 - 05:57 AM Rate Topic: -----

#1 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Array trouble in PHP

Posted 14 November 2008 - 11:47 AM

Please could anybody check my source code about upload image in database. I'm getting this error message below everytime I pressed the upload button. I marked in red the lines where I believe there's problem. Help please


Array ( )
Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\MyAdmintrator\Temp\addPhoto.php on line 7

<?php
	  if ($_POST['Submit']) {
		if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
		 [color=#FF0000] print_r($_FILES);[/color]
		  mysql_connect("localhost", "root", "") or die(mysql_error());
		  mysql_select_db("test");
		  [color=#FF0000]$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "rb"), $_FILES['file']['size']));[/color]
		  
		   $query = sprintf("INSERT INTO image(Image, FileType) VALUES('%s', '%s')", $photo, $_FILES['file']['type']);
		   if (mysql_query($query)) {
			$messages[] = "Your files is successfully store in database"; 
		   } else {
			$messages[]= mysql_error();
		   }
		  } 
		   else {
		   $messages[]="The file is bigger than the allowed size please resize";
		  }
		}
?>
	  <html>
	  <head>
	  <title>Add Image</title>
	  </head>	
	  <body>
<? 
	  if (isset($messages)) { 
		foreach ($messages as $message) {
		 print $message. "<br>";
		} 
	  }
?>
	  <form action="" method="post" enctype="multipart/form-data" name="form1">
	  <input type="file" name="file">
	  <input type="hidden" name="MAX_FILE_SIZE" value="96000">
	  <input type="submit" name="Submit" value="Submit">
	  </form>
	  </body>
	  </html>



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Array trouble in PHP

#2 RawrItsAdam  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 72
  • Joined: 10-November 08

Re: Array trouble in PHP

Posted 14 November 2008 - 12:21 PM

If you can Put your code in the [code] tages it makes it easier for us to see. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Array trouble in PHP

Posted 14 November 2008 - 02:52 PM

Correct me if I'm wrong, but I don't think you are allowed to pass fopen() directly to fread(). You have to first create a handle using fopen(), and then use fread() on that handle.

Here's an example of making a handle and using fread() on it from the PHP manual page on fread().

<?php
$handle = fopen("http://www.example.com/", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);
?>


Was This Post Helpful? 0
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Array trouble in PHP

Posted 14 November 2008 - 04:39 PM

View PostMoonbat, on 14 Nov, 2008 - 03:52 PM, said:

Correct me if I'm wrong, but I don't think you are allowed to pass fopen() directly to fread().

Actually, that works just fine. The problem with doing this is that you can't check that the open actually succeeded. Plus it's kind of hard to close a file handle when you don't save a reference to it. And it's hard to read - if you're going to read the entire file anyway, just use file_get_contents().

Anyway, the problem is obvious from the results of the print_r() - the $_FILES array isn't being populated. As for why it isn't populating, I have no idea. I tried your code on my machine and it works just fine. Perhaps it's some kind of configuration problem. Take a look at the file upload documentation and make sure your php.ini settings are correct.
Was This Post Helpful? 0
  • +
  • -

#5 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: Array trouble in PHP

Posted 18 November 2008 - 05:43 AM

[quote name='Ghost rider' date='14 Nov, 2008 - 10:47 AM' post='464685']
Please could anybody check my source code about upload image in database. I'm getting this error message below everytime I pressed the upload button. I marked in red the lines where I believe there's problem. Help please

This the error message I'm getting when I pressed upload button.

Quote

Array ( )
Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\MyAdmintrator\Temp\addPhoto.php on line
7

That script

<?php
	  if ($_POST['Submit']) {
		if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
		 [color=#FF0000] print_r($_FILES);[/color]
		  mysql_connect("localhost", "root", "") or die(mysql_error());
		  mysql_select_db("test");
		  [color=#FF0000]$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "rb"), $_FILES['file']['size']));[/color]
		  
		   $query = sprintf("INSERT INTO image(Image, FileType) VALUES('%s', '%s')", $photo, $_FILES['file']['type']);
		   if (mysql_query($query)) {
			$messages[] = "Your files is successfully store in database"; 
		   } else {
			$messages[]= mysql_error();
		   }
		  } 
		   else {
		   $messages[]="The file is bigger than the allowed size please resize";
		  }
		}
?>
	  <html>
	  <head>
	  <title>Add Image</title>
	  </head>	
	  <body>
<? 
	  if (isset($messages)) { 
		foreach ($messages as $message) {
		 print $message. "<br>";
		} 
	  }
?>
	  <form action="" method="post" enctype="multipart/form-data" name="form1">
	  <input type="file" name="file">
	  <input type="hidden" name="MAX_FILE_SIZE" value="96000">
	  <input type="submit" name="Submit" value="Submit">
	  </form>
	  </body>
	  </html>



Thanks for your correction, I was trying to find our to put quote in my code. Hope that it'll be much easier to read now.

** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#6 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: Array trouble in PHP

Posted 18 November 2008 - 05:51 AM

[quote name='RawrItsAdam' date='14 Nov, 2008 - 11:21 AM' post='464717']
If you can Put your code in the
 tages it makes it easier for us to see. Thanks :)
[/quote]

This the error message
[code]Array ( ) 
Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\MyAdmintrator\Temp\addPhoto.php on line 7


This the script

<?php
if ($_POST['Submit']) {
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
print_r($_FILES);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test");
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "rb"), $_FILES['file']['size']));

$query = sprintf("INSERT INTO image(Image, FileType) VALUES('%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query)) {
$messages[] = "Your files is successfully store in database"; 
} else {
$messages[]= mysql_error();
}
} 
else {
$messages[]="The file is bigger than the allowed size please resize";
}
}
?>
<html>
<head>
<title>Add Image</title>
</head> 
<body>
<? 
if (isset($messages)) { 
foreach ($messages as $message) {
print $message. "<br>";
} 
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="96000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>


Thank for your correction, I was trying to find how to put these tags. I hope that now it will be easier to see what the wrong with my code. Thanks
Was This Post Helpful? 0
  • +
  • -

#7 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: Array trouble in PHP

Posted 18 November 2008 - 05:57 AM

View PostAdaHacker, on 14 Nov, 2008 - 03:39 PM, said:

View PostMoonbat, on 14 Nov, 2008 - 03:52 PM, said:

Correct me if I'm wrong, but I don't think you are allowed to pass fopen() directly to fread().

Actually, that works just fine. The problem with doing this is that you can't check that the open actually succeeded. Plus it's kind of hard to close a file handle when you don't save a reference to it. And it's hard to read - if you're going to read the entire file anyway, just use file_get_contents().

Anyway, the problem is obvious from the results of the print_r() - the $_FILES array isn't being populated. As for why it isn't populating, I have no idea. I tried your code on my machine and it works just fine. Perhaps it's some kind of configuration problem. Take a look at the file upload documentation and make sure your php.ini settings are correct.



I just would like know when you pressed upload button, It's not giving error message. I'm using WAMP5 to test this code what you're using to test the code. If you've some guidance please help!Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1