Jump to content

Veiwing images (database)


squizz

Recommended Posts

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

Link to comment
Share on other sites

im trying to view it in an album...

i followed a tutorial and it says its uploading and viewing but i get this...

 

error_icon.gif

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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;
}		

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.