我遇到了创建一些csv文件的PHP脚本问题。 PHP脚本如下:
<?php
$inputFile = "/var/www/vhosts/pecso.it/httpdocs/test/export30gg.txt";
$csvData = file_get_contents($inputFile);
$rows = explode(PHP_EOL, $csvData);
$rowsArray = array();
foreach ($rows as $row) {
$rowsArray[] = str_getcsv($row);
}
$csvFileName = "/var/www/vhosts/pecso.it/httpdocs/graphs/export30gg.csv";
if (file_exists($csvFileName)){
unlink($csvFileName);
}
$csvFile = fopen($csvFileName, "w");
$csvFileForGraph = fopen($csvFileNameForGraph, "w");
for ($i = 0; $i < count($rowsArray); $i++) {
$dateTime = DateTime::createFromFormat('d/m/Y', $rowsArray[$i][0]);
$d = $dateTime->format('Y-m-d');
$rowsArray[$i][0] = $d;
$rowForGraph = $rowsArray[$i];
unset($rowForGraph[1]);
$row = implode(',',$rowsArray[$i]);
$rowForGraph = implode(',',$rowForGraph);
file_put_contents($csvFileName, $row.PHP_EOL , FILE_APPEND);
}
fclose($csvFileName);
?>
此脚本正常运行并且正确创建了csv文件export30gg.csv但是,每次运行此脚本时,我都会出现以下错误:
fclose() expects parameter 1 to be resource
请帮帮我吗?
答案 0 :(得分:1)
应该是
fclose($csvFile);
因为您在$ csvFile中存储了追索链接,而不是在$ csvFileName
中答案 1 :(得分:1)
flose()接受参数作为文件指针资源,在代码执行时会找到它,例如在你的情况下是$csvFile = fopen($csvFileName, "w");
所以,它应该是
fclose($csvFile);
而不是fclose($ csvFileName);