SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Add uniqid() To String

    I am trying to uniq to a string so the following code has:

    Code:
    	$n = "$imagename.$ext";
    It creates a range and adds the file type such as: 19572185.png

    Can anyone advise how I add uniqid() to a string.

    Code:
    case 'image/jpeg': $ext = 'jpg'; break;
    case 'image/gif': $ext = 'gif'; break;
    case 'image/png': $ext = 'png'; break;
    case 'image/tiff': $ext = 'tif'; break;
    default: 		$ext = ''; break;
    }
    if ($ext)
    {
    	$n = "uniqid().$ext";
    	move_uploaded_file($_FILES['filename']['tmp_name'], $n);
    	echo "Upload image '$name' as '$n':<br />";
    }
    else echo "'$name' is not accepted image file";
    }
    else echo "No image has been uploaded";

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Howdy,
    Just to understand what you are trying to do:
    A user uploads a file.
    If it is of an accepted type, you want to generate a unique name and rename it.
    That correct?

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi mate,

    Its for an image upload, currently it only uploads one image and calls it image.png

    I am trying to make it so it uploads infinite images each with a unique number.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Could you not then generate a unique file name based on the user's IP address and the current time stamp?
    Something like:
    Code PHP:
    $n = time() . $_SERVER['REMOTE_ADDR'] . $ext;

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,154
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    The error in your code is because you have $n = "uniqid().$ext"; instead of $n = uniqid().".$ext";

  6. #6
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    The error in your code is because you have $n = "uniqid().$ext"; instead of $n = uniqid().".$ext";
    Brilliant, thanks everyone. That worked straight away.

  7. #7
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Just curious... If you try uploading a file type not in your case statement, does it error out saying nothing was uploaded, or does it try to go through the logic anyway? I ask because I thought that if($ext) check to see if it is set, while you would be setting an empty string to $ext.

  8. #8
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes I have this to only accept image files.


    Code:
    case 'image/jpeg': $ext = 'jpg'; break;
    case 'image/gif': $ext = 'gif'; break;
    case 'image/png': $ext = 'png'; break;
    case 'image/tiff': $ext = 'tif'; break;
    default: 		$ext = ''; break;
    }
    if ($ext)
    {
    	$n = uniqid().".$ext";
    	move_uploaded_file($_FILES['filename']['tmp_name'], $n);
    	echo "Upload image '$name' as '$n':<br />";
    }
    else echo "'$name' is not accepted image file";

  9. #9
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Right, but if I'm not mistaken, even this line will cause if($ext) to return true:

    Code PHP:
    default: $ext = ''; break;

  10. #10
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Not sure what you mean. When I try to upload a PHP file it rejects it.

  11. #11
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by justlukeyou View Post
    Hi,

    Not sure what you mean. When I try to upload a PHP file it rejects it.
    Alright, I'm mistaken then

  12. #12
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No thats fine, thanks for checking.

    When the code is up an running I need to through all the security holes so its good to discuss it.

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,154
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    Right, but if I'm not mistaken, even this line will cause if($ext) to return true:

    Code PHP:
    default: $ext = ''; break;
    It wouldn't. You can use this chart to see what PHP would interpret it as.

    Also this thread is a good read too.

  14. #14
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    ah thank you


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
  •