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
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

Link to comment
Share on other sites

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 />";
}
}

?>

Link to comment
Share on other sites

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); 
}

?>

Link to comment
Share on other sites

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); 
}
?>

Link to comment
Share on other sites

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); 
}
?>

Link to comment
Share on other sites

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); 
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.