cjohnson Posted October 1, 2008 Share Posted October 1, 2008 Hi guys, I have a problem I can not figure out, so before I have a stroke...here it is. I am loading an image from a php form (PHP 5) to a MySQL database, Blob column (I hate doing this, but I don't have permission to write to a folder and can't set the folder permissions). I can upload the image. When I try to display the image, I get junk. When I try to download the image, the file is the correct size and type, but is unreadable. Any help would be greatly appreciated. Here is how I did it... Oh, and I also attached a before and after pic. Upload page... <?php # upload.php //This file is the site's file upload page. ob_start(); session_start(); require_once ('header.php'); if (isset($_POST['filesubmitted']) && isset($_FILES['userfile']) && isset($_SESSION['userid'])) { $userid = $_SESSION['userid']; $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $image = fread($fp, filesize($tmpName)); if(!get_magic_quotes_gpc()) {$image = addslashes($image);} fclose($fp); if(!get_magic_quotes_gpc()) {$fileName = addslashes($fileName);} global $dbc; require_once (MYSQL); $s = "INSERT INTO files (userid, image, name, type, size) VALUES ('$userid', '$image', '$fileName', '$fileType', '$fileSize')"; $t = mysqli_query ($dbc, $s) or trigger_error("Query: $s\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { echo '<script>alert("File Submitted")</script>'; } else { echo '<script>alert("File Not Submitted")</script>'; } } echo ' <fieldset><legend><span style="color:blue">Form Upload </span></legend> <center><p>In this section, you may upload scanned images of the required forms for this position.</p> <form method="post" enctype="multipart/form-data"> <b>Select file to upload:</b><br /> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input type="file" name="userfile" style="width:50%;" /><br /> <input type="submit" name="submit" value="Upload" style="width:50%;" /> <input type="hidden" name="filesubmitted" value="TRUE" /> </form> </center> </fieldset> '; ?> <?php require_once ('footer.php'); ?> View Page: <?php # view.php //This file is the site's file upload page. ob_start(); session_start(); require_once ('header.php'); if (isset($_POST['viewfilesubmitted'])) { $fileid = $_POST['fileid']; global $dbc; require_once (MYSQL); $s = "SELECT * FROM files WHERE fileid='$fileid'"; $t = mysqli_query ($dbc, $s) or trigger_error("Query: $s\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { $u = mysqli_fetch_array ($t, MYSQLI_ASSOC); $size = $u['size']; $type = $u['type']; $name = $u['name']; $image = $u['image']; header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $image; exit; } else { echo 'Error: File could not be retrieved.'; } } ?> <?php require_once ('footer.php'); ?> [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/126647-blob-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 You probably should read how to output images on a web page (requires an <img tag for each image.) - http://www.w3schools.com/html/html_images.asp If you examine the second file you attached to the post using a programming editor, you will see why it is not a valid image. And if you are attempting to provide a download link, you need to provide a link on your web page that when clicked, causes the browser to request a URL that outputs the headers and the content of the image field. You cannot output image data in the middle of a web page, nor can you output the download headers and the image data in the middle of a web page. Link to comment https://forums.phpfreaks.com/topic/126647-blob-help/#findComment-654937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.