Jump to content

Archived

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

Madatan

Pictures in database

Recommended Posts

Okey, I simply wanna know if this is possible and if so then how.

What I wanna do is being able to upload pictures and store them in a database, is this possible with mysql? And how would such a thing look?

Would you use this kind of code?
[code]
insert into bla_bla (picid, name) VALUES('','???');

[/code]

Share this post


Link to post
Share on other sites
You can upload images directly into a database but what i do is upload the images to a folder and in the database insert the name of the image....so i would insert into the field picname....something.jpg

How i would display it is like this

<img src=/images/".$code['photo'];">

Share this post


Link to post
Share on other sites
yes, that is one way, I will try it and see if it suffice for my purpose. Thanks

Share this post


Link to post
Share on other sites
i use this code

[code]
if(isset($_POST['upload'])) {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
      $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
      $fp = fopen($tmpName, 'r');
      $content = fread($fp, $fileSize);
      $content = addslashes($content);
      fclose($fp);
       
      if(!get_magic_quotes_gpc()) {
          $fileName = addslashes($fileName);
      }
      $query = "INSERT INTO images (name, size, type, content, username, description) ".
                "VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$session_username', '$_POST[description]')";
        mysql_query($query) or die('Error, query failed');                   
      include 'library/closedb.php'; 
      echo "<br>File $fileName uploaded<br>";
}       
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="350" border="0" cellpadding="1" cellspacing="1" class="textbox">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="textbox" id="userfile">      </td>
      <td width="246"></td>
      <td width="246"><input name="upload" type="submit" class="textbox" id="upload" value="  Upload  " /></td>

    </tr>
  </table>
</form>[/code]


does the trick for me :)

Share this post


Link to post
Share on other sites
Thats great! One question however. How do I show the pictures? I guess echo is the wrong command?

Share this post


Link to post
Share on other sites
I use this code as a seperate page to show the full file and then call it in <img src="view.php?id=1"> ot what ever the ID of the image is in the DB


[code]<?php
if(isset($_GET['id']))
{
    include 'library/config.php';
    include 'library/opendb.php';

    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, content FROM images WHERE id = '$id'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;

    include 'library/closedb.php';     
    exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'library/config.php';
include 'library/opendb.php';

$query  = "SELECT id, name FROM images";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";

else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
<img src="view.php?id=<?=$id;?>"><br><?=$name;?></a> <a href="delete.php?id=<?=$id;?>">Delete</a><br>
<?         
    }
}
include 'library/closedb.php';
?>
</body>
</html> [/code]


I also use a thumbnail one that i call thumbs.php

[code]<?php
include("library/opendb.php");
// Place the code to connect your Database here
// DATABASE CONNECTION

$id=$_GET['id'];

// Check if ID exists
if(!is_numeric($id)) die("No image with the ID: ".$id);

// Get data from database
$dbQuery = "SELECT content, type ";
$dbQuery .= "FROM images ";
$dbQuery .= "WHERE ID = $id ";
$dbQuery .= "LIMIT 1";

$result = mysql_query($dbQuery);

// read imagetype + -data from database
if(mysql_num_rows($result) == 1) {
$fileType = mysql_result($result, 0, "type");
$fileContent = mysql_result($result, 0, "content");

header("Content-type: $fileType");

// get originalsize of image
$im = imagecreatefromstring($fileContent);
$width = imagesx($im);
$height = imagesy($im);

// Set thumbnail-width to 100 pixel
$imgw = 120;

// calculate thumbnail-height from given width to maintain aspect ratio
$imgh = $height / $width * $imgw;

// create new image using thumbnail-size
$thumb=ImageCreateTrueColor($imgw,$imgh);

// copy original image to thumbnail
ImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));

// show thumbnail on screen
$out = ImagejpeG($thumb);
print($out);

// clean memory
imagedestroy ($im);
imagedestroy ($thumb);
}
?>[/code]

and i call this in the same way..

This is a straight paste out of my code so may need modifying

Regards
Liam

Share this post


Link to post
Share on other sites
Could not get that to work at all, so I searched a little at php.net after and found out a thing called imagefromstring. I tried to use some of the examples and modified them the way it should be. Still no luck getting my pictures to show from the database... could anyone look at this and tell me what i'm doing wrong?

[code]
<?php
include 'databas.php'
$query = "SELECT * FROM picsandchicks";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$fid = $row['fid']; //Pictures ID
$namn = $row['namn']; //Just the name of the pic
$content = $row['content']; //The picture in a string way looks something like this: GIF89a ô÷....
$type = $row['typ']; //Image type, GIF in this example.
$size = $row['storlek']; //Size..,



header('Content-Type: image/gif');

$im = imagecreatefromstring($content);
imagegif($im);

?>
[/code]

Share this post


Link to post
Share on other sites
Pehaps a few minutes reading this easy to follow tutorial may help resolve all of your issues.

http://www.phpriot.com/d/articles/database/images-in-mysql/

Lite...

Share this post


Link to post
Share on other sites

×

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.