SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    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,312
    Mentioned
    177 Post(s)
    Tagged
    8 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
    730
    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,312
    Mentioned
    177 Post(s)
    Tagged
    8 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
    4,807
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    The error in your code is because you have $n = "uniqid().$ext"; instead of $n = uniqid().".$ext";
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    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,075
    Mentioned
    53 Post(s)
    Tagged
    0 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
    730
    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,075
    Mentioned
    53 Post(s)
    Tagged
    0 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
    730
    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,075
    Mentioned
    53 Post(s)
    Tagged
    0 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
    730
    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
    4,807
    Mentioned
    141 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.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  14. #14
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 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
  •