Home » Php » php – fopen: failed to open stream: No such file or directory – but file/directory exists

php – fopen: failed to open stream: No such file or directory – but file/directory exists

Posted by: admin February 25, 2020 Leave a comment


Edit: Originally I simplified the original csv file (from com1_webscrapersolution_new_email-10-02-20.csv to products.csv) to minimise/simplify the question, but it turned out the file name is of consequence here and is key to the solution. I have edited the question in such a way to make this apparent. I have also added an answer without an known explanation which I’m hoping someone else can provide.

For the following problem, I can confirm that

  • /folder/custom_csv_parsing_functions.php exists, the relevant excerpt is shown below
  • /c1/products.csv /c1/com1_webscrapersolution_new_email-10-02-20.csv exists and may be viewed in full here

I am running the PHP in bash as php -f file.php.




    $out = '/c1/com1_webscrapersolution_new_email-10-02-20.csv';


    $in = $out;

    $out = str_replace('.csv','_enc_hd.csv',$in);


throws the error:

failed to open stream: No such file or directory in
on line 58

Excerpt from /folder/custom_csv_parsing_functions.php:

//removed code here as this is an excerpt

function enclose_headers_dquote($csv_input_file, $csv_output_file){

    $line = fgets(fopen($csv_input_file, 'r')); //this is line 58

    $line = str_replace("\r\n", '', $line);

    $arr = explode(',', $line);

    $header = '"' . implode('","', $arr) . '"';

    $header = array(

    $fulltext = file_get_contents($csv_input_file);

    $fulltext = explode("\r\n", $fulltext);


    $reconstituted_csv_array = array_merge($header, $fulltext);
    $reconstituted_csv_str   = implode("\r\n", $reconstituted_csv_array);
    file_put_contents($csv_output_file, $reconstituted_csv_str);


If the file products.csvcom1_webscrapersolution_new_email-10-02-20.csv exists, and is set to 777 permissions, why is PHP reporting “fopen(‘/c1/products.csvcom1_webscrapersolution_new_email-10-02-20.csv’): failed to open stream: No such file or directory”?

Further, I can confirm that running the PHP directly in bash interactive shell does succeed:

[[email protected] c1]# php -a
Interactive shell

php > include('/folder/custom_csv_parsing_functions.php');
php > enclose_headers_dquote('/c1/com1_webscrapersolution_new_email-10-02-20.csv','/c1/com1_webscrapersolution_new_email-10-02-20_enc_hd.csv');

Successful output is /c1/com1_webscrapersolution_new_email-10-02-20_enc_hd.csv (can be viewed here).

So why doesn’t it work when running the PHP in bash as php -f file.php?

How to&Answers:

I do not understand why but this works, and I will have to edit the original question accordingly.

Unfortunately, in trying to comply with minimal example I renamed the actual name of the csv file from com1_webscrapersolution_new_email-10-02-20.csv to products.csv in order to simplify the name, thinking this difference was of no consequence.

When I renamed to com1.csv, it worked. I’m assuming it has to do with the complexity of all the character types in the name and the way it is parsed or some such.

I have no idea why this difference makes a difference? Have I violated rules of file naming in Linux? Both dashes, numbers and underscores are all legal in Linux file names, no?

I have even explicitly passed the name as '\'c1/com1_webscrapersolution_new_email-10-02-20.csv\'' so the argument value provided would be explicitly enclosed in single quotes.