doubu1970 2015-04-13 12:57
浏览 67
已采纳

如何从数据库中回显文件而不使用数组

I am using this script for counting downloads which echos all the files with download counts in the folder "files":

$extension='';
$files_array = array();

$dir_handle = @opendir($directory) or die("There is an error with your file directory!");

while ($file = readdir($dir_handle)) 
{

if($file{0}=='.') continue;
$parts = explode('.',$file);
$extension = strtolower(end($parts));

if($extension == 'php') continue;

$files_array[]=$file;
}

sort($files_array,SORT_STRING);

$file_downloads=array();

$result = mysql_query("SELECT * FROM download_manager");

if(mysql_num_rows($result))
while($row=mysql_fetch_assoc($result))
{
$file_downloads[$row['filename']]=$row['downloads'];
}

The files are displayed on this way:

foreach($files_array as $key=>$val)
    {
        echo '<li><a href="download.php?file='.urlencode($val).'">'.$val.' 
                <span class="download-count" title="Times Downloaded">'.(int)$file_downloads[$val].'</span> <span class="download-label">download</span></a>
                </li>';
    }

How do i have to modify the script so that it does not echo the complete array but, lets say only: file1.zip, file2.zip and file3.zip?

Update Example of output how it is now: Left the filename, in the middle the counts of download and on the right the link to download. The foreach displays all the 8 files in the folder.

Files  Count
a.zip   2   download
b.zip   1   download
c.zip   1   download
d.zip   0   download
e.zip   0   download
f.zip   0   download
g.zip   0   download
h.zip   0   download

Now i want to manually choose the files which should be displayed:

Files  Count
a.zip   2   download
d.zip   1   download
g.zip   1   download
// skip the other files
  • 写回答

2条回答 默认 最新

  • duannuo7878 2015-04-13 13:52
    关注

    I'm assuming since you just made up filenames there isn't a pattern to which files you want to display, so you'll just have to make basically a whitelist. You can do something like:

    $whitelist = array('a.zip', 'd.zip', 'g.zip');
    foreach($files_array as $key=>$val)
    {
        if(in_array($val, $whitelist) {
            echo '<li><a href="download.php?file='.urlencode($val).'">'.$val.' 
                <span class="download-count" title="Times Downloaded">'.(int)$file_downloads[$val].'</span> <span class="download-label">download</span></a>
                </li>';
        }
    }
    

    Basically you are just listing all the files you want to allow to be output in the $whitelist array, and it will check if $val matches any of those files in the whitelist before it can be display.

    Now if there is a pattern to which files you want to output, then there are better solutions than whitelisting (which is why I requested an example), but assuming there isn't, your only choice is to basically specify the files you want to allow.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号