PHP:从ftp-server下载任何文件到harddrive? [英] PHP: download any file from ftp-server to harddrive?

查看:102
本文介绍了PHP:从ftp-server下载任何文件到harddrive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的真的需要你的帮助。我正在成功建立与我的ftp服务器的连接。但是我无法弄清楚如何从我的服务器下载动态文件。我想这会帮助很多其他人,因为我无法在网路上任何地方找到教程或解释。



我只是列出我的所有我的ftp文件如果我点击一个我调用download.php再次连接到服务器,应该将文件下载到我的硬盘。我能够自动提示一个下载窗口,一个文件被下载到我的hdd。但是只有一个损坏的文件没有标题设置。

  / * ftp服务器上的文件路径...例如/folder/folder/name_of_file.jpg * / 
$ p_arr = explode(/,$ path);
/ *文件名...例如name_of_file.jpg * /
$ file = end($ p_arr);

// $ finfo = finfo_open(FILEINFO_MIME_TYPE); //返回一个致命错误 - 未找到函数
// $ mimetype = finfo_file($ finfo,$ file);
// finfo_close($ finfo);

// filetype($ file)

/ *创建一个temporyry文件保存到* /
$ tempFile = tempnam(/ tmp,FOO );

if(ftp_get($ conn_id,$ tempFile,$ file,FTP_BINARY)){
/ * header内容类型:必须是动态的* /
// header内容类型:'。$ mimetype);

/ *头到auto_prompt下载窗口* /
头('Content-Disposition:attachment; filename ='$ file。'');
readfile($ tempFile);
} else {
echo有一个问题< br>;
echo $ file。 <峰; br> 中; //例如。 image.jpg
}

/ *在进程后删除文件* /
// unlink($ tempFile);

1。)我无法弄清楚为什么ftp_get返回false。连接被设置,文件存在于正确的目录中。
2.)我不知道我如何找到服务器上的文件的mimetype,并给它下载的文件,所以它没有损坏。



<请帮助我在这里,我真的卡住了。感谢您提前

解决方案

尝试在本地先保存,然后将其推回浏览器。



使用该代码在本地保存文件。

 <?php 
//定义一些变量
$ folder_path =你的文件夹路径
$ local_file =本地文件路径;
$ server_file =SERVER FILE PATH;

// - 连接设置
$ ftp_server =IP ADDRESS; // FTP服务器的地址
$ ftp_user_name =USERNAME; //用户名
$ ftp_user_pass =PASSWORD; // Password
#$ destination_file =FILEPATH;

//设置基本连接
$ conn_id = ftp_connect($ ftp_server);

//用用户名和密码登录
$ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass);

//尝试下载$ server_file并保存到$ local_file
if(ftp_get($ conn_id,$ local_file,$ server_file,FTP_BINARY)){
echo成功写入$ local_file\\\
;
} else {
echo有问题\
}

//关闭连接
ftp_close($ conn_id);
?>

我也发现这个,也许可以帮助你


我的FTP服务器总是响应
bool(false),而不是呈现
a目录列表。我不得不添加
ftp_pasv($ conn_id,true);在
之后$ login_result = ftp_login(...);
行。之后,它工作正常。



i really really need your help. I'm successfully setting up a connection to my ftp server. 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. If i click on one i call download.php which connects again to the server and should download the file to my harddrive. 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. 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.

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. I had to add ftp_pasv($conn_id, true); right after the $login_result = ftp_login(...); line. After that it just worked fine.

这篇关于PHP:从ftp-server下载任何文件到harddrive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆