Jump to content

[SOLVED] Limiting an Output


jonoc33

Recommended Posts

Hi guys,

I have a script which lists a bunch of files in a folder:

<?php
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while (false !== ($file = readdir($handle)))
{
    if ($file != '.' && $file != '..')
       $files[] = $file; 
} 
closedir($handle); 

#echo the files 
foreach ($files as $file) { 

    echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
?>

How exactly would I limit this to only show 5 of the most recent files, and if thats not possible, just 5 files in the folder.

Link to comment
https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/
Share on other sites

You will need to add a counter into your while loop.

<?php
$i=0;
if($i<5)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 
$i++;
} 
}
else
{
closedir($handle); 

#echo the files 
foreach ($files as $file) { 

   echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
}

?>

not 100% sure if that will work

I got no output.

Using this code:

<?php 
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

$i=0;
if($i<5)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 
$i++;
} 
}
else
{
closedir($handle); 

#echo the files 
foreach ($files as $file) { 

#160; &#160; echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
}

?>

try this

<?php 
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

$i=0;
if($i<5)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 
$i++;
} 

#echo the files 
foreach ($files as $file) { 
echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}

}
else
{
closedir($handle); 
}

?>

Add this to the top of the php and tell me any errors

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

 

edit....

 

Also try this

 

<?php 
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

$i=0;
if($i<5)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 
$i++;
} 
}

#echo the files 
foreach ($files as $file) 
{ 
echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
else
{
closedir($handle); 
}
?>

Try

 

<?php 
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

$i=0;
while($i<5)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 

} 
$i++;
}

#echo the files 
foreach ($files as $file) 
{ 
echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
else
{
closedir($handle); 
}
?>

Try

 

<?php 
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

$i=0;
while($i<5)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 

} 
$i++;
}

#echo the files 
foreach ($files as $file) 
{ 
echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
else
{
closedir($handle); 
}
?>

 

wrong .... i guess

<?php 
$folder = FILE_FOLDER; 
$handle = opendir($folder); 

$i=0;
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
$files[] = $file; 
$i++;
if ($i == 5){break;}//-----------------------see this
}

#echo the files 
foreach ($files as $file) 
{ 
echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; 
}
if($files == 0){
echo "<br />";
}
else
{
closedir($handle); 
}
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.