Script won't always run on server
08-18-07, 09:24 PM
Newbie Coder
Join Date: Jul 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Script won't always run on server
I've written a script that reads in a few files, converts the contents of those files to arrays in the form I need and then writes out the result to a file. The script runs great on my local computer. When I upload it to the server, it will sometimes run but many times it will die.
If I reduce the size of the input files, the script runs fine so it appears to be some resource problem.
I added set_time_limit(0) at the top of the script but it didn't change anything. I put in some sleep statements thinking that it was still a timeout issue but no change. I also changed the following settings in the php.ini file I am using:
max_execution_time
max_input_time
memory_limit
and added some statements to prevent the page from being cached, like header("Pragma: no-cache").
But no matter what I do, the script seems to fail at will. It alternates between getting an Internal Server Error and a page not found message. Occassionally it will just stop with one of the echo's I am using being the last thing on the screen but the browser showing no activity.
Does anyone have any idea what the problem might be? How I go about isolating the problem?
08-19-07, 05:13 AM
Community Leader
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
08-19-07, 03:34 PM
Newbie Coder
Join Date: Jul 2006
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
I didn't do that due to the size but here it is:
PHP Code:
<?php
// Date in the past
header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
// always modified
header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
// HTTP/1.1
header ( "Cache-Control: no-store, no-cache, must-revalidate" );
header ( "Cache-Control: post-check=0, pre-check=0" , false );
// HTTP/1.0
header ( "Pragma: no-cache" );
set_time_limit ( 0 );
$filename = 'temp/csv1.txt' ;
$filenameAttr = 'temp/csv2.txt' ;
$colCtr = 0 ; //track how many columns are in the file that are being used
$colNotUsed = array(); //record which columns are to be skipped
function GetQuantity ( $array , $modelNumber )
{
foreach( $array as $k => $d )
{
if ( $modelNumber === $k )
return $d ;
}
return 0 ;
}
function GetQuantityAttribute ( $modelNumber , $size , $attrSizes )
{
for ( $i = 0 ; $i < count ( $attrSizes ); $i += 3 )
{
if ( $modelNumber === $attrSizes [ $i ][ 'modelnumber' ])
{
if ( $attrSizes [ $i + 1 ][ 'size' ] === $size )
{
return '0' . "\t" ;
}
}
}
return '' . "\t" ;;
}
function LoadAttributeFile ( $filename , & $attrSizes )
{
if (( $file = file ( $filename )))
{
$products = array();
foreach ( $file as $line )
{
$parts = explode ( "\t" , $line );
$parts [ 0 ] = trim ( $parts [ 0 ]);
$qty = 0 ; //default value if column is set to NO
if ( trim ( $parts [ 2 ]) === "LOW" ) //some value below 5
$qty = 2 ;
else if ( trim ( $parts [ 2 ]) === "YES" ) //some value up to 10
$qty = 7 ;
$products [ $parts [ 0 ]] = (isset( $products [ $parts [ 0 ]])) ? $products [ $parts [ 0 ]] + $qty : $qty ;
$attrSizes [][ 'modelnumber' ] = $parts [ 0 ];
$attrSizes [][ 'size' ] = trim ( $parts [ 1 ]);
$attrSizes [][ 'qty' ] = $qty ;
}
return $products ;
}
else
echo 'Failed to open ' . $filename ;
return FALSE ;
}
function MapColNames ( $colHdr )
{
global $colCtr , $colNotUsed ;
$epNames = array( 'model' => 'v_products_model' ,
'image' => 'v_products_image' ,
'name' => 'v_products_name_1' ,
'desc' => 'v_products_description_1' ,
'price' => 'v_products_price' ,
'cat1_name' => 'v_categories_name_1' ,
'cat2_name' => 'v_categories_name_2' ,
'cat3_name' => 'v_categories_name_3' ,
'end' => 'EOREOR' );
switch ( $colHdr )
{
case 'PROD_CD' :
$colHdr = $epNames [ 'model' ] . "\t" ; $colCtr ++;
break;
case 'DESCRIP' :
$colHdr = $epNames [ 'desc' ] . "\t" ; $colCtr ++;
break;
case 'CLASS1_CD' :
$colHdr = $epNames [ 'cat1_name' ] . "\t" ; $colCtr ++;
break;
case 'CLASS2_CD' :
$colHdr = $epNames [ 'cat2_name' ] . "\t" ; $colCtr ++;
break;
case 'CLASS3_CD' :
$colHdr = $epNames [ 'cat3_name' ] . "\t" ; $colCtr ++;
break;
case 'OPEN_PRS' :
$colHdr = $epNames [ 'price' ] . "\t" ; $colCtr ++;
break;
case 'IMAGE' :
$colHdr = $epNames [ 'image' ] . "\t" ; $colCtr ++;
break;
case 'STYLE_CD' :
$colHdr = $epNames [ 'name' ] . "\t" ; $colCtr ++;
break;
default:
$colHdr = '' ; $colCtr ++; $colNotUsed [] = $colCtr ; //record columns to skip
break;
}
return $colHdr ;
}
function CSV2Array ( $filename )
{
if ( $fp = fopen ( $filename , "r" ))
{
while (! feof ( $fp )) {
$columns [] = explode ( "\t" , fgets ( $fp , 4096 ));
}
fclose ( $fp );
}
else
echo 'Failed to open file: ' . $filename . '<br>' ;
return $columns ;
}
if (isset( $_POST [ 'buttoninsert' ]) && $_POST [ 'buttoninsert' ] == 'Convert' )
{
$attrSizes = array();
$attrFile = LoadAttributeFile ( $filenameAttr , $attrSizes );
$r = CSV2Array ( $filename );
$rowCount = count ( $r ) - 2 ; //used to indicate time completed
$epfile = '' ;
$row = 0 ;
$newline = false ;
$colPos = 1 ;
$firstLine = true ; //identify title line
$startOfLine = false ; //used to track where the model number is located
$modelNumber = '' ;
if (isset( $attrFile ) && isset( $r ))
{
foreach( $r as $v => $k )
{
foreach( $k as $t )
{
if ( $row == 0 )
{
$epfile .= MapColNames ( $t );
}
else
{
if ( $newline == true ) //a new line has started so end previous line with EOREOR delimiter
{
if ( $firstLine ) //no quantity or attributes in source file so force it in here
{
$epfile .= 'v_products_quantity' . "\t" .
'v_tax_class_title' . "\t" .
'v_status' . "\t" .
'v_attribute_options_id_1' . "\t" . // disable until attributes quantity can be used
'v_attribute_options_name_1_1' . "\t" .
'v_attribute_values_id_1_1' . "\t" .
'v_attribute_values_name_1_1_1' . "\t" .
'v_attribute_values_price_1_1' . "\t" .
'v_attribute_values_id_1_2' . "\t" .
'v_attribute_values_name_1_2_1' . "\t" .
'v_attribute_values_price_1_2' . "\t" .
'v_attribute_values_id_1_3' . "\t" .
'v_attribute_values_name_1_3_1' . "\t" .
'v_attribute_values_price_1_3' . "\t" .
'v_attribute_values_id_1_4' . "\t" .
'v_attribute_values_name_1_4_1' . "\t" .
'v_attribute_values_price_1_4' . "\t" .
'v_attribute_values_id_1_5' . "\t" .
'v_attribute_values_name_1_5_1' . "\t" .
'v_attribute_values_price_1_5' . "\t" .
'v_attribute_values_id_1_6' . "\t" .
'v_attribute_values_name_1_6_1' . "\t" .
'v_attribute_values_price_1_6' . "\t" .
'v_attribute_values_id_1_7' . "\t" .
'v_attribute_values_name_1_7_1' . "\t" .
'v_attribute_values_price_1_7' . "\t" .
'v_attribute_values_id_1_8' . "\t" .
'v_attribute_values_name_1_8_1' . "\t" .
'v_attribute_values_price_1_8' . "\t" .
'v_attribute_values_id_1_9' . "\t" .
'v_attribute_values_name_1_9_1' . "\t" .
'v_attribute_values_price_1_9' . "\t" .
'v_attribute_values_id_1_10' . "\t" .
'v_attribute_values_name_1_10_1' . "\t" .
'v_attribute_values_price_1_10' . "\t" .
'v_attribute_values_id_1_11' . "\t" .
'v_attribute_values_name_1_11_1' . "\t" .
'v_attribute_values_price_1_11' . "\t" .
'v_attribute_values_id_1_12' . "\t" .
'v_attribute_values_name_1_12_1' . "\t" .
'v_attribute_values_price_1_12' . "\t" .
'v_attribute_values_id_1_13' . "\t" .
'v_attribute_values_name_1_13_1' . "\t" .
'v_attribute_values_price_1_13' . "\t" .
'v_attribute_values_id_1_14' . "\t" .
'v_attribute_values_name_1_14_1' . "\t" .
'v_attribute_values_price_1_14' . "\t" .
'v_attribute_values_id_1_15' . "\t" .
'v_attribute_values_name_1_15_1' . "\t" .
'v_attribute_values_price_1_15' . "\t" .
'v_attribute_values_id_1_16' . "\t" .
'v_attribute_values_name_1_16_1' . "\t" .
'v_attribute_values_price_1_16' . "\t" .
'v_attribute_values_id_1_17' . "\t" .
'v_attribute_values_name_1_17_1' . "\t" .
'v_attribute_values_price_1_17' . "\t" .
'v_attribute_values_id_1_18' . "\t" .
'v_attribute_values_name_1_18_1' . "\t" .
'v_attribute_values_price_1_18' . "\t" .
'v_attribute_values_id_1_19' . "\t" .
'v_attribute_values_name_1_19_1' . "\t" .
'v_attribute_values_price_1_19' . "\t" .
'v_attribute_values_id_1_20' . "\t" .
'v_attribute_values_name_1_20_1' . "\t" .
'v_attribute_values_price_1_20' . "\t" .
'v_attribute_values_id_1_21' . "\t" .
'v_attribute_values_name_1_21_1' . "\t" .
'v_attribute_values_price_1_21' . "\t" .
'v_attribute_values_id_1_22' . "\t" .
'v_attribute_values_name_1_22_1' . "\t" .
'v_attribute_values_price_1_22' . "\t" .
'v_attribute_values_id_1_23' . "\t" .
'v_attribute_values_name_1_23_1' . "\t" .
'v_attribute_values_price_1_23' . "\t" .
'v_attribute_values_id_1_24' . "\t" .
'v_attribute_values_name_1_24_1' . "\t" .
'v_attribute_values_price_1_24' . "\t" .
'v_attribute_values_id_1_25' . "\t" .
'v_attribute_values_name_1_25_1' . "\t" .
'v_attribute_values_price_1_25' . "\t" .
'v_attribute_values_id_1_26' . "\t" .
'v_attribute_values_name_1_26_1' . "\t" .
'v_attribute_values_price_1_26' . "\t" .
'v_attribute_values_id_1_27' . "\t" .
'v_attribute_values_name_1_27_1' . "\t" .
'v_attribute_values_price_1_27' . "\t" .
'v_attribute_values_id_1_28' . "\t" .
'v_attribute_values_name_1_28_1' . "\t" .
'v_attribute_values_price_1_28' . "\t" ;
}
else
{
$epfile .= GetQuantity ( $attrFile , $modelNumber ) . "\t" ;
$epfile .= 'Taxable Goods' . "\t" ;
$epfile .= 'Active' . "\t" ;
$epfile .= '2' . "\t" . 'Size' . "\t" . '1' . "\t" . '5' . "\t" ; //set the first four attrbutes values - same for all columns
$epfile .= GetQuantityAttribute ( $modelNumber , '5' , $attrSizes ) ;
$epfile .= '2' . "\t" . '5 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '5 1/2' , $attrSizes );
$epfile .= '3' . "\t" . '6' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '6' , $attrSizes );
$epfile .= '4' . "\t" . '6 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '6 1/2' , $attrSizes );
$epfile .= '5' . "\t" . '7' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '7' , $attrSizes );
$epfile .= '6' . "\t" . '7 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '7 1/2' , $attrSizes );
$epfile .= '7' . "\t" . '8' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '8' , $attrSizes );
$epfile .= '8' . "\t" . '8 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '8 1/2' , $attrSizes );
$epfile .= '9' . "\t" . '9' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '9' , $attrSizes );
$epfile .= '10' . "\t" . '9 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '9 1/2' , $attrSizes );
$epfile .= '11' . "\t" . '10' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '10' , $attrSizes );
$epfile .= '12' . "\t" . '10 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '10 1/2' , $attrSizes );
$epfile .= '13' . "\t" . '11' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '11' , $attrSizes );
$epfile .= '14' . "\t" . '11 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '11 1/2' , $attrSizes );
$epfile .= '15' . "\t" . '12' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '12' , $attrSizes );
$epfile .= '16' . "\t" . '12 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '12 1/2' , $attrSizes );
$epfile .= '17' . "\t" . '13' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '13' , $attrSizes );
$epfile .= '18' . "\t" . '13 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '13 1/2' , $attrSizes );
$epfile .= '19' . "\t" . '14' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '14' , $attrSizes );
$epfile .= '20' . "\t" . '14 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '14 1/2' , $attrSizes );
$epfile .= '21' . "\t" . '15' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '15' , $attrSizes );
$epfile .= '22' . "\t" . '15 1/2' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '15 1/2' , $attrSizes );
$epfile .= '23' . "\t" . '16' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , '16' , $attrSizes );
$epfile .= '24' . "\t" . '1' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , 'l' , $attrSizes );
$epfile .= '25' . "\t" . 'L' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , 'L' , $attrSizes );
$epfile .= '26' . "\t" . 'M' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , 'M' , $attrSizes );
$epfile .= '27' . "\t" . 'S' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , 'S' , $attrSizes );
$epfile .= '28' . "\t" . 'X.5' . "\t" ;
$epfile .= GetQuantityAttribute ( $modelNumber , 'X.5' , $attrSizes ) ;
}
$firstLine = false ;
$newline = false ;
$startOfLine = true ; //single start of line for below
$epfile .= "EOREOR\n" ;
}
if ( in_array ( $colPos , $colNotUsed )) //skip these columns
{
$colPos ++;
continue;
}
$t = trim ( $t );
if ( $startOfLine )
{
$startOfLine = false ;
$modelNumber = $t ;
}
$epfile .= $t . "\t" ;
$colPos ++;
}
}
if ( $row > 1299 && $row % 100 == 0 )
{
sleep ( 10 );
echo round (( 100 - (( $row / $rowCount ) * 100 )), 2 ) . '% to go<br>' ;
}
$row ++;
$newline = true ;
$colPos = 1 ;
}
$fpout = '../temp/result.txt' ;
$fp = fopen ( $fpout , "w" );
if( $fp )
{
fwrite ( $fp , $epfile ); // Write information to the file
fclose ( $fp ); // Close the file
} else {
echo "Error saving file!" ;
}
}
else
echo 'failed to find arrays!<br>' ;
}
?>
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off