move_uploaded_file failure

前端 未结 10 1077
挽巷
挽巷 2020-12-10 13:25

Can someone help me understand why this is returning false?

if ((move_uploaded_file($_FILES[$field][\'tmp_name\'], $path))) {

As in, potent

相关标签:
10条回答
  • 2020-12-10 13:32

    I was facing the same problem.

    If your error says "Failed to open stream: Permission Denied" it means the PHP Server is not being capable of creating the new file inside your destination directory.

    Once you have set the Linux permissions on the directory (wich sounds like you did by making it 777) you should give that special permission to the PHP Server.

    If your folder is named "uploads", you should cd to the previous directory and use the next command:

    chcon -R -t httpd_sys_rw_content_t uploads
    

    That definetly solved my problem.

    Hope it helps.

    0 讨论(0)
  • 2020-12-10 13:42

    Basic debugging steps:

    • What does a print_r($_FILES) look like?
    • Does the source file actually exist?
    • Is the "error" flag of the file upload zero (will be visible when doing the print_r)
    • What does the target $path look like?
    • Are you specifying a full file name in the target path?

    My guess is that $path is only a path to a folder, not to a full file name.

    Update: You need to specify a filesystem path as $path.

    0 讨论(0)
  • 2020-12-10 13:43

    With move_uploaded_file you don't need 777 permissions. What is the output of $path? Have you verified that $path exists? Have you verified that $field exists?

    Either $field or $path don't exist, or open_basedir is in effect is my guess.

    Is open_basedir restriction enabled? That could prevent the destination of the uploaded file from being written to. Look in your php.ini for open_basedir, if there's a path specified then it is enabled and you want to see if the destination path of your uploaded file is within this path. If it is, that's why it's failing.

    update

    $path cannot be a URL, it must be a local path such as /home/user/public_html/

    0 讨论(0)
  • 2020-12-10 13:43

    Don't use

    $path = "http://www.barbadostravelbuddy.co.uk/demo/images/carhire
        /accountile10420103260403000000pm.png"
    

    but

    $path = "/home/sites/barbadostravelbuddy.co.uk/public_html/demo/images/carhire/
        accountile10420103260403000000pm.png"
    

    It needs to be a path on the system, not an URL.

    0 讨论(0)
  • 2020-12-10 13:45

    Did you check permission on the temporary upload folder?

    What does php tell you if you do:

    var_dump($_FILES);
    
    0 讨论(0)
  • 2020-12-10 13:51

    $path has to be a file name and not a path to a directory.

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