php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11062 Sharing Violation Error Message
Submitted: 2001-05-23 14:34 UTC Modified: 2001-11-10 08:43 UTC
From: rkenny at mke dot catalystwms dot com Assigned:
Status: Closed Package: Directory function related
PHP Version: 4.0.6 OS: NT 4.0 SP6a
Private report: No CVE-ID: None
 [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); 
} 

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-04 01:27 UTC] rkenny at mke dot catalystwms dot com
In order to regain control over the directories, it is necessary to restart the entire "IIS Admin Service".  Restarting the "World Wide Web Publishing Service" alone does not fix the problem.
 [2001-06-23 20:58 UTC] sniper@php.net
Does this happen with PHP 4.0.6 ?

 [2001-06-28 07:58 UTC] rkenny at mke dot catalystwms dot com
Yes it does.  :(   Does the folder need to have special permissions?  Right now I've got SYSTEM and CREATOR_OWNER with full controll, and a bunch of groups.
 [2001-06-28 19:02 UTC] phildriscoll@php.net
Is there any chance that while testing this you have also caused IIS to serve any of the files in the directories up? There is an unpleasant problem with IIS whereby it holds files open for some time after serving them. This normally manifests itself by stopping you editing and saving out a html file you have just looked at via the web server, however it would also stop you renaming any parent directory of the file.
 [2001-07-03 11:38 UTC] rkenny at mke dot catalystwms dot com
Yes, the purpose of the script is to display the contents of a directory and make them available via the IIS server.  Unfortunately, I need to make use of the NT security model, so switching to apache is not an option (or I would have, a long time ago).  Do you know if this IIS bug was fixed in IIS 5?  
 [2001-07-21 18:22 UTC] andy@php.net
Make sure Windows or any other app has this directory or any
files in it locked (using them.)
 [2001-10-20 20:54 UTC] sniper@php.net
Is this solved by updating IIS ?

 [2001-11-10 08:43 UTC] sander@php.net
No feedback. Closing.
 [2002-02-21 12:23 UTC] jean-marc_andre at hp dot com
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
 [2012-03-23 15:14 UTC] techyvme at gmail dot com
Hi Guys, just to update you, finally I found a way to solve it
Do you have error messages?, like :

* Path too long
* Error cannot delete file: cannot read from source file or disk
* Cannot delete file: Access is denied
* There has been a sharing violation.
* Cannot delete file or folder The file name you specified is not valid or too long. Specify a different file name.

For that I tried it with: http://longpathtool.com/

LEEROY
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Nov 24 16:01:24 2020 UTC