好的,最近我一直致力于为我的网站开发代码,使已登录的用户能够下载服务器上的文件。我让用户登录正常。我的问题是让ftp_fget()函数工作。我不仅尝试了ftp_fget(),还尝试了ftp_get()amd 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"> </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);
此致
托马斯
答案 0 :(得分:0)
路径名称错误。如果在连接后没有指定路径,它将在基本目录中查找。您可以使用ftp_chdir
切换到目录,也可以添加您尝试下载的文件的路径(即'./folder/subfolder/file.jpg');
此外,仅从查看脚本,根据您的配置方式,可能存在巨大的安全漏洞。您信任用户安全地输入$_GET['file']
,这不是最聪明的事情。您可能希望将文件限制到某些目录(除非您连接到文件服务器,并且确实希望它们能够下载任何内容)。