我目前正在了解更多关于uploadify的信息,顺便说一句,这就是我在Wordpress插件上使用的内容。我正确上传了文件;它的工作是只上传单个.pdf文件。当我两次尝试上传同一个文件并检查上传文件的存储文件夹时,我只有一个文件。我想它被覆盖了,因为我知道文件夹中已经存在该文件。困扰我的是,我将如何更改第二个上传文件(同一文件)的文件名,使其变成"文件名(2)"、"文件名"等
这是我的代码,启发我在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;
}
更改此项:
$newTargetFile = $targetFolder.$name;
对此:
$i = 2;
list( $filename, $ext) = explode( '.', $name);
$newTargetFile = $targetFolder . $filename . '.' . $ext;
while( file_exists( $newTargetFile)) {
$newTargetFile = $targetFolder . $filename . '(' . ++$i . ')' . '.' . $ext;
}
试试这个:
<?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 . ")");
}
?>