Warning with, fopen, feof and fgetcsv

582 views Asked by At

I'm in trouble, I am failing to understand why this error is happening.

So when I only run this code,

function getClientProject($cliente)
{
    $file = fopen("Projetos.csv","r");
    $ArrayCount = 0;
    $bool = false;
    while(! feof($file))
    {
        $data = fgetcsv($file);
        if($data[0] != '')
        {
            if(substr_compare($data[0],$cliente, 0, 3) == 0)
            {
                if($ArrayCount > 0)
                {
                    $total = count($OpenProject);
                    for($i=0;$i<$total;$i++)
                    {
                        if($OpenProject[$i] == $data[0])
                            $bool = true;
                    }
                    if($bool == false)
                    {
                        $OpenProject[$ArrayCount] = $data[0];
                        $ArrayCount++;
                    }
                }else
                {
                    $OpenProject[$ArrayCount] = $data[0];
                    $ArrayCount++;
                }   
            }
        }
    }
    fclose($file);
    return $OpenProject;
}

It works and returns the Array. But when I call the function this way,

include_once 'CRM files/TSread.php';
$arrayC = getClientProject('SLA');

var_dump($arrayC);

No longer works and me these errors,

enter image description here

What am I doing wrong?

path, the file i'm using is "Projeto.php":

enter image description here

and my CRM files folder: enter image description here

1

There are 1 answers

0
Oldskool On BEST ANSWER

You are opening the file using a relative path. You assume that Projetos.csv is always in the same directory as the TSread.php file. Although, when including it, you seem to be in a higher directory (outside of the CRM files directory), therefor PHP can no longer find your CSV file, since it's trying to open it relative to the upper directory now.

You could pass the full path to your getClientProject method to avoid this. So, you would get something like:

$arrayC = getClientProject('SLA', __DIR__ . '/CRM files/Projectos.csv');

Obviously, you will need to change your function a little to work with this new constructor, so it should like something like this:

function getClientProject($cliente, $csv) {
    $file = fopen($csv, "r");
    // Followed by the rest of your function