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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看