Page 1 of 1

slight problem with naming files in php gallery

Posted: Tue Aug 05, 2008 1:28 pm
by slaterino
Hi,
Hopefully there's a quick answer to this issue. The filenames that I'm currently creating for the album images in my gallery are ending with an extra full stop. For example, the name could X78GUB832..jpg. I'm currently having some problems with my gallery in Firefox and want to rule this out as a problem. Below is my code for the page that this is on. Can anyone tell me why the extra full stop is being produced?

/////

<?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 = strtolower(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;
}
?>

/////

Thanks!

Russ

Re: slight problem with naming files in php gallery

Posted: Tue Aug 05, 2008 1:56 pm
by nowaydown1

Code: Select all

 
$ext = strrchr($imgName, ".");
$newName = strtolower(md5(rand() * time()) . ".$ext");
 
I believe that strrchr will return both the starting character and the contents after it. So, if that's true, $ext is actually ".jpg" or something similar. Then, you can see you are including a dot where you append the file extension to the randomly generated filename.

So if you wanted to fix it, remove the extra dot:

Code: Select all

 
$newName = strtolower(md5(rand() * time()) . $ext);