Jump to content

[SOLVED] Auto increment if file exists.


gijew

Recommended Posts

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);
}
?>

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.