Solved

Website stalling on this code for some reason...

Posted on 2011-02-11
4
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

749 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