Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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);
    }
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top