Jump to content

Recommended Posts

I'd like some idea where to look for why my script is quitting on large inputs (only fails on remote host).

 

The PHP script reads a comma-delimited text (CSV) file and converts it to an html web page.

 

It works perfectly on my local computer -- maximum input file size I've tried is 381 KB CSV generating a 696 KB html file in 0.46 seconds.

 

But on my remote host, it fails on large inputs with a blank screen (empty html file).

 

The remote host works with input files up to about 60KB (execution time of 18 milliseconds) but fails with anything much larger than that.

 

The script works by:

- user pastes text into a < textarea > form

- text sent to server via POST

- script writes $_POST[] to a temp file on the server

- parses the temp file using FGETCSV()

- collects generated html in a string until completed

- writes the string to an html file

 

The remote server is using PHP 5.2.5 with settings:

- max_execution_time - 30

- max_input_nesting_level - 64

- max_input_time - 60

- memory_limit - 64M

- post_max_size - 16M

- upload_max_filesize - 16M

 

I added the command "set_time_limit(0)" but it had no apparent effect.

 

There is no error file in the execution directory so I have no idea where or why it is failing (and I don't know where else to look for any other error file).

 

thanks,

Tom

Link to comment
https://forums.phpfreaks.com/topic/224637-apparent-time-out-but-where/
Share on other sites

code below; sample input attached

 

example of page created:

http://mykindred.com/AnnsBlacksmiths/html/iron-1.htm

 

<?php
//
// requires PHP 5 or greater
?>

<?php
$debug = false ;
//
include('userscripts/functions.php') ;
//
// default variables
include('templates/default_variables.txt') ;
//
$title = 'Upload CSV data' ;
$revdate = date("l, F j, Y") ;
//
// global variable for first letters of surnames
global $firstletter ;
$firstletter = array(A => 'A', B => 'B', C => 'C', D => 'D', E => 'E', F => 'F', G => 'G', H => 'H', I => 'I', J => 'J', K => 'K', L => 'L', M => 'M', N => 'N', O => 'O', P => 'P', Q => 'Q', R => 'R', S => 'S', T => 'T', U => 'U', V => 'V', W => 'W', X => 'X', Y => 'Y', Z => 'Z' ) ;
//
$description = file_get_contents('templates/common_description.txt') ;
$keywords = file_get_contents('templates/common_keywords.txt') ;
include('templates/header.txt') ;
echo "<tr>\n<td>\n" ;
echo "<table border=\"0\">\n" ;
echo "<td></td>\n</tr>\n</table>\n</td>\n</tr>\n</table>\n" ;
//
?>
<div align="center">
<h2>Enter new CSV data in the form below:</h2>
<?php
// if $content has data (via POST)
// save it to $filename
// ... and then edit contents of $filename
$filename = 'temp.txt' ;
$workfile = 'temp.txt' ;
process_text($workfile, $debug) ;
?>
</div>
<?php
//
if (isset( $_POST['content'] ) ) {
//
// try to keep from timing out
set_time_limit(0) ;
ini_set('max_execution_time', '0'); // 0 = no limit.
// set start time for elapsed time stat
$time_start = microtime(true);
//
$content = trim(stripslashes( $_POST['content'] )) ;
echo '<div>' ;
//
//$csvfile = 'data/' . $filename . '.csv' ;
//$fp = fopen($csvfile, "w") or die ("<p>Error opening $csvfile in write mode!</p>") ;
//fwrite( $fp, $content) ;
//fclose($fp) or die ("<p>Error closing $csvfile!</p>") ;
//echo "CSV format saved to: <a href=\"$csvfile\">$csvfile</a><br />\n" ;
//
ParseCSV( $content, $revdate, $workfile ) ;
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<div>... Elapsed Time: $time seconds</div>\n";
}
include('templates/footer.txt') ;
return ;

// ==================================================
// process_text -- get text from <textarea> form
// ==================================================

function process_text($workfile, $debug) {
// look for data in $content via POST
// save to $filename and edit
//
// save to $filename
$fp = fopen($workfile, "w") or die ("<p>Error opening file in write mode!</p>") ;
if (isset( $_POST['content'] ) ) {
	$content = trim(stripslashes( $_POST['content'] )) ;
	//
	// set max. length of string
	$max_len = 8192 ;
	//fputs($fp, substr($content, 3, $max_len) ) ;
	fwrite( $fp, $content ) ;
}
else {
	if (!$debug) {
		// empty file contents to begin
		fwrite( $fp, '' ) ;
	}
	else {
		// use test file for debugging
		fwrite( $fp, file_get_contents('data/Yorkshire Blacksmiths-modified-small.csv') ) ;
	}
}
fclose($fp) or die ("<p>Error closing file!</p>") ;
//
?>
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
<table>
<tr>
  <td><textarea rows="25" cols="150" name="content"><?php //readfile($filename) ; ?></textarea></td>
</tr>
<tr>
  <td align="center"><input type="submit" value="Process CSV Data" /></td>
</tr>
</table
</form>
<?php
// end of function process_text
}
?>


<?php
// functions.php
//
// requires PHP 5 or greater
?>

<?php

// ==================================================
// ParseCSV
// ==================================================

function ParseCSV( $CSV, $revdate, $workfile ) {
if (empty($CSV)) return ;
global $count ;
global $filename ;
global $firstletter ;
$title = '' ;
$category = '' ;
$filename = '' ;
$desc = '' ;
//
include('templates/default_variables.txt') ;
$description = file_get_contents('templates/common_description.txt') ;
$keywords = file_get_contents('templates/common_keywords.txt') ;
//
$header = 'templates/header.txt' ;
$footer = 'templates/footer.txt' ;
$data = false ;
//
// cHTML is the output string
$cHTML = '
<table border="1">
<tr>
<th>Surname</th>
<th>Given Name</th>
<th>Trade</th>
<th>Year</th>
<th>Notes</th>
<th>eMail researcher</th>
<th>website URL</th>
</tr>' ;
//
if (false) {
	// put the CSV data into an array
	$rows = explode("\n", $CSV) ;
	$count = 0 ;
}
else {
	// read the csv file
	$file_handle = fopen($workfile, "r");
}
// $fl ==> the first letter of surname
// ... use to set up index letters
$fl = '' ;
while ( !feof($file_handle) ) {
	//foreach ($rows as $row) {
	// get cells from CSV
	//$row = str_getcsv( $row ) ;
	$row = fgetcsv($file_handle);
	//
	// trim all cells
	array_walk($row, 'trim_value');
	//
	// get each field / cell (column)
	if (stripos( $row[0], '$') === 0) {
		//echo $row[0] . '<br />' ;
		$data = false ;
		$cell = $row[0] ;
		if (stripos( $cell, 'title=') ) {
			$title = substr( $cell, strlen('title=') + 1) ;
		}
		elseif (stripos( $cell, 'filename=') ) {
			$filename = substr( $cell, strlen('filename=') + 1) ;
			// get file name without extension
			$filename = getfilename($filename) ;
		}
		elseif (stripos( $cell, 'desc=') ) {
			$desc = substr( $cell, strlen('desc=') + 1) ;
			$description .= ',' . $desc ;
		}
		elseif (stripos( $cell, 'keywords=') ) {
			$keywords .= ',' . substr( $cell, strlen('keywords=') + 1) ;
		}
		elseif (stripos( $cell, 'author=') ) {
			$author = substr( $cell, strlen('author=') + 1) ;
		}
		elseif (stripos( $cell, 'header=') ) {
			$header = substr( $cell, strlen('header=') + 1) ;
		}
		elseif (stripos( $cell, 'footer=') ) {
			$footer = substr( $cell, strlen('footer=') + 1) ;
		}
		elseif (stripos( $cell, 'category=') ) {
			$category = substr( $cell, strlen('category=') + 1) ;
		}
	}
	else {
		// parse CSV into table
		$data = true ;
		//
		// begin the html file
		// make sure there are 7 cells in the array
		$numcells = 7 - 1 ;	// arrays are zero based
		if ( count($row) < $numcells ) {
			for ($cellcount = count($row) ; $cellcount < $numcells ; ++$cellcount  ) {
				$row[$cellcount] = '' ;
			}
		}
		// get email address and web URL
		// the message is in cell 5
		// email addy in cell 6
		// url in cell 7
		if (!empty($row[6]))
		$row[5] = '<a href="mailto:' . $row[6]
		. '?subject=Blacksmiths -- ' . $title . '">' . $row[5] . '</a>' ;
		//
		if (!empty($row[7]))
		$row[6] = '<a target="_blank" href="' . $row[7] . '">Visit site' . '</a>' ;
		else $row[6] = ' ' ;
		//
		$cHTML .=  "<tr>\n" ;
		//
		// replace spaces with non-breaking spaces for surname and given name
		// Print cell 1 == Surname
		$cell = 0 ;
		if (!empty($row[$cell])) {
			$name = $row[$cell] ;
			$row[$cell] = str_replace(' ', ' ', $row[$cell]) ;
			//
			// check for new first letter of surname
			//
			$tmp = strtoupper( substr($row[$cell], 0, 1) ) ;
			if ($tmp <> $fl) {
				$fl = $tmp ;
				// put link into array
				$firstletter[$fl] = "<a class=\"index\" href=\"#$fl\">$fl</a>" ;
				// put bookmark at name
				$row[$cell] = "<a name=\"$fl\"></a>" . $row[$cell] ;
			}
		}
		else $row[$cell] = ' ' ;
		$cHTML .= "<td valign=\"top\">$row[$cell]</td>\n" ;
		// replace spaces with non-breaking spaces for surname and given name
		// print Given Name
		$cell += 1 ;
		if (!empty($row[$cell])) {
			$name = $row[$cell] . ' ' . $name ;
			$row[$cell] = str_replace(' ', ' ', $row[$cell]) ;
		}
		else $row[$cell] = ' ' ;
		$cHTML .= "<td valign=\"top\">$row[$cell]</td>\n" ;
		//
		// add name to keywords
		if (!empty($name)) $keywords .= ',' . $name ;
		//
		//
		// print Occupation
		$cell += 1 ;
		if (!empty($row[$cell])) $cHTML .= "<td valign=\"top\">$row[$cell]</td>\n" ;
		else $cHTML .= "<td> </td>\n" ;
		// print Year
		$cell += 1 ;
		if (!empty($row[$cell])) $cHTML .= "<td valign=\"top\">$row[$cell]</td>\n" ;
		else $cHTML .= "<td> </td>\n" ;
		// print Notes
		$cell += 1 ;
		if (!empty($row[$cell])) $cHTML .= "<td class=\"notes\">$row[$cell]</td>\n" ;
		else $cHTML .= "<td> </td>\n" ;
		// print email message
		$cell += 1 ;
		if (!empty($row[$cell])) $cHTML .= "<td valign=\"top\">$row[$cell]</td>\n" ;
		else $cHTML .= "<td> </td>\n" ;
		// print website URL
		$cell += 1 ;
		if (!empty($row[$cell])) $cHTML .= "<td valign=\"top\">$row[$cell]</td>\n" ;
		else $cHTML .= "<td> </td>\n" ;
		//
		$cHTML .= "</tr>\n" ;
	}
	// count number of people in the data
	if ($data) $count += 1 ;
}
//echo "$title<br />\n" ;
//echo "$filename<br />\n" ;
//echo "$keywords<br />\n" ;
//echo "$author<br />\n" ;
//echo "$header<br />\n" ;
//echo "$footer<br />\n" ;
$cHTML .= "</table>\n" ;
//
// now get header
$cHeader = get_include_contents('templates/header.txt', $title, $keywords, $category, $description, $author, $revdate) ;
//
// and add the index
$cIndex = "<tr>\n<td>\n<hr />\n" ;
$cIndex .= "<table border=\"0\" align=\"center\" width=\"100%\">\n" ;
$cIndex .= "<td> </td>\n" ;
foreach($firstletter as $fletter) {
	$cIndex .= "<td align=\"center\" class=\"index0\">$fletter</td>\n" ;
}
$cIndex .= "<td> </td>\n" ;
$cIndex .= "</tr>\n</table>\n<hr /></td>\n</tr>\n</table>\n" ;
$cHTML = $cHeader . $cIndex . $cHTML ;
//
$cHTML .= get_include_contents('templates/footer.txt', $title, $keywords, $category, $description, $author, $revdate) ;
//
if (!empty($filename)) {
	$htmlfile = $filename . '.htm' ;
	$fp = fopen($htmlfile, "w") or die ("<p>Error opening $htmlfile in write mode!</p>") ;
	fwrite( $fp, $cHTML) ;
	fclose($fp) or die ("<p>Error closing $htmlfile!</p>") ;
	echo "html file saved to: <a href=\"$htmlfile\">$htmlfile</a><br />\n" ;
	//
	$csvfile = 'data/' . $filename . '.csv' ;
	$fp = fopen($csvfile, "w") or die ("<p>Error opening $csvfile in write mode!</p>") ;
	fwrite( $fp, $CSV) ;
	fclose($fp) or die ("<p>Error closing $csvfile!</p>") ;
	echo "CSV format saved to: <a href=\"$csvfile\">$csvfile</a><br />\n" ;
}
echo '</div>' ;
//
}


// ==================================================
// get_include_contents -- process php variables in file into string
// ==================================================

function get_include_contents($filename, $title, $keywords, $category, $description, $author, $revdate) {
if (is_file($filename)) {
	ob_start() ;
	include $filename ;
	$contents = ob_get_contents() ;
	ob_end_clean() ;
	return $contents ;
}
return false ;
}

// ==================================================
// trim_value -- used with array_walk to trim array contents
// ==================================================

function trim_value(&$value)
{
$value = trim($value);
}


// ==================================================
// getfilename -- returns file name without extension
// ==================================================

function getfilename($fnwxt) {
// returns file name without extension
// ignores multiple periods in file name
$fname = '' ;
$nameparts = explode('.', $fnwxt) ;
for ($n = 0; $n < count($nameparts) - 1; ++$n) $fname .= $nameparts[$n] . '.' ;
// now remove the trailing period
$fname = substr($fname, 0, strlen($fname) - 1) ;
return $fname ;
}
?>

 

 

[attachment deleted by admin]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.