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

}




}





Link to comment
Share on other sites

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

}
}
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.';
}
?>

Link to comment
Share on other sites

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'];

           }
}

?>

Link to comment
Share on other sites

And I know why you got those errors - in your method $file contained only the filename + extension; in my method $file contains the whole path "malek/filename.extension", which should be used when checking for the last modified date.

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.