php多文件上传,file_exist

时间:2015-10-03 16:51:57

标签: php html file upload

我刚接触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>';
		}
	}

}
?>

3 个答案:

答案 0 :(得分:1)

如果目录中有文件,则file_exists返回true。 因此,如果你正在检查file_exists的条件,你必须将move_uploaded_file保留在其中。

有关详细信息,请查看此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>';
            }
        }

    }

答案 1 :(得分:0)

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

否则,您可以使用md5作为文件名保存文件。

这是你如何获取文件的md5:

md5_file($tmp)

答案 2 :(得分:0)

您可以尝试以下方面的内容。它使用简单的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 );

?>