?
Solved

Website stalling on this code for some reason...

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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
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…
Suggested Courses

765 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