【问题标题】:PHP download file issue [closed]PHP下载文件问题[关闭]
【发布时间】:2014-01-24 14:15:42
【问题描述】:
<?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);
?>

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

【问题讨论】:

  • 你得到的输出是什么?
  • 您有什么迹象表明存在问题?有错误吗?意外行为?对服务器的请求是什么?服务器的响应是什么?您至少需要进行一些调试。
  • /photos/bigphoto/529/509529_01.jpg是绝对路径,你确定不要使用相对路径
  • 你有什么错误吗??
  • 正如 Jompper 提到的,使用相对路径而不是绝对路径。如果您的 animals 文件夹是您运行脚本的根目录中的一个,请使用 $server_file = 'animals/pets/dogs/pitbull.jpg'; @BannedfromSO ---绝对路径类似于 $server_file = '/var/user/you/public_html/animals/pets/dogs/pitbull.jpg';

标签: php image ftp download


【解决方案1】:

作为 Jompper mentioned in a comment,使用相对路径代替绝对路径。
如果您的动物文件夹是您运行脚本的根目录中的一个,

使用

$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

【讨论】:

  • 如果我想从 Web 服务下载文件,该文件阻止了曝光或图像显示在浏览器上,该怎么办?而且我们没有像public_html/animals/pets/dogs/pitbull.jpg这样的路径,而是`domain.com/images/photos/image.jpg,请回答,因为这是真正的工作。
  • 我认为您可能需要为此使用 Curl。我对它的了解不够多,无法为其编写代码,但我很确定这就是您需要使用的。至于public_html... 问题,如果您担心安全问题,那应该不是问题。 @BannedfromSO
猜你喜欢
  • 2023-04-10
  • 2011-02-07
  • 1970-01-01
  • 2013-11-02
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多