Madatan Posted August 26, 2006 Share Posted August 26, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/ Share on other sites More sharing options...
AdRock Posted August 26, 2006 Share Posted August 26, 2006 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.jpgHow i would display it is like this<img src=/images/".$code['photo'];"> Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-80883 Share on other sites More sharing options...
Madatan Posted August 26, 2006 Author Share Posted August 26, 2006 yes, that is one way, I will try it and see if it suffice for my purpose. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-80884 Share on other sites More sharing options...
shocker-z Posted August 26, 2006 Share Posted August 26, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-80887 Share on other sites More sharing options...
Madatan Posted August 26, 2006 Author Share Posted August 26, 2006 Thats great! One question however. How do I show the pictures? I guess echo is the wrong command? Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-80892 Share on other sites More sharing options...
Madatan Posted August 26, 2006 Author Share Posted August 26, 2006 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-80919 Share on other sites More sharing options...
shocker-z Posted August 27, 2006 Share Posted August 27, 2006 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]<?phpif(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]<?phpinclude("library/opendb.php");// Place the code to connect your Database here// DATABASE CONNECTION$id=$_GET['id'];// Check if ID existsif(!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 databaseif(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 thumbnailImageCopyResized($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));// show thumbnail on screen$out = ImagejpeG($thumb);print($out);// clean memoryimagedestroy ($im);imagedestroy ($thumb);}?>[/code]and i call this in the same way..This is a straight paste out of my code so may need modifyingRegardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-81056 Share on other sites More sharing options...
Madatan Posted August 27, 2006 Author Share Posted August 27, 2006 Excellent, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-81062 Share on other sites More sharing options...
Madatan Posted August 27, 2006 Author Share Posted August 27, 2006 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]<?phpinclude '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] Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-81279 Share on other sites More sharing options...
litebearer Posted August 27, 2006 Share Posted August 27, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/18752-pictures-in-database/#findComment-81341 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.