SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    slight problem with naming files in php gallery

    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

  2. #2
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is this line:
    PHP Code:
    $ext strrchr($imgName"."); 
    Change it to:
    PHP Code:
    $ext pathinfo($imgNamePATHINFO_EXTENSION); 
    The problem lies in that the returned string of strrchr() includes $needle (the first argument). pathinfo() is a function specifically made to retrieve information about a path.

    Edit: As a matter of fact, you could also change
    PHP Code:
    $newName strtolower(md5(rand() * time()) . ".$ext"); 
    to
    PHP Code:
    $newName strtolower(md5(rand() * time()) . $ext); 
    instead. Do whatever you want

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While your method of avoiding filename collisions is not likely to fail, eventually it will.

    A better method would be to use tempnam() or just name the file after the primary key in the database, because that is guaranteed to be unique. For example, an auto increment integer.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey guys,
    cheers for your help! now sorted out the problem with the full stop. still having problems with Firefox showing the headers of the document at completely random times but other than that is good.

    crmalibu, you are definitely right. i will look into a better system as soon as I get my gallery working right.

    thanks!
    russ


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •