Solved

Website stalling on this code for some reason...

Posted on 2011-02-11
4
366 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Read about why website design really matters in today's demanding market.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

910 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

29 Experts available now in Live!

Get 1:1 Help Now