SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    rename upload file (sorting)

    Hi


    I have directory and it's files like this(

    1.doc
    2.doc
    3.doc
    )

    I want when upload new file to this directory change name to be (lastno+1.doc)
    if last doc like example(3.doc) when upload new one rename to be(4.doc)

    any help??

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi altarek,

    Try this:
    PHP Code:
    $count 1;
    while (
    file_exists($targetDir DIRECTORY_SEPARATOR $count '.doc'))
        
    $count++;

    $fileName $count '.doc'
    It basically loops checking if a file exists called 1.doc, 2.doc etc to find the next available filename.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but if i have one file (20.doc) by this way the new file name will be(1.doc)

    I want it to be (21.doc)


    ??

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by altarek View Post
    but if i have one file (20.doc) by this way the new file name will be(1.doc)

    I want it to be (21.doc)
    OK, in that case try this:
    PHP Code:
    $last_used_number 0;
    $files scandir('folder/');

    foreach(
    $files as $file) {
      
    $info pathinfo($file);
      if (
    is_numeric($info['filename']) AND $info['filename'] > $last_used_number) {
        
    $last_used_number $info['filename'];
      }
    }

    $file_name = ($last_used_number 1) . '.doc'

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Or simplier:
    PHP Code:
    $next countglob'directory/to/files/*.doc' ) ) + 1;
    print 
    $next '.doc'
    Edilt, looks like the above link is that samething...oh well. Didn't read the link until now.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    logic_earth, that's a nice solution to counting the number of files in a directory. The problem is if you had just one file named 20.doc, this method would give you 2.doc rather than 21.doc as altarek says he wants.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adapt as necessary...

    Code PHP:
    <?php
    $files = glob('*.doc');
    $c= count($files);
    $i = 0;
    while($i<$c) {
    	$temp_array  = explode(".", $files[$i]);
    	$temp_values[$i] = $temp_array[0];
    	$i ++;
    }
    sort($temp_values);
    $last = $c - 1;
    $next = $temp_values[$last] + 1;
    $file_name = $next . ".doc";
    echo $file_name;
    ?>

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    How would that code I posted give you 2 instead of 21? That makes no logical sense. We are pulling in all files that match the pattern and counting them. If their is 20 files we will get 21 within "$next" as it should be.

    Please, elaborate how you got 2 when adding one to 20.

    You need to examine my code more closely. You should see I am using "count() + 1" directly. If there are 20 files, count() will be 20, what happens then? We will get 21 from the plus one.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by fretburner View Post
    The problem is if you had just one file named 20.doc, this method would give you 2.doc rather than 21.doc as altarek says he wants.
    Your method counts the number of files in the directory, rather than finding the next unused number in the sequence.. I'm not sure when you would need it to work like this, but altarek presumably knows what he wants. Perhaps files 1.doc to 19.doc get periodically moved to a different location, who knows?


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
  •