Problem with Accessing Pictures in PHP Gallery
Posted: Sun Jul 13, 2008 6:04 pm
Hi,
I've created an image gallery where photos are uploaded to my mySql database. A thumbnail is automatically created and they're both stored with randomly-generated names. However, I'm having some problems viewing these pictures once they're on my site. This is some of the coding that I've used. These specify the directories where the images are saved:
<?php
define('ALBUM_IMG_DIR', 'C:/webroot/gallery/images/album/');
define('GALLERY_IMG_DIR', 'C:/webroot/gallery/images/gallery/');
?>
This is the code that's used to upload the images
<?php
function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';
if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1);
$imagePath = md5(rand() * time()) . ".$ext";
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
if ($result) {
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
$imagePath = $thumbnailPath = '';
}
}
return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
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);
} elseif ($tmpDest['extension'] == "png") {
$dest = imagecreatetruecolor($w, $h);
} 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;
}
?>
This is the code used on the page where images can be uploaded as the album cover:
<?php
require_once '../library/config.php';
require_once '../library/functions.php';
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;
$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='index.php?page=list-album';</script>";
exit;
}
?>
And finally, this is the code used to view the images:
<?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 {
exit;
}
header("Content-length: " . filesize($filePath));
header("Content-type: image/" . substr($name, strpos($name, '.') + 1));
readfile($filePath);
?>
The images are not appearing at all in the gallery. This error message is appearing right after I choose an image to be uploaded:
Warning: move_uploaded_file(C:/webroot/gallery/images/gallery/86faf39a2a8edb50e90236bf14053c4b.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/sites/thedaffodilsociety.com/public_html/gallery/library/functions.php on line 21
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpeHb4pH' to 'C:/webroot/gallery/images/gallery/86faf39a2a8edb50e90236bf14053c4b.JPG' in /home/sites/thedaffodilsociety.com/public_html/gallery/library/functions.php on line 21
Error uploading file
Does anyone know why this error could be occurring? Is it possibly because JPG is written in capitals and not lowercase? Could this cause problems and how could I fix it? The images are being saved in the database and the filenames all correspond so I really think it must be a problem with how I am linking to the images, but really don't know where to start!! If anyone can have a look at any of this code and maybe suggest why the gallery might not be working that would be amazing!!
Thanks
Russ
I've created an image gallery where photos are uploaded to my mySql database. A thumbnail is automatically created and they're both stored with randomly-generated names. However, I'm having some problems viewing these pictures once they're on my site. This is some of the coding that I've used. These specify the directories where the images are saved:
<?php
define('ALBUM_IMG_DIR', 'C:/webroot/gallery/images/album/');
define('GALLERY_IMG_DIR', 'C:/webroot/gallery/images/gallery/');
?>
This is the code that's used to upload the images
<?php
function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';
if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1);
$imagePath = md5(rand() * time()) . ".$ext";
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
if ($result) {
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
$imagePath = $thumbnailPath = '';
}
}
return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
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);
} elseif ($tmpDest['extension'] == "png") {
$dest = imagecreatetruecolor($w, $h);
} 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;
}
?>
This is the code used on the page where images can be uploaded as the album cover:
<?php
require_once '../library/config.php';
require_once '../library/functions.php';
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;
$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='index.php?page=list-album';</script>";
exit;
}
?>
And finally, this is the code used to view the images:
<?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 {
exit;
}
header("Content-length: " . filesize($filePath));
header("Content-type: image/" . substr($name, strpos($name, '.') + 1));
readfile($filePath);
?>
The images are not appearing at all in the gallery. This error message is appearing right after I choose an image to be uploaded:
Warning: move_uploaded_file(C:/webroot/gallery/images/gallery/86faf39a2a8edb50e90236bf14053c4b.JPG) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/sites/thedaffodilsociety.com/public_html/gallery/library/functions.php on line 21
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpeHb4pH' to 'C:/webroot/gallery/images/gallery/86faf39a2a8edb50e90236bf14053c4b.JPG' in /home/sites/thedaffodilsociety.com/public_html/gallery/library/functions.php on line 21
Error uploading file
Does anyone know why this error could be occurring? Is it possibly because JPG is written in capitals and not lowercase? Could this cause problems and how could I fix it? The images are being saved in the database and the filenames all correspond so I really think it must be a problem with how I am linking to the images, but really don't know where to start!! If anyone can have a look at any of this code and maybe suggest why the gallery might not be working that would be amazing!!
Thanks
Russ