0

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
nuet maessen
  • 131
  • 10
  • Im confused. What do you mean by "echo the complete array"? – Erick Apr 13 '15 at 12:59
  • Add a WHERE clause to your query? – andrewsi Apr 13 '15 at 13:00
  • You need to explain what criterion you are trying to match against, and what undesirable results are occurring. For instance, if you're trying to weed out `.htaccess` and only display `.zip` archive files, you would need to say that. Just saying you want those specific files means very little, unless you're also trying to search by name... – Josh Apr 13 '15 at 13:01
  • What i want to achieve: the foreach displays all the files in the folder. What i want is to display only some files, which i want to choose myself. but i do not know how to do that – nuet maessen Apr 13 '15 at 13:04
  • Can you post an example output snippet of what it currently output (that includes both files you want to include and those you want to exclude)? – cwurtz Apr 13 '15 at 13:12
  • Post above updated: example – nuet maessen Apr 13 '15 at 13:30
  • You might want to take a look at [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/q/12859942/214577) – Oldskool Apr 13 '15 at 13:56

2 Answers2

1

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.

cwurtz
  • 3,177
  • 1
  • 15
  • 15
1

For the above result you can modify the query by adding a where condition (assuming count is stored in database).

$result = mysql_query("SELECT * FROM download_manager WHERE count_field > 0"); 

You can also restrict it in the foreach loop using an if condition.

foreach($files_array as $key=>$val)
    {
        $count = (int)$file_downloads[$val];
        if($count >0)
        {
            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>';
        }
    }
Syam
  • 409
  • 5
  • 8