php 多个文件上传,file_exist

php multiple file upload, file_exist

我是PHP的编程新手,我正在尝试制作一个多文件上传脚本,但我不知道如何检查上传文件是否已经存在!我怎样才能做到这一点?你能帮助我吗? 这是我的代码:

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload">
</form>

foreach($_FILES['files']['name'] as $i => $name) {

 $name = $_FILES['files']['name'][$i];
 $size = $_FILES['files']['size'][$i];
 $type = $_FILES['files']['type'][$i];
 $tmp = $_FILES['files']['tmp_name'][$i];

 $explode = explode('.', $name);

 $ext = end($explode);

 $path = 'uploads/';
 $path = $path . basename( $explode[0] . time() .'.'. $ext);
 
 $errors = array();

 if(empty($_FILES['files']['tmp_name'][$i])) {
  $errors[] = 'Please choose at least 1 file to be uploaded.';
 }
 if(empty($errors)) {
  
  if(!file_exists('uploads')) {
   mkdir('uploads', 0777);
  }

  if(move_uploaded_file($tmp, $path)) {
   echo '<p>The file <b>'.$name.'</b> successfully uploaded</p>';
  }else {
   echo 'Something went wrong while uploading the file <b>'.$name.'</b>';
  }

 }else {
  foreach($errors as $error) {
   echo '<p>'.$error.'<p>';
  }
 }

}
?>

考虑到您给他们随机命名,这很难做到。如果您正在使用数据库,请考虑保存文件 md5 并检查您是否已经拥有它。

否则您可以将文件以其md5 作为文件名来保存。

这是获取文件的 md5 的方法:

md5_file($tmp)

您可以尝试以下方法。它使用一个简单的 preg_match 函数调用来查看是否存在同名减去时间戳的文件。

<?php

    $errors = array();
    $path = 'uploads/';

    /* Check if the target directory exists - no need to do it repeatedly */
    if( !file_exists( $path ) ) mkdir( $path, 0777 );
    /* file_exists results are cached */
    clearstatcache();


    foreach( $_FILES['files']['name'] as $i => $name ) {
        if( !empty( $_FILES['files']['tmp_name'][$i] ) ) {
            $name = $_FILES['files']['name'][$i];
            $size = $_FILES['files']['size'][$i];
            $type = $_FILES['files']['type'][$i];
            $tmp  = $_FILES['files']['tmp_name'][$i];

            $ext=pathinfo( $name, PATHINFO_EXTENSION );
            $basename=pathinfo( $name, PATHINFO_FILENAME );
            $filepath = $path . $basename . time() . '.' . $ext;
            $pttn='@'.$basename.'\d{10}@';

            /* Does a file, with the name of the original, exist? */
            if( preg_match( $pttn, implode( '', glob( $path .'*.*' ) ) ) ){
                /* Files already exist */   
            } else {
                $result=@move_uploaded_file( $tmp, $filepath );
                echo $result ? '<p>The file <b>'.$name.'</b> successfully uploaded</p>' : 'Something went wrong while uploading the file <b>'.$name.'</b>';
            }
        } else {
            $errors[] = 'Please choose at least 1 file to be uploaded.';
        }
    }
    if( !empty( $errors ) ) echo implode( '<br />', $errors );

?>

file_exists return 如果文件在目录中则为真。 因此,您必须将 move_uploaded_file 保留在您正在检查 file_exists 的 if 条件中。

有关详细信息,请查看此 File Exist - W3 Schools

    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple>
        <input type="submit" value="Upload">
    </form>

    foreach($_FILES['files']['name'] as $i => $name)
    {

        $name = $_FILES['files']['name'][$i];
        $size = $_FILES['files']['size'][$i];
        $type = $_FILES['files']['type'][$i];
        $tmp = $_FILES['files']['tmp_name'][$i];

        $explode = explode('.', $name);

        $ext = end($explode);

        $path = 'uploads/';
        $path = $path . basename( $explode[0] . time() .'.'. $ext);

        $errors = array();

        if(empty($_FILES['files']['tmp_name'][$i])) {
            $errors[] = 'Please choose at least 1 file to be uploaded.';
        }
        if(empty($errors)) 
        {

            if(!file_exists($path))
            {

                if(move_uploaded_file($tmp, $path)) 
                {
                    echo '<p>The file <b>'.$name.'</b> successfully uploaded</p>';
                }
                else
                {
                    echo 'Something went wrong while uploading the file <b>'.$name.'</b>';
                }
            }
        }
        else
        {
            foreach($errors as $error)
            {
                echo '<p>'.$error.'<p>';
            }
        }

    }