Problem in textfile cannot be deleted

Hi, I am creating a textfile through batch file,when my textfile is created and some data on it,and when i stoped the batch file and trying to delete the textfile or renamed on it.

it shows this error “The action cannot be completed because the file is open in php.exe”

how can i solved this problem?

Thank you in advance.

That’s because it hasn’t been closed for whatever reason. Do you explicitly close the file once you’ve created and finished with it? Can you show the code?

Here is the code.




$address = 'xxx.xxx.x.xx';//server ip
$port = xxxx;//port

   

    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

    socket_bind($sock, $address, $port);

    socket_listen($sock);

    $clients = array($sock);
  
    while (true) {
	 $file = fopen('mytxt.log','a'); 
  
        $read = $clients;
        $write = NULL;
        $except = NULL;
	    $tv_sec = 0; 
   
		
        if (socket_select($read, $write , $except,  $tv_sec) < 1)
            continue;
       
      
        if (in_array($sock, $read)) {
    
            $clients[] = $newsock = socket_accept($sock);
        
            $key = array_search($sock, $read);
            unset($read[$key]);
        }
       
       
        foreach ($read as $read_sock) {
            $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);
           
            // check if the client is disconnected
            if ($data === false) {
                // remove client for $clients array
                $key = array_search($read_sock, $clients);
                unset($clients[$key]);
       
                continue;
            }
           
         
            $data = trim($data);
           
            if (!empty($data)) {
				 fwrite($file,$data."\
");
            }
           
        } // end of reading foreach
		
		fclose($file);
    }

    socket_close($sock);
?>