Hi i want to display files only from a folder,how to check if it is file or folder .give any sample code ,it will be very useful to me.
// open the current directory
$dhandle = opendir( "./");
// define an array to hold the files
$files = array();
if ($dhandle) {
// loop through all of the files
while (false !== ($fname = readdir($dhandle))) {
// if the file is not this file, and does not start with a '.' or '..',
// then store it for later display
if (($fname != '.') && ($fname != '..') &&
($fname != basename($_SERVER['PHP_SELF']))) {
// store the filename
$files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
}
}
// close the directory
closedir($dhandle);
}
this code retrive foldernames too. what wrong in it .i want to display the files name only
The following line is why everything is been included in the $files array.
$files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
To retrieve only files the following will work
if (is_file($fname)) {
$files[] = $fname;
}
i tried to delete the file using unlink command but it is not work. is it we have to pass extension in it. its extension is file .
No, the while loop contains the filename + the extension so no further logic is needed, my question to you is have you checked your error log as if unlink() is failing itβs most likely due to a permissions error.
thank you. its path problem i didnt give path in unlink . now it solves