php check if file exist

后端 未结 2 930
梦谈多话
梦谈多话 2020-12-21 04:33

can someone guide me on how to do checking to tell if file already exist in database or not before we upload it?

i wanna upload a file into database, but before it s

相关标签:
2条回答
  • Judging from the article you linked to, a file is considered as "uploaded to the database" when this query has been executed :

    $query = "INSERT INTO upload (name, size, type, content ) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
    

    Which means a way to check if a file "has been uploaded to the database" would be to check, when a file is being uploaded, if there is already an entry in the DB with the same name, file, type, and content that the current one.

    I suppose such an entry could be fetched using a query like this one :

    select *
    from upload
    where name = '$fileName'
        and size = '$fileSize'
        and type = '$fileType'
        and content = '$content'
    

    If that query returns a result, your file probably already is "in the database".


    BTW : To avoid checking the content of the whole file, a solution might be to add a new column to your upload table, to store some kind of hash of the file (see md5 and/or sha1 for instance).


    BTW 2 : you should read a bit about SQL Injections, before using the code presented in this article without really understanding it : the following portion of code, copy/pasted from the "code for downloading files" section, is quite frightening, for instance :

    $id    = $_GET['id'];
    $query = "SELECT name, type, size, content " .
             "FROM upload WHERE id = '$id'";
    
    0 讨论(0)
  • 2020-12-21 04:58

    I'm not sure if this is what you're asking, but have you used PHP's built in function, file_exists(ABSOLUTE-PATH-TO-FILE)?

    $exists = file_exists('myfile.doc');
    if(!$exists) {
    // do your processing
    }
    

    http://php.net/manual/en/function.file-exists.php

    If you're checking the database, then just query whatever column might hold your file, for example:

    SELECT * FROM my-table WHERE my-column = 'my-uploaded-file-name';
    
    0 讨论(0)
提交回复
热议问题