1

我目前正在通过 uploadify 了解更多信息,顺便说一句,这就是我在 Wordpress 插件上使用的。我正确上传了文件;它的工作是仅上传单个 .pdf 文件。当我尝试两次上传相同的文件并检查将存储上传文件的文件夹时,我只有一个文件。我猜它被覆盖知道该文件已经存在于文件夹中。让我烦恼的是,我将如何更改第二个上传文件(同一个文件)的文件名,使其变为“文件名(2)”、“文件名(3)”等等。

这是我的代码,请告诉我应该从哪里开始配置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;
    }
4

2 回答 2

2

改变这个:

$newTargetFile = $targetFolder.$name;

对此:

$i = 2;
list( $filename, $ext) = explode( '.', $name);
$newTargetFile = $targetFolder . $filename . '.' . $ext;
while( file_exists( $newTargetFile)) {
    $newTargetFile = $targetFolder . $filename . '(' . ++$i . ')' . '.' . $ext;
}
于 2012-07-19T17:50:00.690 回答
1

试试这个:

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

?>
于 2012-07-19T17:44:30.660 回答