Society 2007 - Debt Consolidation - Free Credit Report - Debt Consolidation - Debt Consolidation

PDA

View Full Version : Handling file-read failure


erdubya
Jan 2nd 2007, 12:26 pm
I'm kinda new with php, but I'm dying to know how to do the following. But can't seem to get the syntax right. Here's the pseudo code...

Page Refresh = 30 secs
--Read file-a.txt
--file-a.txt not available?
----read file-b.txt
----file-b not available?
------do nothing... (page will refresh in 30 secs and repeat the process)


The problem:
=======================
I have a lot of visitors accessing my site and a lot of times the page becomes inaccessible if it refreshes at just the right (wrong) time while it's being read/written to. Instead of the page locking up or giving a server error, I want to be able to try to read a secondary file as a backup.

How can I achieve the logic above? (Check to see if a file is available to be read or not.)




Thanks for your help!

Elliott

ZareMedia
Jan 2nd 2007, 1:24 pm
$data = @file_get_contents('fileA.txt');

if ($data==FALSE)
$data = @file_get_contents('fileB.txt');

echo $data;


The @ symbol suppresses errors so if the file isn't found, it won't give away your internal file names to your visitors. You could also do more elegant error checking by check if file_exists('filename.txt') before attempt to read it. Either way same functionality.

If the second file fails then $data is going to be false... which when echo'd simply displays nothing. I assume before this you'd be outputting a <meta> tag to handle the page-refresh

erdubya
Jan 2nd 2007, 1:51 pm
ZareMedia...

Thanks... That's exactly what I was looking for!

I'll give it a try tonight.

Chemo
Jan 2nd 2007, 8:09 pm
Never...ever...ever use @ to suppress errors. It makes your code very hard to debug and also virtually impossible to implement proper exception handling later.


<?php
/**
* Short example for erdubya (Digital Point forum member)
*
* http://forums.digitalpoint.com/showthread.php?t=210157
*
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0
* @link http://www.oscommerce-freelancers.com/ osCommerce-Freelancers
* @copyright Copyright 2007, Bobby Easland
* @author Bobby Easland
* @filesource
*/

// Define the base file path
define('FILE_FS_PATH', '/your/path/to/the/files/');

// Define the files
define('FILE_A', FILE_FS_PATH . 'file-a.txt');
define('FILE_B', FILE_FS_PATH . 'file-b.txt');
define('FILE_C', FILE_FS_PATH . 'file-c.txt');

// Switch to ensure the file is readable
switch(true){
case is_readable(FILE_A):
$fileRead = file_get_contents(FILE_A);
break;
case is_readable(FILE_B):
$fileRead = file_get_contents(FILE_B);
break;
case is_readable(FILE_C):
$fileRead = file_get_contents(FILE_C);
break;
default:
$fileRead = false;
break;
} # end switch

// If none of the files are readable exit
if ( $fileRead === false ){
exit('Sorry...this file is not available');
}

/*
* Do something with the $fileRead var
*/
echo $fileRead;
?>