Solved

Website stalling on this code for some reason...

Posted on 2011-02-11
4
355 Views
Last Modified: 2012-05-11
Hi All!  Can anyone see any inherent reason why this code won't work.  The webpage loads up for some reason stalls with the progress bar.  I remember it working when I coded it but now it doesn't.  I checked for an error log and there aren't any recent ones... Thanks!!
<?
$myFile = "prices-web.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
list($avgas, $jet, $fsii, $eff) = split('[=]', $theData);
echo "<br>Current Prices as of $eff<br><br>AVGAS 100LL: \$$avgas + Tax<br>Jet A1: \$$jet + Tax<br>Jet A1 with FSII: \$$fsii + Tax";
?>

Open in new window

0
Comment
Question by:zack4x4
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:ropenner
ropenner earned 200 total points
ID: 34876402
Possible problems depending on the error reporting you have set in PHP.
  split is deprecated and preg_split replaces it or a variation of explode
  PHP not specified in the top
  file permissions on the file you are loading may have changed

The errors that report in my apache log file are given below ... you can ignore the ones about the file not loading, but it should be there if you try my test below.

Try removing that file and see if errors get reported.  If not you need to increase your error reporting for PHP so you can debug.


<?PHP
$myFile = "prices-web.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
list($avgas, $jet, $fsii, $eff) = split('[=]', $theData);
echo "<br>Current Prices as of $eff<br><br>AVGAS 100LL: \$$avgas + Tax<br>Jet A1: \$$jet + Tax<br>Jet A1 with FSII: \$$fsii + Tax";
?>

PHP Warning:  fopen(prices-web.txt): failed to open stream: No such file or directory in loadfile.php on line 3
PHP Warning:  fgets() expects parameter 1 to be resource, boolean given in loadfile.php on line 4
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in loadfile.php on line 5
PHP Notice:  Undefined offset: 3 in /var/www/testing/loadfile.php on line 6
PHP Notice:  Undefined offset: 2 in /var/www/testing/loadfile.php on line 6
PHP Notice:  Undefined offset: 1 in /var/www/testing/loadfile.php on line 6
PHP Deprecated:  Function split() is deprecated
0
 
LVL 7

Expert Comment

by:lexlythius
ID: 34876605
Three things:

1) Enable error reporting as per ropenner's advice
2) fgets reads lines of text, not the full file contents.
3) fgets was not binary safe prior to PHP 4.3.0, so depending on the file contents you might have problems.

I modified your script to make it a bit more robust, this works for me.
<?php

error_reporting(E_ALL);

$myFile = "prices-web.txt";

$theData = readDataMap($myFile);

echo "<br>Current Prices as of " . $theData['eff'] 
		. "<br><br>AVGAS 100LL: \$" . $theData['avgas'] 
		. " + Tax<br>Jet A1: \$" . $theData['jet'] 
		. " + Tax<br>Jet A1 with FSII: \$" . $theData['fsii'] . " + Tax";
		

function readDataMap($sourceFile) 
{
	$fh = fopen($sourceFile, 'r') or die("cannot open data file $sourceFile");
	$data = array();
	while (($line = fgets($fh)) != false) {
		$entry = explode('=', $line, 2);
		if (count($entry) == 2) {
			$data[trim($entry[0])] = trim($entry[1]);
		}
	}
	fclose($fh);
	return $data;
}		
		
?>

Open in new window


With the following data file:
avgas=112.2
jet=81
fsii=75
eff=February 2011

Open in new window

yields these results:
Current Prices as of February 2011

AVGAS 100LL: $112.2 + Tax
Jet A1: $81 + Tax
Jet A1 with FSII: $75 + Tax

Open in new window

0
 
LVL 7

Accepted Solution

by:
lexlythius earned 300 total points
ID: 34876761
Sorry, I made a little mistake:
while (($line = fgets($fh)) != false) {

Open in new window

should be:
while (($line = fgets($fh)) !== false) {

Open in new window



0
 
LVL 4

Author Closing Comment

by:zack4x4
ID: 34891990
Thanks for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now