Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Tutorial: Storing Images in MySQL with PHP

max_file_size mysql images php

  • This topic is locked This topic is locked
47 replies to this topic

#25 plyswthsqurles

plyswthsqurles

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 July 2009 - 07:40 PM

great tutorial, just what i needed
  • 0

#26 Guest_arslan220_*

Guest_arslan220_*
  • Guest

Posted 27 July 2009 - 12:35 AM

thanks for this tutorial.
  • 0

#27 hardinera

hardinera

    CC Regular

  • Just Joined
  • PipPipPip
  • 41 posts

Posted 29 July 2009 - 10:28 PM

thanks for the tutorial really nice hehe :)

um.. is it posible that pic_id can hold 2 or more picture at the same time?

table would be :

pic_id : int
pic_1: blob
pic_2: blob
pic_3: blob
....etc...

then if i call pic_id all pictures will appear? kinda like that hehe:rolleyes:

or updating the pics?
just wondering hihihi.. is there a simple/short code for that? >.> like this tutorial?


thanks,
hardinera

Edited by hardinera, 30 July 2009 - 12:44 AM.

  • 0

#28 Filips

Filips

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 30 July 2009 - 04:35 PM

Helllo Jordon,
I followed your instruction -

DB and Table
DATABASE Name : binary;

TAble Name : tbl_images (
> id tinyint(3) unsigned NOT NULL auto_increment,
> image blob NOT NULL,
> PRIMARY KEY (id)
> );


<html>
<head>
</head>
<body>
<h1>Upload Your Image</h1>
<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</body>
</html>


insert.php


<html>
<head></head>
<body>
<?php
// Create MySQL login values and
// set them to your login information.
$username = "root";
$password = "1234";
$host = "localhost";
$database = "binary";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);

// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);

// Print results
print "Thank you, your file has been uploaded.";
}
else {
print "No image selected/uploaded";
}
// Close our MySQL Link
mysql_close($link);
?>
</body>
</html>

End of insert.php

view.php

<html>
<head>
</head>
<body>
<?php

$username = "root";
$password = "1234";
$host = "localhost";
$database = "binary";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = 1;
//$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;
}
?>
</body>
</html>

end of view.php

Problem 1: I can add 1 image,even if I tried to insert another image, it shows successfull,but when I open the mysql talbe, I see only one record

Problem 2: view.php open my Dreamweaver and shows garbage data.

Can you please help me?

Edited by Orjan, 30 July 2009 - 11:38 PM.
Use code tags

  • 0

#29 hardinera

hardinera

    CC Regular

  • Just Joined
  • PipPipPip
  • 41 posts

Posted 30 July 2009 - 05:34 PM

if i replace insert with update

$query = "UPDATE members SET image='$data' WHERE id=1";

but nothing happens >.>

am i missing sumthing?

Edited by hardinera, 30 July 2009 - 08:56 PM.

  • 0

#30 Orjan

Orjan

    CC Mentor

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

Posted 30 July 2009 - 11:37 PM

view.php


<html>
<head>
</head>
<body>
<?php

$username = "root";
$password = "1234";
$host = "localhost";
$database = "binary";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = 1;
//$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;
}
?>
</body>
</html>

end of view.php


The view.php shall NOT have any html tags! Remove them and it will work better to view the images.
  • 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.


#31 Filips

Filips

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 01 August 2009 - 06:12 PM

Thank you very very much.I will try this code.Thank you again.
  • 0

#32 jpiche

jpiche

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 12 August 2009 - 08:14 PM

Also, if you have a limited number of small images being stored in the table, this technique could also be used to take advantage of mysql's cache--maybe.
  • 0

#33 Filips

Filips

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 13 August 2009 - 02:44 PM

Thank you very much.
  • 0

#34 ssSuave

ssSuave

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 20 August 2009 - 04:53 PM

Hi thanks again for your help

So my upload form is working perfectly on Safari and FF but it fails to work on IE. The image just does not get uploaded.
It is really annoying and I cannot figure it out. I have tried searching for an answer on google but couldn't find anything. I know i am not the only one since there were other people who had the same problem but I couldn't find any solutions. I read something about how instead of "enctype" there should be "encoding" for IE but i don't know how to integrate it. I would Really appreciate your help.

Thank you in advance
  • 0

#35 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 21 August 2009 - 10:13 AM

Hey ssSuave,
Sure, I'll be glad to help. What version of IE are you using and did you change the code in any way? If so, can you post it?
  • 0

#36 phillw

phillw

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 23 August 2009 - 03:03 AM

Thanks for the tutorial, between yourself and Jaan - it's just what I need :thumbup:

Regards,

Phill.
  • 0





Also tagged with one or more of these keywords: max_file_size, mysql, images, php