PHP下载文件问题

时间:2014-01-24 14:15:42

标签: php image ftp download

<?php

// sample usage
ini_set('max_execution_time', 6000);
ini_set('memory_limit', '-1');

$local_file = '123.jpg';
$server_file = '/animals/pets/dogs/pitbull.jpg';
$ftp_user_name='username';
$ftp_user_pass='password';
$ftp_server='URL';
// 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);


ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 60);
ftp_pasv($conn_id, false);
// 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);
?>

上面的代码没有下载文件。 下载文件可能会出现什么问题?

1 个答案:

答案 0 :(得分:1)

作为Jompper mentioned in a comment,使用相对路径而不是绝对路径 如果您的animals文件夹是您运行脚本的根目录,

使用

$server_file = 'animals/pets/dogs/pitbull.jpg';

绝对路径类似于

$server_file = '/var/user/you/public_html/animals/pets/dogs/pitbull.jpg';

正如我所提到的,如果您从根运行此代码,那么请使用我写的内容。

例如

(root是你的public_html文件夹)

root/your_script.php/animals/pets/dogs/pitbull.jpg