squizz Posted May 22, 2008 Share Posted May 22, 2008 im having trouble viewing images from the database <?php if (!isset($_GET['type']) || !isset($_GET['name'])) { exit; } $type = $_GET['type']; $name = $_GET['name']; include 'library/config.php'; if ($type == 'album') { $filePath = ALBUM_IMG_DIR . $name; } else if ($type == 'glimage') { $filePath = GALLERY_IMG_DIR . $name; } else if ($type == 'glthumbnail') { $filePath = GALLERY_IMG_DIR . 'thumbnail/' . $name; } else { // invalid image type exit; } header("Content-length: " . filesize($filePath)); header("Content-type: image/" . substr($name, strpos($name, '.') + 1)); readfile($filePath); ?> Quote Link to comment Share on other sites More sharing options...
947740 Posted May 22, 2008 Share Posted May 22, 2008 So...you want to display the image on the website? Just include this code: echo "<img src='$filePath' />"; Quote Link to comment Share on other sites More sharing options...
squizz Posted May 22, 2008 Author Share Posted May 22, 2008 im trying to view it in an album... i followed a tutorial and it says its uploading and viewing but i get this... with this error from safari Warning: filesize() [function.filesize]: Stat failed for /images/album/578732c0667c44c19e489f94f0a3c57b.jpg (errno=2 - No such file or directory) in /viewImage.php on line 31 Warning: Cannot modify header information - headers already sent by (output started at /viewImage.php:31) in /viewImage.php on line 31 Warning: Cannot modify header information - headers already sent by (output started at /viewImage.php:31) in /viewImage.php on line 34 Warning: readfile(/images/album/578732c0667c44c19e489f94f0a3c57b.jpg) [function.readfile]: failed to open stream: No such file or directory in /viewImage.php on line 37 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 22, 2008 Share Posted May 22, 2008 Well, the original error is generated here: header("Content-length: " . filesize($filePath)); It would seem the file contained in $filePath does not exist. Have you checked the relevant folder? The other errors are caused by the same problem. On a side note, you'll be sending an incorrect MIME type by the looks of things. A image's extension is not necessarily the same as it's MIME type. A .jpg should have the type image/jpeg. Not that's a major thing, i 'spect most browsers wouldn't have a problem Quote Link to comment Share on other sites More sharing options...
squizz Posted May 22, 2008 Author Share Posted May 22, 2008 $filePath is defined in the config.php with define('ALBUM_IMG_DIR', 'images/album/'); define('GALLERY_IMG_DIR', 'images/gallery/'); define('THUMBNAIL_WIDTH', 100); i have these file set on my server had a look the upload isn't going to the files (they're empty) if(isset($_POST['txtName'])) { $albumName = $_POST['txtName']; $albumDesc = $_POST['mtxDesc']; $imgName = $_FILES['fleImage']['name']; $tmpName = $_FILES['fleImage']['tmp_name']; $ext = strrchr($imgName, "."); $newName = md5(rand() * time()) . $ext; $imgPath = ALBUM_IMG_DIR . $newName; //it says its going there but there is no image in the file $result = createThumbnail($tmpName, $imgPath, THUMBNAIL_WIDTH); if (!$result) { echo "Error uploading file"; exit; } if (!get_magic_quotes_gpc()) { $albumName = addslashes($albumName); $albumDesc = addslashes($albumDesc); } $query = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date) VALUES ('$albumName', '$albumDesc', '$newName', NOW())"; mysql_query($query) or die('Error, add album failed : ' . mysql_error()); echo "<script>window.location.href='adbum.php?page=list-album';</script>"; exit; } Quote Link to comment Share on other sites More sharing options...
squizz Posted May 23, 2008 Author Share Posted May 23, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 23, 2008 Share Posted May 23, 2008 i have these file set on my server had a look the upload isn't going to the files (they're empty) What did you mean by that? Have we now narrowed the error down to the upload of the files? Quote Link to comment Share on other sites More sharing options...
squizz Posted May 23, 2008 Author Share Posted May 23, 2008 the paths have been defined and i got the files they should go to but they not seem to upload there. yet my database says they have been stored. Quote Link to comment Share on other sites More sharing options...
Dathremar Posted May 23, 2008 Share Posted May 23, 2008 You can't mix upload and insertion into db. I mean, I dont see that you have prevented the insert into database part of your code if the upload failed. I have come across similar problems. Usually it was a problem with access rights. Check what access rights you have for the folders where you wanna upload the files. And 2nd thing is to double check the path to the files. Hope I helped you in any way. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 23, 2008 Share Posted May 23, 2008 All of your upload appears to be handled by your createThumbnail() function. We'd need to see that. Quote Link to comment Share on other sites More sharing options...
squizz Posted May 28, 2008 Author Share Posted May 28, 2008 I know its a bit late of a reply but i been busy sorry i think this is the code function uploadImage($inputName, $uploadDir) { $image = $_FILES[$inputName]; $imagePath = ''; $thumbnailPath = ''; // if a file is given if (trim($image['tmp_name']) != '') { $ext = substr(strrchr($image['name'], "."), 1); // generate a random new file name to avoid name conflict // then save the image under the new file name $imagePath = md5(rand() * time()) . ".$ext"; $result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath); if ($result) { // create thumbnail $thumbnailPath = md5(rand() * time()) . ".$ext"; $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH); // create thumbnail failed, delete the image if (!$result) { unlink($uploadDir . $imagePath); $imagePath = $thumbnailPath = ''; } else { $thumbnailPath = $result; } } else { // the image cannot be uploaded $imagePath = $thumbnailPath = ''; } } return array('image' => $imagePath, 'thumbnail' => $thumbnailPath); } /* Create a thumbnail of $srcFile and save it to $destFile. The thumbnail will be $width pixels. */ function createThumbnail($srcFile, $destFile, $width, $quality = 75) { $thumbnail = ''; if (file_exists($srcFile) && isset($destFile)) { $size = getimagesize($srcFile); $w = number_format($width, 0, ',', ''); $h = number_format(($size[1] / $size[0]) * $width, 0, ',', ''); $thumbnail = copyImage($srcFile, $destFile, $w, $h, $quality); } // return the thumbnail file name on sucess or blank on fail return basename($thumbnail); } /* Copy an image to a destination file. The destination image size will be $w X $h pixels */ function copyImage($srcFile, $destFile, $w, $h, $quality = 75) { $tmpSrc = pathinfo(strtolower($srcFile)); $tmpDest = pathinfo(strtolower($destFile)); $size = getimagesize($srcFile); if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg") { $destFile = substr_replace($destFile, 'jpg', -3); $dest = imagecreatetruecolor($w, $h); //imageantialias($dest, TRUE); } elseif ($tmpDest['extension'] == "png") { $dest = imagecreatetruecolor($w, $h); //imageantialias($dest, TRUE); } else { return false; } switch($size[2]) { case 1: //GIF $src = imagecreatefromgif($srcFile); break; case 2: //JPEG $src = imagecreatefromjpeg($srcFile); break; case 3: //PNG $src = imagecreatefrompng($srcFile); break; default: return false; break; } imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); switch($size[2]) { case 1: case 2: imagejpeg($dest,$destFile, $quality); break; case 3: imagepng($dest,$destFile); } return $destFile; } Quote Link to comment Share on other sites More sharing options...
squizz Posted May 29, 2008 Author Share Posted May 29, 2008 *bump* Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 29, 2008 Share Posted May 29, 2008 Well, for starters, how about adding something which tells you if the upload failed? function uploadImage($inputName, $uploadDir) { $image = $_FILES[$inputName]; $imagePath = ''; $thumbnailPath = ''; // if a file is given if (trim($image['tmp_name']) != '') { $ext = substr(strrchr($image['name'], "."), 1); // generate a random new file name to avoid name conflict // then save the image under the new file name $imagePath = md5(rand() * time()) . ".$ext"; $result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath); if ($result) { // create thumbnail $thumbnailPath = md5(rand() * time()) . ".$ext"; $result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH); // create thumbnail failed, delete the image if (!$result) { unlink($uploadDir . $imagePath); $imagePath = $thumbnailPath = ''; echo 'Thumbnail could not be created<br />'; } else { $thumbnailPath = $result; } } else { // the image cannot be uploaded $imagePath = $thumbnailPath = ''; echo 'Image could not be created<br />'; } } return array('image' => $imagePath, 'thumbnail' => $thumbnailPath); } Quote Link to comment Share on other sites More sharing options...
squizz Posted May 29, 2008 Author Share Posted May 29, 2008 hmmm still uploading blank/broken images images no echo came up saying its couldnt upload it Quote Link to comment Share on other sites More sharing options...
squizz Posted May 29, 2008 Author Share Posted May 29, 2008 *bump* (again) Quote Link to comment Share on other sites More sharing options...
squizz Posted May 30, 2008 Author Share Posted May 30, 2008 *bump* please it problem is rateling my brain i dont know why its not loading the images Quote Link to comment 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.