【问题标题】:PHP ftp_get failed to open stream when trying to download file尝试下载文件时 PHP ftp_get 无法打开流
【发布时间】:2023-03-27 08:10:02
【问题描述】:

我正在尝试让浏览器从 FTP 服务器下载文件,但无论我尝试什么,都会收到此错误:

Warning: ftp_get(taak4.docx) [function.ftp-get]: failed to open stream: Permission denied in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231

Warning: ftp_get() [function.ftp-get]: Error opening taak4.docx in /home/jamesmr117/domains/notepark.be/public_html/classes/taak.php on line 231

但是我 100% 确定我的 FTP 服务器工作正常,因为上传文件工作正常。我还将每个文件夹都设置为 chmod 777。有人知道可能是什么问题吗?

我的php代码:

$local_file="taak4.dockx";
$server_file="taak4.dockx";
ftp_get($FTPClient->connectionId, $local_file, $server_file, FTP_BINARY);

提前致谢!

【问题讨论】:

  • 您确定您有权将本地文件保存在您的 PHP 脚本文件夹中吗?

标签: php ftp


【解决方案1】:

您必须指定文件的完整路径。例如:

/var/home/victor/files/taak4.dockx

使用$_SERVER['DOCUMENT_ROOT'] 获取文档根目录路径。

【讨论】:

  • 文件直接在脚本的路径中。这个我试过了,还是不行。
【解决方案2】:

您需要拥有$local_file 路径的写入权限。让它成为一个完整的路径。示例:chmod 777 /test 并使 $local_file 类似于 /test/taak4.docx

【讨论】:

    【解决方案3】:

    即使在更改了远程服务器上的文件权限后,我也遇到了这个问题,但我无法在本地服务器上下载它:

    Warning: ftp_get(): Can't open Capture.PNG: No such file or directory in C:\MAMP\htdocs\ftp.php on line 25
    

    解决方案:

    在 $server_file 变量中写入任何路径之前,必须包含“/”,因此完美运行的整个示例在这里:

    // This is the path and new file name on my server (name can be different from the remote server file )
    // if i want to save it just where my current php file is running ,no need to enter any path just file name
    $local_file = 'capture.png';
    
    // This is the path and file name on my Remote server from which i am downloading from
    // this should start with '/' and write 'public_html' or 'htdocs' afterwards
    $server_file = '/public_html/Capture.PNG';
    
    // ftp details
    $ftp_server="example.host.com";
    $ftp_user_name="username";
    $ftp_user_pass="password";
    
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
    // This is to so that we do not time out
    ftp_pasv($conn_id, true);
    
    // try to download $server_file and save to $local_file
    if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
        echo "Successfully written to $local_file\n";
    } else {
        echo "There was a problem\n";
    }
    
    // close the connection
    ftp_close($conn_id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-23
      • 2013-12-07
      • 2019-02-26
      • 2019-09-30
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多