Check if NFS share is up in PHP

2019-08-04 06:42发布

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?

4条回答
Luminary・发光体
2楼-- · 2019-08-04 07:01

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);
    }
}
?>
查看更多
可以哭但决不认输i
3楼-- · 2019-08-04 07:07

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.

查看更多
疯言疯语
4楼-- · 2019-08-04 07:07

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.

查看更多
女痞
5楼-- · 2019-08-04 07:11

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!');
}
查看更多
登录 后发表回答