簡體   English   中英

嘗試下載文件時,PHP ftp_get無法打開流

[英]PHP ftp_get failed to open stream when trying to download file

我正在嘗試讓瀏覽器從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);

提前致謝 !

您必須指定文件的完整路徑。 例如:

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

使用$_SERVER['DOCUMENT_ROOT']獲取文檔根目錄路徑。

您需要對$local_file路徑具有寫權限。 讓它成為一條完整的道路。 示例: chmod 777 /test和make $local_file類似於/test/taak4.docx

即使在我的遠程服務器上更改文件權限后,我也遇到了這個問題,我無法在本地服務器上下載它:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM