Re: Re: HTTP_Upload unique files

From: Date: Mon, 04 Oct 2004 05:12:34 +0000
Subject: Re: Re: HTTP_Upload unique files
References: 1 2 3 4 5 6  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-33644@lists.php.net to get a copy of this message
What I have been thinking about is a bit more complex. what if the uploaded userfile already had a sequence in it's name? e.g userGuide[3].pdf This is like what i have been thinking of. I really like the idea of the while loop Norbert suggested, I guess it's better than getting the highest sequence and then adding one to that, and it's for sure more effecient in most cases of there are gaps in sequences. So that might replace the 'max_seq' thing. anyway, tell me what you think of this? function nameToSeq($dir_dest) {
       //let's check if the a file with the same name already exists
       $name_dest = $dir_dest . DIRECTORY_SEPARATOR .
$this->upload['name'];
       if (!@is_file($name_dest)) {
           return $this->upload['name'];
       } else {
           //we need to strip out the extension and the '.' of the file
           $base_file_length = strlen($this->upload['name']) -
strlen($this->upload['ext']) - 1;
           $base_file_name = substr( $this->upload['name'],0,
$base_file_length );
           $base_file_name;
           $pattern = '(\[)([[:digit:]]+)(\])$'; //we're looking for
this pattern at the end of the name
            //strip the pattern out of the file if it exists, so that
we don't end up with ugly names eventually. e.g userGuide[3][5].pdf
            $base_file_name = ..........
            //get the highest sequence and store it in $max_seq
            //return the new file name
            return $base_file_name . '[' . ++$max_seq . ']' ;
       }
} Norbert Mocsnik wrote:
Erik Heerlein wrote:
What about being able to specify the starting number of the sequence? To start at something other than zero? Or pick where the number would go in the filename? Like suppose somebody wanted to do 'userGuide_6.pdf' or userGuide[f][6].pdf. I don't know if there would be a great need to do this I'm just wondering.
function nameToSeq($path, $template = '') { .. if (empty($template)) {
       $template = $this->upload['name'] . '[%d]';
} while (...) { // or do .. while, dunno how you want it
       $i++;
       $filenametocheck = sprintf($template, $i);
       ... check if available ...
} .. } // usually $this->$nameToSeq('/upload_path/'); // Erik's suggestion $this->$nameToSeq('/upload_path/', 'userGuide[f][%d]');
    


« previous php.pear.dev (#33644) next »