0

I'm new to programming in PHP, and I'm trying to make a multiple file upload script, but I don't know how to check if the uploading files already exist! How can I do that? Can you help me? This is my code:

<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
  • Check if $path already exists with file_exists($path) before move_uploaded_file () Commented Oct 3, 2015 at 17:00
  • You are already doing that for a folderif(!file_exists('uploads')) { mkdir('uploads', 0777); } do the same with the file name. Commented Oct 3, 2015 at 17:10
  • Is it important that you generate the filename that includes the time? Commented Oct 3, 2015 at 17:25

3 Answers 3

1

file_exists return true if file is there in directory. So, you have to keep move_uploaded_file inside that if condition where file_exists you are checking.

For more info, check this 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>';
            }
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

0

It's pretty hard to do it, considering that you're giving them random names. If you're using a database, considering saving the file md5 and check if you already have it.

Otherwise you could save the file with its md5 as the file name.

This is how you can get the file's md5:

md5_file($tmp)

2 Comments

Where are you seeing the md5 file names ?
@Alex Please explain this ^
0

You could try something along the lines of the following. It uses a simple preg_match function call to see if there is a file that has the same name minus the timestamp.

<?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 );

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.