开发者

Check if NFS share is up in PHP

开发者 https://www.devze.com 2023-01-24 14:12 出处:网络
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

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 serve开发者_运维问答r 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?


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.


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!');
}


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.


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);
    }
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消