-2

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.

file.php:

    <?php

    include('/folder/custom_csv_parsing_functions.php');

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


    //ENCLOSE HD DQUOTE

    $in = $out;

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

    enclose_headers_dquote($in,$out);

throws the error:

fopen('/c1/products.csvcom1_webscrapersolution_new_email-10-02-20.csv'): failed to open stream: No such file or directory in /folder/custom_csv_parsing_functions.php 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(
        $header
    );

    $fulltext = file_get_contents($csv_input_file);

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

    array_shift($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:

[root@server 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?

user136649
  • 59
  • 1
  • 7
  • `('../c1/products.csv')` –  Feb 13 '20 at 19:06
  • 1
    I think that you are repeating a mistake I am doing so many times. Please keep in mind that `/folder/custom_csv_parsing_functions.php` and `./folder/custom_csv_parsing_functions.php` are two completely different paths. In other words, try adding a single dot `.` at the beginning of the path – Pavel Janicek Feb 13 '20 at 19:07
  • @PavelJanicek but in my case, `/folder/custom_csv_parsing_functions.php` is an absolute path. That means `folder` dir is in the server root, so is your advice still applicable? – user136649 Feb 13 '20 at 19:09
  • 1
    @user136649 it is not in that case. I instantly suspected it is NOT in document root and as said earlier, I was unable to spot this so many times, that I instantly assumed that you are hold by the same error – Pavel Janicek Feb 13 '20 at 19:13
  • There is second thing. below you are calling function `enclose_headers_dquote` from `/c1` folder, but in your example, you are includung it from `/folder` folder. Are the two functions same? As in, I am assuming that root cause is still some hard to find typo in code – Pavel Janicek Feb 13 '20 at 19:27
  • `enclose_headers_dquote` is a function inside `custom_csv_parsing_functions.php`, which resides in the absolute path `/folder`. And yes, I am calling from another php file in `/c1` which includes() `/folder/custom_csv_parsing_functions.php` – user136649 Feb 13 '20 at 19:32
  • @PavelJanicek if you are interested, check my answer. Do you understand why it works when the file is actually renamed from `com1_webscrapersolution_new_email-10-02-20.csv` to `com1`? – user136649 Feb 13 '20 at 21:50

1 Answers1

0

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.

user136649
  • 59
  • 1
  • 7
  • 1
    In your code above, you are having few "renaming" shenanigans with the file name. You have two approaches to follow. Either know "It works with `products.csv`, so the file name MUST be that and it works, so I am not touching it" or, you have to keep in mind that there is some **stupid** typo somewhere, so you split code to as many lines as possible to see where it is actually. My current guess is that you have mismatch between hyphen`-` and dash`–` – Pavel Janicek Feb 14 '20 at 08:06
  • 1
    And just to add: Do not feel stupid doing this. If I had a cent for each of mistake of that type, I would be probably king of the world :) – Pavel Janicek Feb 14 '20 at 08:12