Check if NFS share is up in PHP

爷,独闯天下 提交于 2020-02-06 05:30:14

问题


I am working on a system that will store uploaded files. The metadata will go into a locally-accessible database, but the files themselves are going to be stored on a remote box via NFS so that PHP can interact with the server as if it was a directory.

I identified an issue that may occur if somebody attempts to upload a file when the NFS server is down or otherwise unavailable, which could cause the script to error out or hang. Obviously we want to avoid this scenario and handle it in a graceful manner, but we aren't sure how we can do this.

We are thinking of a) checking the server at page-display time and ghosting out the file upload portion of the form should the server be down, or b) checking the link before executing move_uploaded_file to store the uploaded document.

Is it possible to do this from within PHP, and if so, how?


回答1:


Checkout http://www.php.net/manual/en/function.stream-set-timeout.php

You could write a simple check that tries to write to the NFS share with a 2 second timeout. If it succeeds, proceed with the move_uploaded_file. If it fails, give the user a graceful error.




回答2:


I don't know what your setup looks like... If you are mounting it, could you use is_writable?

if (!is_writable('/path/to/nfs/share/mount')) {
   die('NFS share is not writable!');
}



回答3:


I'd try to write a small file for real at nfs-mountpoint, if success you're online and can write the posted file.

If not, cache it at webserver-disk for later (automatic) save.




回答4:


Check if you can opendir() the directory?

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        // do your stuff
        closedir($dh);
    }
}
?>


来源:https://stackoverflow.com/questions/4165296/check-if-nfs-share-is-up-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!