error upon uploading image no such path or directory

后端 未结 1 724
闹比i
闹比i 2021-01-06 16:09

Good day, I have been following a tutorial and have worked really hard to make this script work, I have managed to fix many things however the one constant problem I\'m gett

相关标签:
1条回答
  • 2021-01-06 16:56

    When dealing with images, you need to use a binary format.

    FTP_BINARY instead of FTP_ASCII.

    As per the manual:

    • http://php.net/manual/en/function.ftp-put.php

    Make sure the path is correct. This can differ from different servers.

    You also need a trailing slash here:

    public_html/img/userPics/
                            ^
    

    Otherwise, your system will interpret that as:

    userPicsIMAGE.JPG rather than the intended userPics/IMAGE.JPG

    Yet, you may need to play around with the path itself. FTP is a bit tricky that way.

    I.e.:

    • /home/xxxxxxx/public_html/userPics/
    • img/userPics/
    • ../img/userPics/

    Depending on the file's area of execution.

    Root > ftp_file.php  
    -img  
       -userPics
    

    The folder also needs to have proper permissions to be written to.

    • Usually 755.

      Use error reporting if your system isn't already setup to catch and display error/notices:

      • http://php.net/manual/en/function.error-reporting.php

    An example from the FTP manual:

    <?php 
    ftp_chdir($conn, '/www/site/'); 
    ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY ); 
    ?>
    

    Another thing though, the use of of "true" and "false". You're most likely wanting to check for truthness

    $file_upload=true;
    

    rather than a string literal $file_upload="true";

    same thing for

    if($file_upload=="true")
    

    to

    if($file_upload==true)
    

    and for $file_upload="false"; to check for falseness

    to $file_upload=false;

    Read the manual on this:

    • http://php.net/manual/en/language.types.boolean.php

    This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE.


    Test your FTP connection, pulled from https://stackoverflow.com/a/3729769/

    try {
        $con = ftp_connect($server);
        if (false === $con) {
            throw new Exception('Unable to connect');
        }
    
        $loggedIn = ftp_login($con,  $username,  $password);
        if (true === $loggedIn) {
            echo 'Success!';
        } else {
            throw new Exception('Unable to log in');
        }
    
        print_r(ftp_nlist($con, "."));
        ftp_close($con);
    } catch (Exception $e) {
        echo "Failure: " . $e->getMessage();
    }
    

    Naming conventions:

    I also need to note that on LINUX, userPics is not the same as userpics, should your folder name be in lowercase letters.

    Unlike Windows, which is case-insensitive.

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