www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: I need help creating an unique file name

  1. #1
    Join Date
    Mar 2010
    Posts
    701

    I need help creating an unique file name

    PHP Code:
    $temp_image_name $_FILES$field ][ 'name' ];
    $num 1;
    while (
    1) {
          if (
    file_exists("http://linksku.com/wp-content/upload/" $temp_image_name)) {
          if (
    $num == 1) {
          
    preg_match('/\.[a-zA-Z]{3,4}$/',$temp_image_name,$match);
          
    $extension $match[0];
          }
          
    $temp_image_name preg_replace('/\.[a-zA-Z]{3,4}$/','',$temp_image_name);
          if (
    $num 1)
          
    $regex '/'.$num.'$/';
          if (
    $num 1)
          
    $temp_image_name preg_replace($regex,'',$temp_image_name);
          
    $num $num 1;
          
    $temp_image_name $temp_image_name.$num;
          } else {
          if (
    $num!=1)
          
    $temp_image_name $temp_image_name.$extension;
          break;
          }

    I want this to create an unique file name, but this isn't working. I know there are much more efficient ways of writing this.

  2. #2
    Join Date
    Mar 2010
    Posts
    701
    P.S. The filenames are incrementing.

  3. #3
    Join Date
    Apr 2008
    Location
    Mumbai, India
    Posts
    255
    Try this...

    PHP Code:
    $temp_image_name $_FILES$field ][ 'name' ]; 
    $num 1
    while (
    $num) { 
          if (
    file_exists("http://linksku.com/wp-content/upload/" $temp_image_name)) { 
          if (
    $num == 1) { 
          
    preg_match('/\.[a-zA-Z]{3,4}$/',$temp_image_name,$match); 
          
    $extension $match[0]; 
          } 
          
    $temp_image_name preg_replace('/\.[a-zA-Z]{3,4}$/','',$temp_image_name); 
          if (
    $num 1
          
    $regex '/'.$num.'$/'
          if (
    $num 1
          
    $temp_image_name preg_replace($regex,'',$temp_image_name); 
          
    $num $num 1
          
    $temp_image_name $temp_image_name.$num
          } else { 
          if (
    $num!=1
          
    $temp_image_name $temp_image_name.$extension
          break; 
          }
          
    $num++;

    Hope this helps..

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Or you could use uniqid.
    The first rule of Tautology Club is the first rule of Tautology Club.

  5. #5
    Join Date
    Mar 2010
    Posts
    701
    PHP Code:
    $num 2
    while (
    1) { 
          if (
    file_exists("../wp-content/uploads/wp-post-image/" $temp_image_name)) {
          if (
    $num == 2) { 
          
    preg_match('/\.[a-zA-Z]{3,4}$/',$temp_image_name,$match); 
          
    $extension $match[0]; 
          } 
          
    $temp_image_name preg_replace('/\.[a-zA-Z]{3,4}$/','',$temp_image_name); 
          if (
    $num 2
          
    $regex '/'.$num.'$/'
          if (
    $num 2
          
    $temp_image_name preg_replace($regex,'',$temp_image_name); 
          
    $temp_image_name $temp_image_name.$num
          } else {
          if (
    $num 2
          
    $temp_image_name $temp_image_name.$extension
          break; 
          }
          
    $num++;

    I fixed it by making the path relative, since file_exists() returns false when the path is absolute and safe mode is on.

  6. #6
    Join Date
    Mar 2010
    Posts
    672
    You really need to learn and understand the difference between a url and a system absolute path. For one, it is quite possible it doesn't allow remote access to a file via url in safe mode... but also quite possible that it does for local files, while in safe mode.
    When you request a file via url, you're telling php that the file is remote, and so every time it will have to do a dns lookup for the domain, request the file, get a response, all of which is running throughout the network known as the internet. Compare this to providing the function, file_exists(), with a system absolute path where it just has to make a system call to the file system and get the results. For a test/procedure on any local file you want to use a local absolute (or relative if it fits the situation) path to it, as it is many times faster than remote access via url, not to mention many times procedures on remote files have limited permissions/access for security reasons.

  7. #7
    Join Date
    Mar 2010
    Posts
    701
    So I can use /public_html/forum/styles/prosilver/template/overall_header.html in a PHP include?

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    If that file really exists on the server (which it wont!). There is a difference between urls and filesystem paths as mentioned above. Include expects a filesystem path.
    The first rule of Tautology Club is the first rule of Tautology Club.

  9. #9
    Join Date
    Mar 2010
    Posts
    672
    Quote Originally Posted by narutodude000 View Post
    So I can use /public_html/forum/styles/prosilver/template/overall_header.html in a PHP include?
    Yes, that would correspond to:
    http://linksku.com/forum/styles/pros...ll_header.html

    Though normally, the entire absolute path is something like:
    /home/username(whateverthismaybe)/public_html/forum/styles/prosilver/template/overall_header.html (i'm assuming a linux/unix type server).

    An absolute file system path is a combo of two things, the document root path and the path to the file from that root. In the above example, the /home/username/public_html/ is the doc root. To get the complete path of the document root you can generally echo out the value of $_SERVER['DOCUMENT_ROOT'], or use it to make an absolute path. Though, as stated previously, a relative path is just fine too, the main thing is just to understand is that using a url makes php treat it as a remote file (and if you try to use a url for a php file you'll only get the output of the php file code, not the code of the file itself). Using a url for a file operation will still work for items like images, like you were trying to do earlier, but by using a url you have to go through many different systems to get the final result. This could include many servers and firewalls, all to get the same result that a local file system request would get in a near instant.
    However, since a php include requires a php file, it wouldn't work for that particular .html file you're requesting in the above code. For that html file you'd need to use something like file_get_contents() or file().
    Last edited by Jarrod1937; 07-21-2010 at 06:55 PM.

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    Not true in most cases. In PHP, the include statement takes either a relative path or an absolute one. The example provided used an absolute path (on *nix systems) that (more than likely) does not exist. A "/" at the beginning (on a *nix system) makes PHP start at the root of the file system.

  11. #11
    Join Date
    Mar 2010
    Posts
    701
    Quote Originally Posted by Jarrod1937 View Post
    However, since a php include requires a php file, it wouldn't work for that particular .html file you're requesting in the above code. For that html file you'd need to use something like file_get_contents() or file().
    My bad, I was editing that file when I used it as an example, and I forgot that it wasn't PHP . Thanks for your explanation, the only part where I'm still confused is, how does the PHP processor distinguish the difference between a relative URL and a relative filesystem path? They're both written the same way, right (correct me from wrong).

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    However, since a php include requires a php file, it wouldn't work for that particular .html file you're requesting in the above code. For that html file you'd need to use something like file_get_contents() or file().
    This is also not true. file_get_contents() or file() will read the contents of a file to a variable. include will (as on php.net):
    The include() statement includes and evaluates the specified file.

  13. #13
    Join Date
    Mar 2010
    Posts
    672
    Quote Originally Posted by criterion9 View Post
    This is also not true. file_get_contents() or file() will read the contents of a file to a variable. include will (as on php.net):
    Yes, sorry you're correct, i was thinking of the include causing the include text to be parsed, but you can include any file and it will be included just like any other server side type of include.

  14. #14
    Join Date
    Mar 2010
    Posts
    672
    Quote Originally Posted by narutodude000 View Post
    My bad, I was editing that file when I used it as an example, and I forgot that it wasn't PHP . Thanks for your explanation, the only part where I'm still confused is, how does the PHP processor distinguish the difference between a relative URL and a relative filesystem path? They're both written the same way, right (correct me from wrong).
    Not sure what you mean by relative url in this case. A web url is treated as that, a web url, so it treats any file located by a url as a remote file. However, a file system path (relative or absolute) does not begin with a protocol specification like http, and treats any file referenced as such as local. The difference between relative and absolute file system paths is less important, both work perfectly fine for just about any situation. However, it may be preferred to an absolute paths for certain operations.

  15. #15
    Join Date
    Mar 2010
    Posts
    672
    Quote Originally Posted by criterion9 View Post
    Not true in most cases. In PHP, the include statement takes either a relative path or an absolute one. The example provided used an absolute path (on *nix systems) that (more than likely) does not exist. A "/" at the beginning (on a *nix system) makes PHP start at the root of the file system.
    Yes, sorry if that was not clear, that is why i proceeded to say "Though normally, the entire absolute path is something like:
    /home/username(whateverthismaybe)/public_html/forum/styles/prosilver/template/overall_header.html", it was sort of my way of indirectly correcting what he said. It indeed would be very unlikely he'd have a file path starting from root of /public_html/...etc.
    Last edited by Jarrod1937; 07-21-2010 at 07:55 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles