upload file and rename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    upload file and rename

    ive got an upload file form and it can have as many fields as a user wants to add images but say the user accidently pushes submit and the images upload (they are renamed as 1.jpg, 2.jpg, 3.jpg etc etc) but the user still wants to upload more images.....is it possible for php to read a directory and check what the filenames are and then renamed all extra images that are uploaded starting from the next digit?

    so if there is already a 1.jpg and a 2.jpg once more pictures are uploaded it starts from 3.jpg and so forth....ive tried looking for something like this but dont know where to start.

    thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Here's something to get you started:

    readdir() - read to documentation!
    You'll probably also need to explode() the file name to get the name minus it's extension.

    Theory:
    1. Loop through the directory's files using readdir(),
    2. Determine whether you are at the end of the directory (I'll let you figure this out as a little brain-workout),
    3. find the number in the files name,
    4. increment the number to use as your next file name.


    - mark.

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #3
      ok thanx markus....once ive gotta tester ill come back for more advice......tha nks again

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by anfetienne
        ok thanx markus....once ive gotta tester ill come back for more advice......tha nks again
        No problem, anfetienne.

        - mark.

        Comment

        • anfetienne
          Contributor
          • Feb 2009
          • 424

          #5
          ok ive got to the point where i can open a directory and list all files in the directory and strip off the .jpg......i used a similar code to rename files and move them.....i haven't finished building it as I am restructure all my coding to suit this. but based on what you showed me the cide i need to use for starters is this

          Code:
          <?php
          if ($handle = opendir('.')) {
              while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                      echo "$file\n";
                  }
              }
              closedir($handle);
          }
          ?>
          from here i have no idea on how to get the php to detect what number the last uploaded image is on and then how to get that into a variable

          Comment

          • anfetienne
            Contributor
            • Feb 2009
            • 424

            #6
            my guess was to try get it into an array so its read to the last value and then add onto that but that attempt failed.....i was nowhere close to it lol

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Assuming your files are in the format x.jpg (x being a number), you can explode() the file name using '.jpg' as your needle.

              Code:
              $file = "12.jpg";
              
              $file = explode(".jpg", $file);
              
              // $file is now an array.
              // $file[0] holds the number 12

              Comment

              • anfetienne
                Contributor
                • Feb 2009
                • 424

                #8
                so to add the extra numbers ontop would i use?

                $i=1;

                $file[0].$i++

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Something like below:

                  Code:
                  $num;
                  
                  while( /* read directory */ )
                  {
                      // Do explode.
                      $num = $file[0];
                  }
                  
                  // Loop has ended. Increment $num by 1 for new number.
                  ++$num;

                  Comment

                  • anfetienne
                    Contributor
                    • Feb 2009
                    • 424

                    #10
                    ahhhhhh i see what you are getting at......

                    so lastly could i use ++$num like this?

                    (rename($pathA. $filename, $pathB.++$num.' .jpg')

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by anfetienne
                      ahhhhhh i see what you are getting at......

                      so lastly could i use ++$num like this?

                      (rename($pathA. $filename, $pathB.++$num.' .jpg')
                      Sure, but be careful: ++ increments the variable, so multiple calls to ++$num may confuse you on the output.

                      Code:
                      $i = 9;
                      
                      echo ++$i; // ouput: 10.
                      echo ++$i; // output: 11.

                      Comment

                      • anfetienne
                        Contributor
                        • Feb 2009
                        • 424

                        #12
                        would you recommend me using echo to make sure that ++$num outputs as it should?

                        Comment

                        • anfetienne
                          Contributor
                          • Feb 2009
                          • 424

                          #13
                          this is the code i have made to test this....i add to images and it increments fine to show the number 3 but if i add anymore images it doesn't detect them and continue incrementing.

                          Code:
                          <?
                          $path="upload/test/";
                          $num;
                           
                          if ($handle = opendir($path)) {
                             while (false !== ($file = readdir($handle))) {
                              if ($file != "." && $file != "..") {
                          
                          		
                                      $file = explode(".jpg", $file);
                          			$num = $file[0];
                          
                                 }
                             }
                          
                          }
                          closedir($handle);
                             
                          ++$num;
                          
                          echo $num;
                          ?>

                          Comment

                          • anfetienne
                            Contributor
                            • Feb 2009
                            • 424

                            #14
                            any ideas where ive gone wrong anyone?

                            Comment

                            • Atli
                              Recognized Expert Expert
                              • Nov 2006
                              • 5062

                              #15
                              Hi.

                              Before you take this any further, let me offer an alternate solution (or two)...

                              First, if you want to name the files "1.jpg", "2.jpg", etc...
                              Rather than count the actual files, it would be much much easier to simply store the last used number in a file (or preferably, a database), so that you could just read that number and increment it.

                              The file_get_conten ts and file_put_conten ts can make that very easy.

                              Second...
                              Unless you actually have a good reason to do so, storing the images as "1.jpg", "2.jpg", etc... isn't all that efficient.
                              It requires that you go out of your way to count the images and make sure there are no collisions.

                              I would recommend using a timestamp instead, which would require minimal tampering with to ensure that there are no collisions.

                              PHP has the function time, or even better, microtime, which will give you the current UNIX timestamp in seconds or milliseconds, respectively.

                              So, to generate a unique name, you could simply do:
                              [code=php]
                              <?php
                              $name = microtime(true) . mt_rand(1,999) . ".jpg";
                              ?>[/code]
                              Which would give you:
                              - 1239162720.1566 .jpg

                              And even if you happen to have two requests processed at the very same millisecond, there is only 1:999 chance that you get a collision. (Which can easily be increased)

                              Comment

                              Working...