我有一个PHP脚本,我从csv中读取并将值导入数组中进行计算。我已经使用WAMP在localhost上构建了这个大型脚本,一切都正常工作。我现在已经把它放进了一个网络服务器,大约在中途收到了一个内存错误(我在本地主机中没有收到)
为了进行故障排除,我在代码的各个部分都放入了以下代码:
echo memory_get_usage();
并对所有章节进行了注释,并继续工作,直到出现错误。
在第一段中,我为localhost分配了2.16 MB的内存,为webserver分配了1.29 MB的内存。在第二段中,我为localhost分配了2.32 MB的内存,为webserver分配了1.57 MB的内存。
然而,在第三段上,我为localhost获得了3.32MB,但为服务器获得了以下错误:
致命错误:内存不足(已分配326631424)(试图分配71字节)在/home/bblabla 中
有人知道是什么原因造成的吗?该文件来自同一目录,下面是代码。这是一个足球计算器,它读取历史数据来计算梦幻足球积分,只有194行长的
无论如何,我都不是一个好的程序员,我正在使用这个项目来学习我的第一门语言。这是我写的第一段代码,所以我确信它需要重新编写(我可能会),但我不认为是什么原因导致了这个错误。它是我正在创建的数组的数量吗?为什么它能在localhost中工作,而不能在webserver中工作?使用siteground作为的宿主
感谢大家抽出时间
$file_handle = fopen("football/Historical/QBHIST.csv", "r"); //opens historical csv file of qb
while (!feof($file_handle) ) { //while the end of file is not reached, populate arrays as listed below.
$line_of_text = fgetcsv($file_handle, 1024); //line of text[n] is the value after each comma delimeter in the handle file
$PName = $line_of_text[0]; //player name
$PTeam = $line_of_text[1]; //team
$PPlays = $line_of_text[2]; //plays played
$PGamesPlayed = $line_of_text[3]; //games played
$PRushAtt = $line_of_text[4]; //rush attempts
$PRushYd = $line_of_text[5]; //rush yards
$PRushTD = $line_of_text[6]; //rush td
$PPassAtt = $line_of_text[7]; //pass attempts
$PPassComp = $line_of_text[8]; //pass completions
$PPassYard = $line_of_text[9]; //pass yards
$PPassTD = $line_of_text[10]; //pass td
$PFumbLst = $line_of_text[11]; //fumbles lost
$PIntThr = $line_of_text[12]; //interceptions thrown
$PYear = $line_of_text[13]; //season year
$PPos = $line_of_text[14]; //player position
$PRecTD = $line_of_text[15]; //receiving td
$PRecYd = $line_of_text[16]; //receiving yards
$PRecCat = $line_of_text[17]; //receptions
$PRecTar = $line_of_text[18]; //targets
$PRecDrp = $line_of_text[19]; //drops
$PIncThr = $PPassAtt - $PPassComp; //pass incompletions
if ($PGamesPlayed <= 0){ //in case the CSV is wrong - Zeroes create weird results (but only have an effect in SUPER SUPER SUPER deep leagues (like 300+ players)
$PGamesPlayed = 1;
}
if ($PassYdValue == 0){ //these are to prevent n/0 errors.
$PassYdValue = 100000000000000000;
}
if ($RushYdValue == 0){
$RushYdValue = 100000000000000000; //these are to prevent n/0 errors.
}
if ($RecYdValue == 0){
$RecYdValue = 100000000000000000; //these are to prevent n/0 errors.
}
//points scored formula
$QBPoints =
($PPassTD * $PassTdValue) +
($PPassYard / $PassYdValue) +
($PPassComp * $PassCompValue) +
($PIntThr * $PassIntValue) +
($PIncThr * $PassIncValue) +
($PPassAtt * $PassAttValue) +
($PRushAtt * $RushAttValue) +
($PRushTD * $RushTdValue) +
($PRushYd / $RushYdValue) +
($RushFumbValue * $PFumbLst) +
($PRecTD * $RecTdValue) +
($PRecYd / $RecYdValue) +
($PRecTar * $RecTarValue) +
($PRecCat * $RecCatValue) +
($ArrayKey / 10000000)* -1; //this arraykey/billion is to prevent duplicate scores that might have an effect. each player = unique score
$PointsArray[$ArrayKey] = $QBPoints; //populates arrays for calc'd points to remain UNSORTED
$NamesArray[$ArrayKey] = $PName; //populates array for player name
$YearArray[$ArrayKey] = $PYear; //populates array for year
$GamesArray[$ArrayKey] = $PGamesPlayed; //populates array for games played
$PTDArray[$ArrayKey] = $PPassTD; //populates array for pass td
$RTDArray[$ArrayKey] = $PRushTD; //populates array for rush td
$PYdArray[$ArrayKey] = $PPassYard; //populates array for pass yards
$RYdArray[$ArrayKey] = $PRushYd; //populates array for rush yards
$PPGArray[$ArrayKey] = $QBPoints / $PGamesPlayed; //populates array for Points per game to remain UNSORTED
$PPGSortArray[$ArrayKey] = $QBPoints / $PGamesPlayed; //populates array for Points per game to be SORTED
$PosArray[$ArrayKey] = "QB"; //populates array for position
$PAttArray[$ArrayKey] = $PPassAtt; //populates array for PassATT
$PCompArray[$ArrayKey] = $PPassComp; //populates array for PassComp
$RAttArray[$ArrayKey] = $PRushAtt; //populates array for RushATT
$PIntArray[$ArrayKey] = $PIntThr; //populates array for interceptions thrown
$CatArray[$ArrayKey] = $PRecCat; //populates array for Catches
$CYdArray[$ArrayKey] = $PRecYd; //populates array for receiving yards
$CTDArray[$ArrayKey] = $PRecTD; //populates array for receiving TD
$FumbArray[$ArrayKey] = $PFumbLst; //populates array for fumbles
$ArrayKey = $ArrayKey + 1;
}
array_pop($PointsArray);
array_pop($NamesArray);
array_pop($YearArray);
array_pop($GamesArray);
array_pop($PTDArray);
array_pop($RTDArray);
array_pop($PYdArray);
array_pop($RYdArray);
array_pop($PPGArray);
array_pop($PPGSortArray);
array_pop($PosArray);
array_pop($PAttArray);
array_pop($RAttArray);
array_pop($PIntArray);
array_pop($CatArray);
array_pop($CYdArray);
array_pop($CTDArray);
//array_pop($GamesArray);
fclose($file_handle); //close your csv silly!
echo "<br>";
echo "made it to QB";
echo memory_get_usage();
不完全确定代码本身,但内存分配不是静态的。仅仅因为您本地机器上的WAMP是为了处理内存使用而设置的,并不意味着在您的Web服务器上安装php是.
我建议设置一个phpinfo文件,并将您的WAMP版本的php与托管版本进行比较。你应该能够很快看出两者之间的区别。您可以设置本地WAMP安装以匹配您的托管版本,也可以要求您的网络主机更改它们的版本。
如果你在共享服务器上,你在托管端可能不会有太多运气,但值得一试。
希望对一些人有所帮助。