kringshot Posted May 28, 2009 Share Posted May 28, 2009 I've made an image uploader that will store an image in a mysql database, and then I have a search page so that users can search for the images. But the results all return like: GIF89a�d������������f��3���������̙��f��3�������������f��3���f��f Any idea what I'm doing wrong? From search results page: <?php $filename = $_GET['filename']; //Displaying the files $sql = "SELECT filename, mimetype, user, filedata FROM picstore WHERE filename = '$filename'"; $result = @mysql_query($sql); if (!$result) { exit('Database error: ' . mysql_error()); } $file = mysql_fetch_array($result); if (!$file) { exit('File with given filename not found in database!'); } $filename = $file['filename']; $mimetype = $file['mimetype']; $filedata = $file['filedata']; header("content-disposition: inline; filename=$filename"); header("content-type: $mimetype"); header('content-length: ' . strlen($filedata)); echo $filedata; exit(); ?> Thanks =) Link to comment https://forums.phpfreaks.com/topic/160035-created-an-image-uploaderviewer-but-the-pictures-just-come-out-as-text/ Share on other sites More sharing options...
kringshot Posted May 28, 2009 Author Share Posted May 28, 2009 (Can you only modify a post once? Was just going to modify that first one to put this in...) I think I've solved it, I was just saving the information of the file in the database, the image has to be uploaded to the directory as well as the information being saved (right?) and then you use the database to find the image and display it from the directory? Even so i'm still getting errors =( If anyone could give a quick scout over my code and see where I am going wrong I would be much appreciated, currentyl I'm getting an error on line 79 which is the copy($_FILES['upload'] etc line. This is my upload form and script: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <p align="center"><label>Upload File:<input type="file" name="upload" /></label></p> <p align="center"><label>Description:<input type="text" name="desc" maxlength="255" /></label></p> <p align="center"><input type="submit" value="Upload" /></p> </form> <?php // Pick a file extension if (eregi('^image/p?jpeg(;.*)?$', $_FILES['upload']['type'])) { $extension = '.jpg'; } else { $extension = '.gif'; } // The complete path/filename $filename = 'C:/xampp/localhost/kraft/uploads/' . time() . //This i have tried as 'localhost/uploads and localhost/kraft/uploads and neither seems to fix anything... $_SERVER['REMOTE_ADDR'] . $extension; // Copy the file (if it is deemed safe) if (is_uploaded_file($_FILES['upload']['tmp_name']) and copy($_FILES['upload']['tmp_name'], $filename)) { echo "<p>File stored successfully as $filename.</p>"; } else { echo "<p>Could not save file as $filename!</p>"; } ?> <?php // If the file isn't an upload somehow, then this will exit if (!is_uploaded_file($_FILES['upload']['tmp_name'])) { exit(); } $uploadfile = $_FILES['upload']['tmp_name']; $uploadname = $_FILES['upload']['name']; $uploadtype = $_FILES['upload']['type']; $uploaddesc = $_POST['desc']; $uploaduser = $_SESSION['user']; $uploaddate = date('Y-m-d'); // Open file for binary reading $tempfile = fopen($uploadfile, 'rb'); // Read the entire file into memory using PHP's filesize function $filedata = fread($tempfile, filesize($uploadfile)); // Prepares it for datatbase entry $filedata = addslashes($filedata); // Create the SQL query and store in $sql $sql = "INSERT INTO picstore SET filename = '$uploadname', mimetype = '$uploadtype', description = '$uploaddesc', user = '$uploaduser', date = '$uploaddate', filedata = '$filedata'"; // Perform the insert. $ok = @mysql_query($sql); if (!$ok) { exit('Database error storing file: ' . mysql_error()); } ?> Link to comment https://forums.phpfreaks.com/topic/160035-created-an-image-uploaderviewer-but-the-pictures-just-come-out-as-text/#findComment-844250 Share on other sites More sharing options...
Axeia Posted May 28, 2009 Share Posted May 28, 2009 Without reading all the code.. If you store an image in a database you need to use the BLOB (Binary Large Object.. don't know what the last B stands for) datatype. And then you can echo it out after echo'ing out the appropriate header header('Content-Type: image/jpeg'); echo $databaseBlobValue; You can not echo it out in the middle of a page (unless you want to screw over all your internet explorer users not using IE8). To get around it out, create a page with something like that code above and get the info based on $_GET, so in search results you can do <img src="/imageEchoingScript.php?imageId" alt=""/> Of course you could make it yourself easy and NOT store the image themselves in the database, but the paths to them.. less likely to anger your host as well as storing everything in the database and fetching it via $_GET is a multitude more resource intensive than just fetching a bunch of links. Link to comment https://forums.phpfreaks.com/topic/160035-created-an-image-uploaderviewer-but-the-pictures-just-come-out-as-text/#findComment-844313 Share on other sites More sharing options...
kringshot Posted May 28, 2009 Author Share Posted May 28, 2009 Without reading all the code.. If you store an image in a database you need to use the BLOB (Binary Large Object.. don't know what the last B stands for) datatype. And then you can echo it out after echo'ing out the appropriate header header('Content-Type: image/jpeg'); echo $databaseBlobValue; You can not echo it out in the middle of a page (unless you want to screw over all your internet explorer users not using IE8). To get around it out, create a page with something like that code above and get the info based on $_GET, so in search results you can do <img src="/imageEchoingScript.php?imageId" alt=""/> Of course you could make it yourself easy and NOT store the image themselves in the database, but the paths to them.. less likely to anger your host as well as storing everything in the database and fetching it via $_GET is a multitude more resource intensive than just fetching a bunch of links. Ok I'm a little confused on what your saying here, so the best bet would be then to store the images in a directory and then run $_GETs on a database of filepaths that can then retrieve the image? This sounds more confusing, how would one go about doing this?? Link to comment https://forums.phpfreaks.com/topic/160035-created-an-image-uploaderviewer-but-the-pictures-just-come-out-as-text/#findComment-844471 Share on other sites More sharing options...
kringshot Posted May 29, 2009 Author Share Posted May 29, 2009 Still having issues, if anyone could share some ideas on the topic would be great Link to comment https://forums.phpfreaks.com/topic/160035-created-an-image-uploaderviewer-but-the-pictures-just-come-out-as-text/#findComment-844902 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.