0

I've a problem with a PHP script that creates some csv file. The PHP script is the following:

<?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);
?>

This script works correctly and csv file export30gg.csv is correctly created but, every time I run this script, I've the following error:

fclose() expects parameter 1 to be resource

Can you help me, please?

luca
  • 55
  • 2
  • 8

2 Answers2

2

flose() accepts parameter as file pointer resource which will be found while code execution for example in your case it is $csvFile = fopen($csvFileName, "w"); So, it should be

fclose($csvFile); 

and not the fclose($csvFileName);

himeshc_IB
  • 853
  • 4
  • 10
1

It should be like

fclose($csvFile);

because you've stored recourse link in $csvFile, not in $csvFileName

Oleks
  • 1,633
  • 1
  • 18
  • 22