繁体   English   中英

从FTP服务器下载文件-Ftp_fget警告没有这样的文件或目录

[英]Downloading Files from FTP server - Ftp_fget Warning No such file or directory

好的,最近我一直在为我的网站编写代码,以使已登录的用户能够下载服务器上的文件。 我的用户登录正常。 我的问题是使ftp_fget()函数正常工作。 我不仅尝试了ftp_fget(),还尝试了ftp_get()和ftp_nb_get()。

我现在如何设置它是在一页上显示某个目录中的所有文件。 然后我添加了这个

<a href="Replays/sc2_replays/ftp_download.php?file=<? echo "$filename" ?>"><input type="image" src="images/dl_icon.png" width="41" height="41"/></a>

单击后,会将用户带到ftp_download.php页面,该页面用于下载所选文件。

这是我在ftp_download页面上获得的内容。

<?=
 $conn_id = ftp_connect("thomassawkins.hostoi.com","21") or die("could not connect");
 $ftp_login = ftp_login($conn_id,"USERNAME", "PASSWORD");
 $remote_file = $_GET['file'];
 $local_file = fopen("$remote_file",'w');

 ftp_pasv($conn_id, true); 
    if(!$ftp_login)
    {
        echo "could not login";
    }
    else
    {

        if (ftp_fget($conn_id, $local_file, $remote_file, FTP_BINARY)){
            echo "Successfully written to $local_file\n"; 
            } else {
            echo "There was a problem\n"; 
            }
    }

    ftp_close($conn);
     ?>

当我单击文件的下载按钮时,在定向到下载页面后出现此错误

Warning: ftp_fget() [function.ftp-fget]: Can't open sc2 test - 2.txt: No such file or directory in /home/a5015247/public_html/Replays/sc2_replays/ftp_download.php on line 15

sc2 test-2.txt是我要下载的测试文件。 它保存在目录/ home / a5015247 / public_html / Replays / sc2_replays /

我要总体上实现的目标是让用户单击所需的文件进行下载,然后让它提示用户他们要在计算机上保存文件的位置。

任何解决我的问题的帮助将不胜感激。

问候,

汤玛士

--edit--这是显示指定目录中所有文件的代码。

<?php
   $startdir = 'Replays/sc2_replays';
   $showthumbnails = false; 
   $showdirs = true;
   $forcedownloads = false;
   $hide = array(
            'dlf',
            'public_html',              
            'index.php',
            'Thumbs',
            '.htaccess',
            '.htpasswd'
        );
   $displayindex = false;
   $allowuploads = false;
   $overwrite = false;

   $indexfiles = array (
            'index.html',
            'index.htm',
            'default.htm',
            'default.html'
        );

          $filetypes = array (
            'png' => 'jpg.gif',
            'jpeg' => 'jpg.gif',
            'bmp' => 'jpg.gif',
            'jpg' => 'jpg.gif', 
            'gif' => 'gif.gif',
            'zip' => 'archive.png',
            'rar' => 'archive.png',
            'exe' => 'exe.gif',
            'setup' => 'setup.gif',
            'txt' => 'text.png',
            'htm' => 'html.gif',
            'html' => 'html.gif',
            'php' => 'php.gif',             
            'fla' => 'fla.gif',
            'swf' => 'swf.gif',
            'xls' => 'xls.gif',
            'doc' => 'doc.gif',
            'sig' => 'sig.gif',
            'fh10' => 'fh10.gif',
            'pdf' => 'pdf.gif',
            'psd' => 'psd.gif',
            'rm' => 'real.gif',
            'mpg' => 'video.gif',
            'mpeg' => 'video.gif',
            'mov' => 'video2.gif',
            'avi' => 'video.gif',
            'eps' => 'eps.gif',
            'gz' => 'archive.png',
            'asc' => 'sig.gif',
        );

         error_reporting(0);
         if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
         $leadon = $startdir;
         if($leadon=='Replays/sc2_replays') $leadon = '';
         if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
         $startdir = $leadon;


    if($_GET['dir']) {
//check this is okay.

if(substr($_GET['dir'], -1, 1)!='/') {
    $_GET['dir'] = $_GET['dir'] . '/';
}


$dirok = true;
$dirnames = split('/', $_GET['dir']);
for($di=0; $di<sizeof($dirnames); $di++) {


    if($di<(sizeof($dirnames)-2)) {
        $dotdotdir = $dotdotdir . $dirnames[$di] . '/';
    }


    if($dirnames[$di] == '..') {
        $dirok = false;
    }
}


if(substr($_GET['dir'], 0, 1)=='/') {
    $dirok = false;
}


if($dirok) {
     $leadon = $leadon . $_GET['dir'];
}
    }


    $opendir = $leadon;
    if(!$leadon) $opendir = 'Replays/sc2_replays/';
    if(!file_exists($opendir)) {
$opendir = 'Replays/sc2_replays/';
$leadon = $startdir;
    }

    clearstatcache();
    if ($handle = opendir($opendir)) {
while (false !== ($file = readdir($handle))) { 
    //first see if this file is required in the listing
    if ($file == "." || $file == "..")  continue;
    $discard = false;
    for($hi=0;$hi<sizeof($hide);$hi++) {
        if(strpos($file, $hide[$hi])!==false) {
            $discard = true;
        }
    }

    if($discard) continue;
    if (@filetype($leadon.$file) == "dir") {
        if(!$showdirs) continue;    

        $n++;
        if($_GET['sort']=="date") {
            $key = @filemtime($leadon.$file) . ".$n";
        }
        else {
            $key = $n;
        }
        $dirs[$key] = $file . "/";
    }
    else {
        $n++;
        if($_GET['sort']=="date") {
            $key = @filemtime($leadon.$file) . ".$n";
        }
        elseif($_GET['sort']=="size") {
            $key = @filesize($leadon.$file) . ".$n";
        }
        else {
            $key = $n;
        }
        $files[$key] = $file;           

        if($displayindex) {
            if(in_array(strtolower($file), $indexfiles)) {
                header("Location: $file");
                die();
            }
        }
    }
}
closedir($handle); 
    }

    //sort our files
    if($_GET['sort']=="date") {
@ksort($dirs, SORT_NUMERIC);
@ksort($files, SORT_NUMERIC);
     }
    elseif($_GET['sort']=="size") {
@natcasesort($dirs); 
@ksort($files, SORT_NUMERIC);
   }
    else {
@natcasesort($dirs); 
@natcasesort($files);
    }

   //order correctly
   if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
   if($_GET['order']=="desc") {$files = @array_reverse($files);}
   $dirs = @array_values($dirs); $files = @array_values($files);

   ?>
   <div id="listingcontainer">
    <div id="listingheader"> 
<div id="headerfile"></div> 
<div id="headersize"></div>
<div id="headermodified"></div>
    </div>
       <div id="listing">
        <?
       $class = 'b';
       if($dirok) {
?>
  <div><a href="<?=$dotdotdir;?>" class="<?=$class;?>"><img src="http://www.000webhost.com/images/index/dirup.png" alt="Folder" /><strong>..</strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div>

<?
    if($class=='b') $class='w';
    else $class = 'b';
}
$arsize = sizeof($dirs);
for($i=0;$i<$arsize;$i++) {
?>
<div><a href="<?=$leadon.$dirs[$i];?>" class="<?=$class;?>"><img             src="http://www.000webhost.com/images/index/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div>
<?
    if($class=='b') $class='w';
    else $class = 'b';  
}


$arsize = sizeof($files);
for($i=0;$i<$arsize;$i++) {
    $icon = 'unknown.png';
    $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
    $supportedimages = array('gif', 'png', 'jpeg', 'jpg');
    $thumb = '';


    if($filetypes[$ext]) {
        $icon = $filetypes[$ext];
    }

    $filename = $files[$i];
    if(strlen($filename)>43) {
        $filename = substr($files[$i], 0, 40) . '...';
    }

    $fileurl = $leadon . $files[$i];
?>
<div>
  <table width="574" border="0.5" align="center">
    <tr>
      <th width="59" align="center" valign="middle" scope="col">&nbsp;</th>
      <th width="136" align="center" valign="middle" scope="col"><img src="http://www.000webhost.com/images/index/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$filename;?></strong>
      </a></th>
      <th width="101" align="center" valign="middle" scope="col"><em>
        <?=round(filesize($leadon.$files[$i])/1024);?>
        KB</em></a></th>
      <th width="186" align="center" valign="middle" scope="col">
        <?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?>
        </a></th>
        <th width="70" align="right" valign="middle" scope="col">
      <a href="ftp_download.php?filedir=<? echo "./$opendir"?>"><input type="image" src="images/dl_icon.png" width="41" height="41"/></a>

        </a></th>
    </tr>
  </table> 
</div>
<?
    if($class=='b') $class='w';
    else $class = 'b';  
}   
?></div>

当用户单击此处的图标时会发生什么

<a href="ftp_download.php?filedir=<? echo "./$opendir"?>"><input type="image" src="images/dl_icon.png" width="41" height="41"/></a>

然后将它们重定向到download.php,该文件将处理其中的下载部分。 目前,由于某种原因,我什至无法使用chdir成功更改目录。 我得到的错误是

Warning: ftp_chdir() [function.ftp-chdir]: Can't change directory to /Replays/sc2_replays/: No such file or directory in /home/a5015247/public_html/ftp_download.php on line 15

我尝试下载的文件dumby文件位于目录public_html / Replays / sc2_replay / dumby.txt中。

我也使用ftp_pwd来查找出现此错误并输出“ /”时我当前所在的目录。 我不确定那是什么意思

ftp_chdir($conn_id, "/Replays/sc2_replays/");
        echo ftp_pwd($conn_id);

问候,

汤玛士

路径名错误。 如果您在连接后未指定路径,它将在基本目录中查找。 您可以使用ftp_chdir切换到目录,也可以在要下载的文件之前添加路径(即“ ./folder/subfolder/file.jpg”);

另外,仅查看脚本,具体取决于配置方式,就可能存在巨大的安全漏洞。 您信任用户安全地输入$_GET['file'] ,这不是最明智的选择。 您可能希望将文件限制在某些目录中(除非您要连接到文件服务器,并且确实希望它们能够下载任何内容)。

暂无
暂无

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

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