Click to See Complete Forum and Search --> : [RESOLVED] move_uploaded_file() issue


davidjmorin
January 2nd, 2008, 10:29 AM
Hey its me again. i have an issue. i get the following error when i try to upload a pic. can someone help? i have set the permissions for the album folder and the gallery folder as well as thumb folder. i know it someting easy. does the image and gallery dir have to be in http://blahblah.com form? or /www/album/thumb/ etc. form?

Warning: move_uploaded_file(/images/gallery/23be8201681ab5510ea9dc86e8c16293.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/portlao9/public_html/image-gallery/image-gallery/library/functions.php on line 21

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpbkxTy5' to '/images/gallery/23be8201681ab5510ea9dc86e8c16293.jpg' in /home/portlao9/public_html/image-gallery/image-gallery/library/functions.php on line 21
Error uploading file

here is my config file:
<?php
session_start();

// db properties
$dbhost = 'localhost';
$dbuser = '';
$dbpass = ';
$dbname = '';

// an album can have an image used as thumbnail
// we save the album image here
define('ALBUM_IMG_DIR', '/images/album');

// all images inside an album are stored here
define('GALLERY_IMG_DIR', '/images/gallery/');

// When we upload an image the thumbnail is created on the fly
// here we set the thumbnail width in pixel. The height will
// be adjusted proportionally
define('THUMBNAIL_WIDTH', 100);

// make a connection to mysql here
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error());
?>


functions.php

<?php
/*
Upload an image and create the thumbnail. The thumbnail is stored
under the thumbnail sub-directory of $uploadDir.

Return the uploaded image name and the thumbnail also.
*/
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;

}

/*
Check if the user is logged in or not
*/
function checkLogin()
{
if (!isset($_SESSION['isLogin']) || $_SESSION['isLogin'] == false) {
header('Location: login.php');
exit;
}
}

/*
Create the link for moving from one page to another
*/
function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
{
$pagingLink = '';
$totalPages = ceil($totalResults / $itemPerPage);

// how many link pages to show
$numLinks = 10;

// create the paging links only if we have more than one page of results
if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;

// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> ";
} else {
$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
}

$first = " <a href=\"$self?$strGet\">[First]</a> ";
} else {
$prev = ''; // we're on page one, don't show 'previous' link
$first = ''; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> ";
$last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
} else {
$next = ''; // we're on the last page, don't show 'next' link
$last = ''; // nor 'last page' link
}

$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;

$end = min($totalPages, $end);

$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page "; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
} else {
$pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
}
}

}

$pagingLink = implode(' | ', $pagingLink);

// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}

return $pagingLink;
}

/*
Display the breadcrumb navigation on top of the gallery page
*/
function showBreadcrumb()
{
if (isset($_GET['album'])) {
$album = $_GET['album'];
$sql = "SELECT al_name
FROM tbl_album
WHERE al_id = $album";

$result = mysql_query($sql) or die('Error, get album name failed. ' . mysql_error());
$row = mysql_fetch_assoc($result);
echo ' &gt; <a href="index.php?page=list-image&album=' . $album . '">' . $row['al_name'] . '</a>';

if (isset($_GET['image'])) {
$image = $_GET['image'];
$sql = "SELECT im_title
FROM tbl_image
WHERE im_id = $image";

$result = mysql_query($sql) or die('Error, get image name failed. ' . mysql_error());
$row = mysql_fetch_assoc($result);

echo ' &gt; <a href="index.php?page=image-detail&album=' . $album . '&image=' . $image . '">' . $row['im_title'] . '</a>';
}
}
}

?>


ive hightlighted in red the stuff im not sure about.

davidjmorin
January 2nd, 2008, 10:33 AM
oh one other thing you should knw is this is the image gallery from php-mysql-tutorials.com

PeejAvery
January 2nd, 2008, 12:35 PM
You are correct, it is a simple issue. However, because we cannot see your file system, we can only point you in the right direction.

The errors in the PHP are because it is not finding the path. Make sure that the folders /images/album and /images/gallery have been created. And that they are the correct paths.

I would suggest using the PHP function echo to output the attempted path. Then you can compare it with the actual file system to see if it exists.

davidjmorin
January 2nd, 2008, 01:36 PM
yup the folders are there and they have been chmod to 777
should i instead use it like this

/gallery?

PeejAvery
January 2nd, 2008, 02:34 PM
As I stated before, you should echo the output and then double check the actual file system.

Does the following return an actual valid path on your server?

echo $uploadDir . $imagePath;

davidjmorin
January 2nd, 2008, 03:25 PM
ok well im lost with this. Thanks anyway

PeejAvery
January 2nd, 2008, 03:33 PM
Your move_uploaded_file() function call moves the file $image['tmp_name'] to $uploadDir . $imagePath. Below that line, echo the $uploadDir . $imagePath as I have already showed you. Then compare that path with the actual file system.

davidjmorin
January 2nd, 2008, 03:59 PM
ohhhh got ya...hha brain fart

davidjmorin
January 2nd, 2008, 04:02 PM
like this? its in red.


<?php
/*
Upload an image and create the thumbnail. The thumbnail is stored
under the thumbnail sub-directory of $uploadDir.

Return the uploaded image name and the thumbnail also.
*/
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);

echo ($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;

}

/*
Check if the user is logged in or not
*/
function checkLogin()
{
if (!isset($_SESSION['isLogin']) || $_SESSION['isLogin'] == false) {
header('Location: login.php');
exit;
}
}

/*
Create the link for moving from one page to another
*/
function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
{
$pagingLink = '';
$totalPages = ceil($totalResults / $itemPerPage);

// how many link pages to show
$numLinks = 10;

// create the paging links only if we have more than one page of results
if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;

// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> ";
} else {
$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
}

$first = " <a href=\"$self?$strGet\">[First]</a> ";
} else {
$prev = ''; // we're on page one, don't show 'previous' link
$first = ''; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> ";
$last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
} else {
$next = ''; // we're on the last page, don't show 'next' link
$last = ''; // nor 'last page' link
}

$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;

$end = min($totalPages, $end);

$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page "; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
} else {
$pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
}
}

}

$pagingLink = implode(' | ', $pagingLink);

// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}

return $pagingLink;
}

/*
Display the breadcrumb navigation on top of the gallery page
*/
function showBreadcrumb()
{
if (isset($_GET['album'])) {
$album = $_GET['album'];
$sql = "SELECT al_name
FROM tbl_album
WHERE al_id = $album";

$result = mysql_query($sql) or die('Error, get album name failed. ' . mysql_error());
$row = mysql_fetch_assoc($result);
echo ' &gt; <a href="index.php?page=list-image&album=' . $album . '">' . $row['al_name'] . '</a>';

if (isset($_GET['image'])) {
$image = $_GET['image'];
$sql = "SELECT im_title
FROM tbl_image
WHERE im_id = $image";

$result = mysql_query($sql) or die('Error, get image name failed. ' . mysql_error());
$row = mysql_fetch_assoc($result);

echo ' &gt; <a href="index.php?page=image-detail&album=' . $album . '&image=' . $image . '">' . $row['im_title'] . '</a>';
}
}
}

?>
or this?

echo $uploadDir . $imagePath;

PeejAvery
January 2nd, 2008, 04:16 PM
The second as already supplied. ;)

davidjmorin
January 2nd, 2008, 04:21 PM
Thanks a bunch!!!