Jump to content

Recommended Posts

I have this bit of code that is checking free/used space on a network server. As long as I have permissions I get a valid response. However, if I don't have access or the path is not correct I get a hard error that the network path was not found:

Warning: disk_total_space() [function.disk-total-space]: The network path was not found. in C:\xampp\xampp\htdocs\test\dir\drivespace2.php on line 5

 

Warning: disk_free_space() [function.disk-free-space]: The network path was not found. in C:\xampp\xampp\htdocs\test\dir\drivespace2.php on line 7

 

Warning: Division by zero in C:\xampp\xampp\htdocs\test\dir\drivespace2.php on line 10

 

Warning: Division by zero in C:\xampp\xampp\htdocs\test\dir\drivespace2.php on line 11

I would like to provide a soft error back that indicates that the problem is either permissions or the network path is not valid instead of the hard error above.

 

Here's the snippet:

 

define("DISK","//netserver/apps/"); 

$total = round(disk_total_space(DISK)/1024/1024,2); 

$free = round(disk_free_space(DISK)/1024/1024,2);
$used = $total - $free; 

$used = round(($used/$total)*100); 
$free = round(($free/$total)*100); 

if ($free < 10) {
//echo ("Bad");
echo (DISK. " " .'<img src="red.png" alt="There is less than '. $free .'% free space at '. DISK .' Attention Required!">');

} else {
//echo ("Good");
echo (DISK. ' ' .'<img src="green.png" alt="There is '. $free .'% free space at '. DISK .'">');
}

 

Any ideas?

 

 

Thanks!

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/63946-solved-soft-error-vs-hard-error/
Share on other sites

you could probably get around the warning by suppressing the error using the @ operator, and then checking the return value.  you'd want to add an if() statement to avoid the division by zero operation if you have no permission to access the path.

Using @ and die would give you a simple universal error for path not found AND bad permissions, but you couldn't discern between the two. I might be wrong, but I don't think that php even looks at permissions. It simply can or cannot access the path, whether it is because it doesn't have permissions or the path doesn't exist isn't even looked at. I know that there are functions that allow you to look at the permissions of a file or folder, but I doubt that would work if you don't have access to them.

Thanks for the info, I am using the @ operator and a "or print" option which works nicely. I also added an IF to handle the division by zero.

 

$total = @round(disk_total_space(DISK)/1024/1024,2) or print '<img src="grey.png" alt="'.DISK.' is not accessible.">';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.