slight problem with naming files in php gallery

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

slight problem with naming files in php gallery

Post 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
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: slight problem with naming files in php gallery

Post 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);
 
Post Reply