简体   繁体   English

PHP的:从FTP服务器下载任何文件到硬盘?

[英]PHP: download any file from ftp-server to harddrive?

i really really need your help. 我真的很需要您的帮助。 I'm successfully setting up a connection to my ftp server. 我已成功建立与ftp服务器的连接。 However I can't figure out how I can download a dynamic file from my server. 但是我不知道如何从服务器下载动态文件。 I guess this would help a lot of other people as well cause i couldn't find a tutorial or explanation anywhere on the web. 我想这也会对很多其他人有所帮助,因为我在网络上的任何地方都找不到教程或解释。

I'm simply listing all of my files on my ftp. 我只是在ftp上列出所有文件。 If i click on one i call download.php which connects again to the server and should download the file to my harddrive. 如果我单击一个,我会调用download.php,它将再次连接到服务器,并且应该将文件下载到我的硬盘中。 I was able to auto-prompt a download window and a file gets downloaded to my hdd. 我能够自动提示下载窗口,并且文件下载到我的硬盘上。 however only a damaged file where no headers are set. 但是,只有未设置标题的损坏文件。

/* path to files on ftp server … e.g. /folder/folder/name_of_file.jpg */
$p_arr = explode("/", $path);
/* the file name … e.g name_of_file.jpg */
$file = end($p_arr);

//$finfo = finfo_open(FILEINFO_MIME_TYPE); //returns a fatal error - function not found 
//$mimetype = finfo_file($finfo, $file);
//finfo_close($finfo);

//filetype($file)

/* creating a temporyry file to save to */
$tempFile = tempnam("/tmp", "FOO");

if(ftp_get($conn_id, $tempFile, $file, FTP_BINARY)){ 
    /*header Content-type: must be dynamic*/
    //header('Content-type:' . $mimetype);

    /*header to auto_prompt download window*/
    header('Content-Disposition: attachment; filename="'. $file .'"');
    readfile($tempFile);
} else { 
    echo "There was a problem <br>";
    echo $file . "<br>"; //e.g. image.jpg
}

/* deleting the file after the process */
//unlink($tempFile);

1.) i can't figure out why ftp_get returns false. 1.)我不知道为什么ftp_get返回false。 The connection gets set up and the file exists in the right directory. 连接已建立,文件位于正确的目录中。 2.) i don't know how i can find out the mimetype of the file on the server and give it the downloaded file, so it's not damaged. 2.)我不知道如何找到服务器上文件的模仿类型并提供下载的文件,因此它没有损坏。

please help me out here, i'm really stuck. 请在这里帮助我,我真的很困。 thank you in advance 先感谢您

Try to save it locally first and then push it back to browser. 尝试先将其保存在本地,然后再将其推回浏览器。

Use that code to save your file locally. 使用该代码将文件保存在本地。

<?php
                // define some variables
        $folder_path = "YOUR FOLDER PATH";
        $local_file = "LOCAL FILE PATH";
        $server_file = "SERVER FILE PATH";

        //-- Connection Settings
        $ftp_server = "IP ADDRESS"; // Address of FTP server.
        $ftp_user_name = "USERNAME"; // Username
        $ftp_user_pass = "PASSWORD"; // Password
        #$destination_file = "FILEPATH";

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

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

I also found this, maybe it could help you 我也找到了,也许可以帮到你

My FTP-Server always responded "bool(false)" instead of presenting me a directory-listing. 我的FTP服务器始终响应“ bool(false)”,而不是向我显示目录列表。 I had to add ftp_pasv($conn_id, true); 我必须添加ftp_pasv($ conn_id,true); right after the $login_result = ftp_login(...); 在$ login_result = ftp_login(...)之后 line. 线。 After that it just worked fine. 在那之后,它运行良好。

This works for me: 这对我有用:

$fname = "file.xxx";

$conn = ftp_connect(FTP_URL);
if(ftp_login($conn, FTP_USER, FTP_PWD))
{
    if(ftp_chdir($conn, FTP_DIR))
    {
        $th = fopen('php://temp', 'r+');
        if(ftp_fget($conn, $th, $fname, FTP_ASCII, 0))
        {
            rewind($th);
            $data = stream_get_contents($th);
        }
    }
}

header("Content-Disposition: attachment; filename=$fname");
echo $data;

This way you dont have to save the file locally first 这样,您不必先在本地保存文件

tempnam() 的tempnam()

When I used tempnam() it changed the file extension to .tmp . 当我使用tempnam()它将文件扩展名更改为.tmp This may cause problems, so I don't recommend doing it this way. 这可能会导致问题,所以我不建议您这样做。 At the very least, you should change the extension back to what it was in the original filename. 至少,您应该将扩展名更改回原始文件名中的扩展名。

You can achieve this using pathinfo() in conjunction with regexp and rename() , after you've ftp'ed the file. 您可以在对文件进行ftp操作之后rename() pathinfo()regexp和named pathinfo()结合使用来实现此目的。

mimetype MIME类型

Regarding mimetype: You only need this information if you want the browser to display the file you send to the user. 关于模仿类型:仅当您希望浏览器显示发送给用户的文件时,才需要此信息。 For downloads, the browser only needs to know it's a binary file, and the browser or user can guess from the extension. 对于下载,浏览器只需要知道它是一个二进制文件,浏览器或用户就可以从扩展名中猜测。

If you still want to check the mimetype, you'll have to wait until the file is on your server, so the mime-checking code will go inside the if(ftp_get()) block. 如果仍然要检查mimetype,则必须等到文件在服务器上之后才能将mime检查代码if(ftp_get())块中。

ftp FTP

You haven't shown your connection code (so I'm assuming), but you will need the full path for the file you're pulling from FTP. 您没有显示连接代码(所以我假设),但是您需要从FTP中提取文件的完整路径 If the ftp_get() step is failing, you're probably requesting an invalid file. 如果ftp_get()步骤失败,则可能是您请求的文件无效。

Otherwise, you will have to ftp_chdir() after connecting and logging in so you're in the correct remote folder. 否则, 连接并登录 ,您将必须使用ftp_chdir() ,因此您位于正确的远程文件夹中。

code

This code worked for me: 这段代码对我有用:

<?php
$ftp = ftp_connect('server');
ftp_login($ftp, 'username', 'password');
$file = '/media.banzaimonkey.net/images/forums/boasas_banner_07.gif';

$tempFile = tempnam("/tmp", "FOO");

if(ftp_get($ftp, $tempFile, $file, FTP_BINARY)){
    echo 'success!';
} else {
    echo 'ffffail';
}
?>

If that code doesn't work for you, you may have a problem with permissions, configuration, or some other environment-specific issue. 如果该代码对您不起作用,则可能是权限,配置或其他特定于环境的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM