[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Website stalling on this code for some reason...

Posted on 2011-02-11
4
Medium Priority
?
426 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 800 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 1200 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

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 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