belial Posted March 10, 2009 Share Posted March 10, 2009 Hello there, I hope someone can help me out with this cos it's driving me nuts. I have some media which is on a several remote servers. The servers are sharing the media using SMB (Samba) shares for a reason which I really can't ellaborate on at this stage, suffice to say they have to be SMB). Because of the size of the repositories, media can appear on 1 or more of the servers, but it is not known if the host server, directory or file that is recorded in the DB actually exists until it is tested. You would have thought that a call to file_exists() or is_dir() would do the job, but the in the instance that the file does not exist or the directory does not exist we wait for 60 secs for the function to return false. This really sucks because there are about 5 servers or so to check, which means the user will be waiting up to 5 minutes to be told the media is (or is not) avaiable. I have tried mucking about with the timeout variable in php.ini and also opening sockets with timeouts (which are a little difficult to do since it's a SMB share I'm trying to connect to, not a webserver or ftp) but to no avail. Now sure I could do a ping to the hostname, to check that the server is up, but to make the situation worse some of the SMB shares are not "browsable". This means that even doing a scandir on the host, and slowly traversing tho the correct directory doesn't work reliably either. So I guess my question is: Having tried most of the example code I have found within the PHP online manual, and various other google resources, has anyone else out there successfully altered the timeout for checking the existence of a file stored on a samba share? Thanks in Advance. Jonathan Link to comment https://forums.phpfreaks.com/topic/148754-solved-smb-share-timeout/ Share on other sites More sharing options...
belial Posted March 18, 2009 Author Share Posted March 18, 2009 Ok, so I've sorted this partly. The major obsticle was determining if the host was up. I cannot rely on a simple ICMP ping using system, so resorted to the socket route. I was getting headaches with the steam context garble, and have resolved that the simplest solution is probably the most effective... try to open a socket to the SMB port on the machine in question fsockopen allows for a timeout, and so long as you surpress verbose errors on the connection using @ you get fairly sturdy results. Here try this: <?php function smb_host_exists($hostname) { $timeout_period=2; // in seconds $smb_port=139; $fp=@fsockopen($hostname,$smb_port,$error_number,$error_string,$timeout_period); if($fp) { echo "Connected to ".$hostname." on port ".$smb_port; fclose($fp); } else echo "Failed to connect to ".$hostname." on port ".$smb_port; } smb_host_exists("hostmachine"); ?> You can then use path information to determine if a given file exists, but at least you don't have to wait an eon for the underlying process to timeout. Hope this helps someone else in the future... Link to comment https://forums.phpfreaks.com/topic/148754-solved-smb-share-timeout/#findComment-787634 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.