Files not storing in the correct folder

前端 未结 2 1419
醉梦人生
醉梦人生 2021-01-25 21:54

I created a page that can add new records to my database, everything is working fine but when I\'m trying to upload a pdf file, it doesn\'t store to the correct folder. It shoul

相关标签:
2条回答
  • 2021-01-25 22:17

    That's because you set only to save file name

    $file = $_REQUEST['file'];

    Instead it should be

    $file = "../uploads/".$_REQUEST['file'];

    0 讨论(0)
  • 2021-01-25 22:33

    There are quite a few logical errors in your code.

    1. You are inserting a record into the MySQL database before you do your check on the file extension and file size.

    2. You just insert the file name into MySQL ($file = $_REQUEST['file'];), hence only the file name appears there. The correct code would be:

      $file = "../uploads/".$_FILES['file']['name'];
      

    A bit more down you need to adjust the file move part:

        move_uploaded_file($file_tmp, $file);
    
    1. In the error checking after the sql insert you use mysql_error(), not mysqli_error($con)

    2. You do not check if the move_uploaded_file($file_tmp,"../uploads/".$file_name); call was successful and the file was moved to its final location.

    3. Also pls consider using prepared statements to prevent sql injection attacks.

    0 讨论(0)
提交回复
热议问题