-1

我有一个 php 文件上传程序。我想为程序添加一项功能,即检查目录中是否存在相同的文件。

我想在我的代码中添加这样的内容

if (file_exists("directory_name/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }  

这是 我的 php 文件上传器代码:

<?php

$valid_formats = array("jpg", "png", "gif", "JPEG", "bmp", "JPG", "PNG", "GIF");
$max_file_size = 1024*500; //100 kb
$path = "images/$_POST[fol]/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
    // Loop $_FILES to exeicute all files
    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['error'][$f] == 4) {
            continue; 
        }          
        if ($_FILES['files']['error'][$f] == 0) {              

            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                continue;
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                continue; 
            }


            else{ 
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; 
            }
        }
    }
}
?>

如何将两者结合起来?谁能帮忙?

4

2 回答 2

1

这是解决方案。

 else{ 

                if(!file_exists($path.$name))
                {
                  move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name);
                  $count++; // Number of successfully uploaded file
                } 
                else{
                    echo "File exists";
                                }


  }
于 2013-06-01T08:02:09.883 回答
1

在移动文件之前检查文件是否存在..尝试类似

        else{ 
                if (file_exists($path.$name)
                {
                  file already exixts
                }  
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                $count++; 
            }
于 2013-06-01T07:01:00.073 回答