Jump to content


Photo

Pictures in database


  • Please log in to reply
9 replies to this topic

#1 Madatan

Madatan
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 26 August 2006 - 09:28 PM

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?
insert into bla_bla (picid, name) VALUES('','???');



#2 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 August 2006 - 09:34 PM

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'];">
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#3 Madatan

Madatan
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 26 August 2006 - 09:36 PM

yes, that is one way, I will try it and see if it suffice for my purpose. Thanks

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 09:42 PM

i use this 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>


does the trick for me :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 Madatan

Madatan
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 26 August 2006 - 09:54 PM

Thats great! One question however. How do I show the pictures? I guess echo is the wrong command?

#6 Madatan

Madatan
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 26 August 2006 - 10:46 PM

Anyone?

#7 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 27 August 2006 - 09:13 AM

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


<?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>


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

<?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);
}
?>

and i call this in the same way..

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

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#8 Madatan

Madatan
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 27 August 2006 - 10:19 AM

Excellent, thank you!

#9 Madatan

Madatan
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 27 August 2006 - 09:37 PM

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?

<?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);

?>


#10 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 27 August 2006 - 11:14 PM

Pehaps a few minutes reading this easy to follow tutorial may help resolve all of your issues.

http://www.phpriot.c...mages-in-mysql/

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users