|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-05-23 14:34 UTC] rkenny at mke dot catalystwms dot com
Hello, I've written a simple script which can recurse into subdirectories when needed, and print out an HTML table with with details about the files it finds. The problem is that after I run this script, if I try and rename one of the folders it has recursed into, I get an error message which reads:
"Cannot rename [folder name]: There has been a sharing violation. The source or destination file may be in use."
Note that I can still rename files, just not folders. If I stop and start IIS, I am able to rename folders until I run the script again. I believe I'm using the closedir() function correctly, and have even echoed out when the directories are opened and closed to make sure I'm not messing up. One last thing to note is that this script is being run on an NTFS partition. A copy of the script follows:
<?php
// $hddir is the hard drive directory passed into this function from the calling page
// $mode can be either "single" or "recurse"
// $order is just ignored right now, but I would like it to be "alphabetical", "size", etc.
function list_dir($hddir, $mode, $order) {
$webdir = "http://www.foo.com" . str_replace("/inetpub/www_root", "", $hddir);
$handle = opendir($hddir);
chdir($hddir);
// if there are files in the directory, print out the table header and set the flag
// once we find a file, don't run the test (and print out the table header) again
while (($file = readdir($handle)) && ($file_count < 1)) {
if ($file == '.' || $file== '..')
continue;
else if (is_file($file)) {
echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";
echo "<tr><th></th><th class=\"high\">Filename</th>";
echo "<th class=\"high\">Size</th>";
echo "<th class=\"high\">Modified</th></tr>";
$file_count += 1;
}
}
rewinddir ($handle); // start reading again from the beginning of the directory
while ($file = readdir($handle)) { // while we can read an entry from the directory (files and dirs)
if ($file == '.' || $file== '..')
continue;
else if (($mode == "recurse") && (is_dir($file)) && (substr("$file", 0, 1) != "_")) { // don't recurse directories which begin with an underscore
$dir_count += 1;
$places_to_go[$dir_count] = "$file";
}
else if (is_file($file)) {
$extension = '';
$parts = split('\.', $file);
if (count($parts) > 1) $extension = end($parts);
if (!$extension && count($parts) > 2) $extension = prev($parts);
if (($extension == "doc") || ($extension == "DOC") || ($extension == "rtf"))
echo "<tr><td><img src=\"/pics/icons/word_sm.gif\" width=\"20\" height=\"19\"></td>";
else
echo "<tr><td><img src=\"/pics/spacer.gif\" width=\"20\" height=\"19\"></td>";
echo "<td bgcolor=\"silver\"><a href=\"" . $webdir . "/" . $file . "\" target=\"_blank\">" . $file . "</a></td>";
$j = 0;
$ext = array(" Bytes", " KB", " MB", " GB", " TB");
$file_size = filesize($file);
while ($file_size >= pow(1024,$j)) ++$j;
$file_size = round($file_size / pow(1024,$j-1) * 100) / 100 . $ext[$j-1];
echo "<td bgcolor=\"silver\">" . $file_size . "</td>";
$filemod = filemtime($file);
$file_modtime = date("F j Y h:i:s A", $filemod);
echo "<td bgcolor=\"silver\">" . $file_modtime . "</td></tr>";
}
} // end while we're reading files from the directory
// if we printed out a table, header (and contents) we should cap it off
if ($file_count >= 1)
echo "</table>";
// if there are other directories to recurse into that were stored in our array,
// print out the name of the directory, and call this function with the new directory name.
if ($dir_count >= 1) {
for ($x=1; $x<=$dir_count; $x++) {
echo "<h4>$places_to_go[$x]</h4>";
echo "<dir>";
$newdir = "$hddir" . "/$places_to_go[$x]";
list_dir($newdir, "recurse", "alpha");
echo "</dir>";
}
}
chdir ("..");
closedir($handle);
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 18:00:01 2025 UTC |
Hi, I have the same problem. I think it may come from rewinddir. I've written a script that first checks is there is at least one file in a given directory. It opens the directory and stores the directory handle into a variable. Further in the same page, the script uses this handle again to gets informations on the files in the directory, so I use rewinddir on it. Then, the function rewind the directory handle but a new one appears (I cheched it with nthandle) Here is a script that I think produces the same effect: <?php $hDir=opendir('c:\web\project1\docs\BCD\Status'); if($hDir){ for($i=0;$i<10;$i++){ while(($file=readdir($hDir))!==false){ echo "$file<br>\n"; } rewinddir($hDir); } closedir($hDir); } ?> then there are still 10 opened handles