1

I'm currently getting to know more with uploadify, which by the way is what I'm using on my Wordpress plugin. I got the uploading of file correctly; it's job is to upload single .pdf files only. When I tried uploading the same file twice and checked the folder where the uploaded files will be stored, I only have a single file. I guess it's being overwritten knowing the file already exists on the folder. What bugs me is that how will I change the filename of the second uploaded file(the same file) such that it will result into 'filename(2)', 'filename(3)' and so on.

Here's my code, enlighten me on where should I start configuring on my uploadify.php:

    if (!empty($_FILES)) {
        $name = $_FILES['Filedata']['name'];

        $tempFile = $_FILES['Filedata']['tmp_name'];
        $targetPath = $targetFolder;
        $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

        $path = pathinfo($targetFile);

        $newTargetFile = $targetFolder.$name;

        // Validate the file type
        $fileTypes = array('pdf'); // File extensions
        $fileParts = pathinfo($_FILES['Filedata']['name']);

        if (in_array($fileParts['extension'],$fileTypes)) {
            // i think somewhere here , will i put something, but what's that something?
            move_uploaded_file($tempFile,$newTargetFile);
            echo $newTargetFile;
        } else {
            echo 'Invalid file type.';
        }
        return $newTargetFile;
    }
Tsukimoto Mitsumasa
  • 541
  • 4
  • 19
  • 42

2 Answers2

2

Change this:

$newTargetFile = $targetFolder.$name;

To this:

$i = 2;
list( $filename, $ext) = explode( '.', $name);
$newTargetFile = $targetFolder . $filename . '.' . $ext;
while( file_exists( $newTargetFile)) {
    $newTargetFile = $targetFolder . $filename . '(' . ++$i . ')' . '.' . $ext;
}
nickb
  • 59,313
  • 13
  • 108
  • 143
  • Thanks a lot man. although I put the (' . ++$i . ') on the front. Using your method, my the file will not be distinguished as such because the result is like this, filename.ext(3), making the file type unknown. BUt thanks a lot though..:) – Tsukimoto Mitsumasa Jul 19 '12 at 17:59
  • Sure - I couldn't tell if `$name` included an extension. Glad I could help! – nickb Jul 19 '12 at 18:01
  • Actually it does include the extension.Is it possible if I can concatenate the path + filename + (++$i) + extension? – Tsukimoto Mitsumasa Jul 19 '12 at 18:07
  • Sure, I can update my answer. One sec :) Edit: Updated - Try that :) – nickb Jul 19 '12 at 18:07
  • Hey, my man, a quick question. What will I do if the filename of the uploaded file contains '.'? Will the part that you explode still work? I tried but it exploded $name upon seeing the first '.' on the file name. – Tsukimoto Mitsumasa Jul 19 '12 at 23:02
  • Nah it'll fail - You'd have to change the explode part to [pathinfo](http://stackoverflow.com/questions/173868/how-to-extract-a-file-extension-in-php) – nickb Jul 20 '12 at 00:27
1

Try this:

<?php

function get_dup_file_name($file_name) {
    $suffix = 0;

    while (file_exists($file_name . ($suffix == 0 ? "" : "(" . $suffix . ")"))) {
        $suffix++;
    }

    return $file_name . ($suffix == 0 ? "" : "(" . $suffix . ")");
}

?>
Daniel Li
  • 14,976
  • 6
  • 43
  • 60
  • can't I just add something, maybe an if condition before implementing move_uploaded_file? It may be hassle for me to use this. Just a thought. – Tsukimoto Mitsumasa Jul 19 '12 at 17:47
  • This is a fairly simple function. The reason why you need to implement this is because you never know if there are 2, 3, 4 or a million files with the same name. This will help you handle all of this accordingly. – Daniel Li Jul 19 '12 at 17:48