Jump to content

[SOLVED] Search for a File & Extension


ballouta

Recommended Posts

Hello

 

I searched the site here for a PHP code that searches for a file in a directory.

I didn't find what i exactly need (http://www.phpfreaks.com/forums/index.php/topic,184175.0.html)

 

The code I need is that I want to show all files found in a specific folder, show the extension separated, then show each file creation date.

 

e.g:

 

File Name           Extension          Creation Date

========         =======          ==========

fileone                .mdl                 5/25/2008

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/107211-solved-search-for-a-file-extension/
Share on other sites

Something like below should get you on the right path

 

if ($handle = opendir('path/to/folder'))
{
while (false != ($file=readdir($handle)))
{

if ( ($file != ".") && ($file != "..") )
{
$array = explode( '.' , $file );

//ABOVE ASSUMES THERE IS ONLY ONE FILE EXTENSION
//WOULD NOT WORK FOR THINGS LIKE file.inc.php

echo "FILENAME:" . $array[0];
echo "EXTENSION:". $array[1];
$created = filectime($file);
echo "CREATED ON: " . date("l, dS F, Y @ h:ia", $created);

//THERE ARE OTHER WAYS YOU CAN FORMAT THE DATE
//IF YOU SEARCH AROUND A BIT
}

}




}





The code is perfect, just had a samll problem

 

i am getting an error on this line:

 

$created = filectime($file);

 

open this link to read the error: http://www.motif-services.com/date2.php

 

when i comment this line, no errors appear:

 

http://www.motif-services.com/date.php

 

Thanks

Hi Skot, i tried your code line, i got the same error:

 

As for jamieh code, as shown in the example, I added that if (if (file_exists($file)) ) and it works, but doesn't show all files modification date!! very strange why?

 

http://www.motif-services.com/date.php

 

<?php

if ($handle = opendir('malek'))
{
while (false != ($file=readdir($handle)))
{

if ( ($file != ".") && ($file != "..") )
{
$array = explode( '.' , $file );

echo "FILENAME:" . $array[0];
echo "<br>";
echo "EXTENSION:". $array[1];
if (file_exists($file)) {echo "<br> was last changed: " . date("F d Y H:i:s.", filectime($file)); }
echo "<br><br><br>";

}
}
}

?>

To get the filename and extension right every time, also when a filename contains more than one dot, use

 

<?php
$file = 'file.name.inc.php';
$info = pathinfo($file);
// $info['extension'] holds 'php'
// $info['filename'] holds 'file.name.inc'
?>

 

Also note that filectime() returns the last modified date. There's no function for the creation date, since "there is no creation time for Unix files in most Unix filesystems".

 

Try filemtime() instead of filectime, and see if that works better.

 

EDIT: And you shouldn't check if the file exists, since it always will. Remember you're scanning the directory for files.

hi thebadbad

 

your right, no need to check if the file exists or not.

As for replacing filectime () with filemtime(), I got the same error

 

But your code is better and short, even though i still have a problem.

 

I wrote:

 

<?php

if ($handle = opendir('malek'))
{
while (false != ($file=readdir($handle)))
{

$info = pathinfo($file);
echo "$info['extension']";
echo "$info['filename']";

           }
}

?>

 

I got this error: http://www.motif-services.com/date2.php

 

Kindly fix it for me.

Got a better solution to slim down the code, use glob(). Whole code (tested, works here without errors):

 

<?php
$dir = 'malek';
$files = glob("$dir/*");
if ($files) {
foreach ($files as $file) {
	$info = pathinfo($file);
	echo
		'Filename: ', $info['filename'],
		'<br />Extension: ', $info['extension'],
		'<br />Last modified: ', date('F d Y H:i:s', filemtime($file)), "<br /><br />\n";
}
} else {
echo 'Error: No files were found.';
}
?>

But to correct your error, use curly brackets, or simply remove the double quotes:

 

<?php

if ($handle = opendir('malek'))
{
while (false != ($file=readdir($handle)))
{

$info = pathinfo($file);
echo $info['extension'];
echo $info['filename'];

           }
}

?>

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.