Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problem-PHP Image upload to mysql

mysql

  • Please log in to reply
5 replies to this topic

#1 pratiktyagi1

pratiktyagi1

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:C, Java, PHP, JavaScript

Posted 04 October 2012 - 02:34 PM

Hello Members..
I am Prateek Tyagi,PHP beginner,I want to get save a image in mysql database but there are some warnings on execution..

Warning: fopen(C:\xampp\tmp\php99D.tmp) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\php_db\user.php on line 25

Warning: filesize() [function.filesize]: stat failed for C:\xampp\tmp\php99D.tmp in C:\xampp\htdocs\php_db\user.php on line 26

Warning: fread() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php_db\user.php on line 26

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php_db\user.php on line 28

I tried a lot but could'nt find the result.Please help me !
Thankyou.

HERE IS THE CODE.

<?php
if(!is_dir("images"))
{
mkdir("images");
}
if($_FILES['image']['error']==0)
{
	if($_FILES['image']['type']=="image/jpeg" ||$_FILES['image']['type']=="image/gif")
	{
		
		$source=$_FILES['image']['tmp_name'];
		$target="images/".$_FILES['image']['name'];
		move_uploaded_file($source, $target);
	}
}
else {
	echo "Error:".$_FILES['image']['error'];
}



$imgname=$_FILES['image']['name'];
$tmpname=$_FILES['image']['tmp_name'];

$fp=fopen("$tmpname",'r');
$data=fread($fp,filesize($tmpname));
$data=addslashes($data);
fclose($fp);

$con=mysql_connect("localhost","PratikTyagi","");
if(!$con)
{
	die("Could not connect to database.<br>".mysql_error());
}
mysql_select_db("php_db",$con);
$imgquery="insert into photo values('$data','$imgname',$size)";
mysql_query($imgquery,$con);

?>

Edited by BenW, 04 October 2012 - 02:38 PM.
Added code tags for readability

  • 0

#2 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 04 October 2012 - 02:46 PM

I think there is a bit of issues here. I see that you upload the folder to $target, can you open the $target variable ... why are you getting the temp again ... I think you're reading from temp that's why ... try fopen("$target") and see if it helps.


$imgname=$_FILES['image']['name'];
$tmpname=$_FILES['image']['tmp_name'];

$fp=fopen("$tmpname",'r');
$data=fread($fp,filesize($tmpname));
$data=addslashes($data);
fclose($fp);

  • 1

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#3 pratiktyagi1

pratiktyagi1

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:C, Java, PHP, JavaScript

Posted 05 October 2012 - 01:05 AM

Thank you friend it works..Thanks again.
  • 0

#4 pratiktyagi1

pratiktyagi1

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:C, Java, PHP, JavaScript
  • Learning:C, Java, PHP, JavaScript

Posted 05 October 2012 - 02:46 AM

The code is going well and also i can retrieve the image from the data base..but when i am trying to set text with the image(echo "text".$image) or trying to apply tags(echo "<center>".$image."/center") it causes error..that is..
The Image "http://localhost/php.../show.php?id=1" can not be displayed because it contains error.

Please assist me how can i apply all these things to the image.
Thank you.
  • 0

#5 Colanth

Colanth

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 165 posts
  • Location:Fayetteville, NC, USA
  • Programming Language:PHP, (Visual) Basic, JavaScript, Visual Basic .NET, Pascal, Logo, Assembly, VBScript, Others

Posted 14 October 2012 - 04:44 PM

"http://localhost/php.../show.php?id=1" isn't an image, it's a link. Try debugging the code to see why $image has the wrong text at that point. (It's getting it from somwewhere, but it's getting the wrong data.)
  • 0

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 22 October 2012 - 06:41 AM

You need to try to find the error in your image-generating code in show.php first. try running it without the header() and try to get output from the file correctly as hex text as a debug mode, or set a param debug=1 means you don't do header() while debugging.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download