0

I found this example at the php.net/manual,but could not get the whole concept in the example. It seems the example is missing some details.

Would be grateful if someone can simplify it for me. Here's the example:

I've found that the most useful thing to use do-while loops for is multiple checks of file existence. The guaranteed iteration means that it will check through at least once, which I had trouble with using a simple "while" loop because it never incremented at the end.

My code was:

 $filename = explode(".", $_FILES['file']['name']); // File being uploaded
    $i=0; // Number of times processed (number to add at the end of the filename)
    do {

/* Since most files being uploaded don't end with a number,
  we have to make sure that there is a number at the end
  of the filename before we start simply incrementing. I
  admit there is probably an easier way to do this, but this
  was a quick slap-together job for a friend, and I find it
  works just fine. So, the first part "if($i > 0) ..." says that
  if the loop has already been run at least once, then there
  is now a number at the end of the filename and we can
  simply increment that. Otherwise, we have to place a
  number at the end of the filename, which is where $i
  comes in even handier */

  if($i > 0) $filename[0]++;
  else $filename[0] = $filename[0].$i;
  $i++;
} while(file_exists("uploaded/".$filename[0].".".$filename[1]));

How can if($i > 0) detect that a file ends with a number ? Does the loop refer to a preg_match ?

Habibillah
  • 27,347
  • 5
  • 36
  • 56
MFIHRI
  • 311
  • 1
  • 3
  • 15

1 Answers1

0

if ($i > 0) detects if the loop has already been run since it is 0 on first entry and > 0 on all subsequent iterations

The first iteration will always append .0 to the filename since $i is set to 0 Then, if file.0.extension doesnt exist, it will try to increment $filename[0] which, as a string, should fail.

You might be intending to compare $filename[0] > 0 which would be false for any non-numeric filename (including filenames that end in a number but include letters) because of the way PHP does casting http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

I see nothing regarding preg_match in there.

If all you want is a unique filename you can try rolling your own with http://php.net/manual/en/function.uniqid.php (see php Unique filename when uploading for example)

Community
  • 1
  • 1
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
  • Thank you for taking the time to explain all this. I was just wondering what array the loop is iterating over, was thinking the $_FILES – MFIHRI Dec 04 '13 at 22:23
  • its not really iterating over an array. what it does is initialize $i to 0, and loops until `"uploaded/".$filename[0].".".$filename[1]` doesnt exist – arcyqwerty Dec 05 '13 at 05:54
  • with respect to the `$filename` array, that is the result of `explode()` which basically splits `filename.ext` into `[filename, ext]` or `filename.somethingelse.ext` into `[filename, somethingelse, ext]` – arcyqwerty Dec 05 '13 at 05:55
  • Thank you arcyqwerty for bearing with me as I am proving slow processing this. So isn't it just a matter of muliple uploads vs a single file upload? With a multiple files upload, there is a number at the end of the file. With a single file upload, there will be no loop iteration and hence we need to add the .0 ["name"]=>array(3) { [0]=>string(9)"file0.txt" [1]=>string(9)"file1.txt" [2]=>string(9)"file2.txt" – MFIHRI Dec 05 '13 at 18:43
  • You are only processing a single upload. `$_FILES['file']['name']` means get the name of the file uploaded from form field ` – arcyqwerty Dec 06 '13 at 20:46