gijew Posted May 2, 2008 Share Posted May 2, 2008 I've been working several hours on trying to get this to work and although I'm gaining ground, I'm not really getting anywhere. The idea is user uploads file -> system checks if file exists in the target folder -> |->exists - check if there are any files already incremented (ie: file (1).txt, file (2).txt, etc). - extract the numeric content value from the string - add a new value - upload file with new id |->does not exist - upload file So here's what I have after programming all day. My regex sucks but I'll try to walk through why I was doing what I was doing. After all this work today I'm thinking I'm taking a bad approach. Can anyone lend a hand please? <?php // rip through the directory to do the file check If ($DIR_HANDLE = opendir($FILE_DIRECTORY)) { While (false !== ($FILED = readdir($DIR_HANDLE))) { // init $MATCHES = Array(); $NUMERIC = Array(); // check if the file being uploaded already exists in the file system If (Preg_Match('/'.$FILE_NAME.'/', $FILED, $MATCHES)) { // if the file already exists let's start peeling off the max count. If (Preg_Match('/\(([0-9]+)\)/', $MATCHES, $NUMERIC)) { // get the numeric value and create a new count $CURRENT_OFFSET = Preg_Match('/[0-9]/',$NUMERIC[0]); $NEW_OFFSET = $CURRENT_OFFSET + 1; $CHANGED_FILE_NAME = $FILE_WO_EXTENSION . ' ('.$NEW_OFFSET.')' . '.' . $FILE_EXT; } Else { // start the auto id at (1) $CHANGED_FILE_NAME = $FILE_WO_EXTENSION . '(1).' . $FILE_EXT; } } Else { // file is new - don't do anything. $CHANGED_FILE_NAME = $FILE_NAME; } } // close the directory closedir($DIR_HANDLE); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103941-solved-auto-increment-if-file-exists/ Share on other sites More sharing options...
gijew Posted May 5, 2008 Author Share Posted May 5, 2008 /bump Forums been busy lately Quote Link to comment https://forums.phpfreaks.com/topic/103941-solved-auto-increment-if-file-exists/#findComment-533505 Share on other sites More sharing options...
MadTechie Posted May 5, 2008 Share Posted May 5, 2008 Maybe somthing like this Test if the file does not exists then use it <?php $FILE_NAME = "test.jpg"; $FILE_DIRECTORY = "/path/php5/"; if (preg_match('/(^.*?)+(?:\((\d+)\))?(\.(?:\w){0,3}$)/si', $FILE_NAME, $regs)) { $filename = $regs[1]; $copies = (int)$regs[2]; $fileext = $regs[3]; $fullfile = $FILE_DIRECTORY.$FILE_NAME; while(file_exists($fullfile) && !is_dir($fullfile)) { $copies = $copies+1; $FILE_NAME = $filename."(".$copies.")".$fileext; $fullfile = $FILE_DIRECTORY.$FILE_NAME; } } echo $FILE_NAME; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103941-solved-auto-increment-if-file-exists/#findComment-533518 Share on other sites More sharing options...
gijew Posted May 5, 2008 Author Share Posted May 5, 2008 Dude. Awesome! I had to make a few modifications to work right in the actual environment but...it works. Thank you <?php If (file_exists($FILE_DIRECTORY.$FILE_NAME) && !is_dir($FILE_DIRECTORY.$FILE_NAME)) { If (preg_match('/(^.*?)+(?:\((\d+)\))?(\.(?:\w){0,3}$)/si', $FILE_NAME, $OPTIONS)) { $OFFSET = (int)$OPTIONS[2]; while(file_exists($FILE_DIRECTORY.$FILE_NAME) && !is_dir($FILE_DIRECTORY.$FILE_NAME)) { $OFFSET = $OFFSET+1; $FILE_NAME = $FILE_WO_EXTENSION."(".$OFFSET.").".$FILE_EXT; $CHANGED_FILE_NAME = $FILE_NAME; } } } Else { $CHANGED_FILE_NAME = $FILE_NAME; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103941-solved-auto-increment-if-file-exists/#findComment-533565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.