E. Martinnez
E. Martinnez

Reputation: 15

How to avoid duplicate file in upload using PHP?

Good day everyone, I want to prevent uploading a file if the name and file extension match one of the saved records. My code below works but how can I avoid duplicate entries?

"My sample array"
Array
(
    [upload-file] => Array
        (
            [name] => Penguins.jpg
            [type] => image/jpeg
            [tmp_name] => C:\xampp\tmp\phpC87.tmp
            [error] => 0
            [size] => 777835
        )

)

case 'upload-file':

    $arr = [ 
        ":userid" => $_SESSION['loggedIn_PH'][0]['user_id'],
        ":filename" => $_FILES['upload-file']['name'],
        ":filelink" => $_FILES['upload-file']['tmp_name']
        ];

        $allowed =  array('xls','xlsx');
        $filename = $_FILES['upload-file']['name'];
        $ext = pathinfo($filename, PATHINFO_EXTENSION);

        if(!in_array($ext,$allowed) ) {
            $response_code = -1;
        }else{
            $response_code = 1;
            $folder = time();
            mkdir("path/".$folder);
            $file = "path".DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$_FILES['upload-file']['name'];
                    move_uploaded_file($_FILES['upload-file']['tmp_name'], $file);

        $query = "INSERT INTO file_rec_tbl ( `file_name`, `file_datetime`,`file_link`, `user_id` )
                    VALUES (:filename, '".date('Y-m-d H:i:s')."',:filelink,:userid)";

        $stmt = $con -> prepare( $query );
        $stmt -> execute( $arr );

    }

    exit(json_encode(array('r_code' => $response_code)));
    break;

Upvotes: 1

Views: 4299

Answers (3)

Pradyut Manna
Pradyut Manna

Reputation: 588

<?php
case 'upload-file':

$arr = [ 
    ":userid" => $_SESSION['loggedIn_PH'][0]['user_id'],
    ":filename" => $_FILES['upload-file']['name'],
    ":filelink" => $_FILES['upload-file']['tmp_name']
    ];

    $allowed =  array('xls','xlsx');
    $filename = $_FILES['upload-file']['name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    if(!in_array($ext,$allowed) ) {
        $response_code = -1;
    }else{
        $response_code = -1;
        $folder = time();
        mkdir("path/".$folder);
        $file = "path".DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$_FILES['upload-file']['name'];
if(!file_exists($file)){
    $response_code = 1;
                move_uploaded_file($_FILES['upload-file']['tmp_name'], $file);

    $query = "INSERT INTO file_rec_tbl ( `file_name`, `file_datetime`,`file_link`, `user_id` )
                VALUES (:filename, '".date('Y-m-d H:i:s')."',:filelink,:userid)";

    $stmt = $con -> prepare( $query );
    $stmt -> execute( $arr );
}

}

exit(json_encode(array('r_code' => $response_code)));

?>

Upvotes: 0

yiming.xie
yiming.xie

Reputation: 23

simple code could like this:

$file_name = 'path/to/file_name.ext';
if (file_exist($file_name)) {
    return true;
} else {
    move_uploaded_file($_FILES['upload-file']['tmp_name'], $file_name);
    insert into database;
    return true;
}

besides, you should not compare two files noly with file name.

Upvotes: 0

kerry
kerry

Reputation: 691

<?php
$file='file-to-check.ext';
if (file_exists($file)) {
    echo "exists";
} else {
    echo "not exist";
}
?>

works for me - obviously the $file could also include a path - this script checks only the current folder

Upvotes: 1

Related Questions