When a user logs into my site...a temporary directory is created on MY server, in the 'c:\windows\temp\' directory

the dir is created with 0770 mode.

when the user logs out i want to empty the folder and remove the directory

this is my attempt:

create temp dir (works fine and all other references to this are ie i can create files in this dir)

if(!is_dir($_SESSION['usrdir'] = "c:/windows/temp/".$_SESSION["username"]."_temporary/")){
			mkdir($_SESSION['usrdir'],0770);
		}

remove the temp dir and any files inside

	if(is_dir($_SESSION['usrdir'] = "c:/windows/temp/".$_SESSION["username"]."_temporary/")){
		//get temp file list
		$count=0;
		if ($Handle = opendir($_SESSION['usrdir'])) {
			while (false !== ($file = readdir($Handle))) {
				if(($file != '.') AND ($file != '..')){
					$List[$count] = $_SESSION['usrdir'].$file;
					$count +=1;
				}
			}
			closedir($Handle);
		}
		//unlink(remove) files in list
		for($x=0;$x<$count;$x++){
			unlink($List[$x]);
		}
		//remove user temp directory
		rmdir($_SESSION['usrdir']);
	}

I am using IIS 5.x on an xp box using PHP 5.0.4

Does anyone have an idea where it is wrong?

    if (file_exists($_SESSION['usrdir']))
    {
    	$handle = opendir($_SESSION['usrdir']) or exit('could not open directory');
    	while (($file = readdir($handle)) !== false)
    	{
    		if ($file != '.' && $file != '..')
    		{
    			unlink($_SESSION['usrdir'] . $file) or exit('could not delete file');
    		} 
    	}
    	rmdir($_SESSION['usrdir']) or exit('could not delete directory');
    }
    

      Besides that, how will you know that a user has actually logged out when she doesnt even click on "logout"? If she simply closes the window?
      Will it be an issue here? :/

        Im using sessions...therefore if they accidently close the window they are still logged in. Therefore they still have access to the temp dir when they enter the application at a later point in the day. They temp dir wont be removed until they have gone throught the logout procedure.

          But when their browser was closed it would have lost the session ID it had been holding.

            My applications open in separate windows....if the main window is closed then yes they will lose session data....but not necessarily the temporary data dir if the windows are closed due client accident or client browser is closed for some reason...Users logout when they have finished theyre session.

            The temp files are used for object serialisation rather being a store for user specific data...anything like that could be done using cookies (a 'remember me'). The objects that a get serialised hold data that we dont want to let other people have on their computers and stored for access later...The site isnt going to be a public site on the web... to start with there are only going to be at most 50 possible people using the site and only companies who deal with our compnay.

              I am still trying to work out why i cant delete the temp dir...Isnt doesnt recognise the newly created dir as a dir.

              I have been trying this:

              session_start();
              
              $dir ="c:/windows/temp/edwardp_temporary/";
              echo $dir.var_dump(is_dir($dir))."<br>";
              
              $dir2 = "c:/windows/temp/";
              echo $dir2.var_dump(is_dir($dir2));
              
              if (is_dir($_SESSION['usrdir'])) { 
              	$handle = opendir($_SESSION['usrdir']) or exit('could not open directory'); 
              	while (($file = readdir($handle)) !== false) { 
              		if ($file != '.' && $file != '..') { 
              			unlink($_SESSION['usrdir'] . $file) or exit('could not delete file'); 
              		}  
              	} 
              	rmdir($_SESSION['usrdir']) or exit('could not delete directory'); 
              }  
              session_destroy();
              

              its the lines after session_start() which i am troubled over.

              as the first says i am creating a temp dir (checking first it doesnt exits). then create the dir with the username as part of it. Assuming a correct login....with username edwardp i should get the following directory created:

                   c:/windows/temp/edwardp_temporary
              

              this works

              now if i logout and run the above script (this post) i get this result:

              bool(false) c:/windows/temp/edwardp_temporary/
              bool(true) c:/windows/temp/

              and thefore if it cant read my created dir as a directory then obviusly the rest fails to execute.

              I hope this made sense

                Write a Reply...