编辑:最初我简化了原始csv文件(从com1_webscrapersolution_new_email-10-02-20.csv
到products.csv
)以最小化/简化问题,但事实证明文件名在这里很重要,并且是解决方案的关键。我以某种方式使问题变得显而易见。我还添加了一个答案,但没有已知的解释,希望其他人可以提供。
对于以下问题,我可以确认
/folder/custom_csv_parsing_functions.php
存在,相关摘录如下我在bash中以
php -f file.php
运行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);
引发错误:
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
摘自
/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);
}
如果文件products.csvcom1_webscrapersolution_new_email-10-02-20.csv存在并且设置为777权限,为什么PHP报告“fopen('/c1/products.csvcom1_webscrapersolution_new_email-10-02-20.csv'):失败打开流:没有这样的文件或目录?
此外,我可以确认直接在bash交互式shell中运行PHP确实可以成功:
[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');
成功的输出是
/c1/com1_webscrapersolution_new_email-10-02-20_enc_hd.csv
(可以在here中查看)。那么,当在bash中以
php -f file.php
运行PHP时,为什么它不起作用?
最佳答案
我不明白为什么,但是这样做有效,因此我将不得不相应地编辑原始问题。
不幸的是,为了尽量简化示例,为了简化名称,我将csv文件的实际名称从com1_webscrapersolution_new_email-10-02-20.csv
重命名为products.csv
,认为这种区别没有任何意义。
当我重命名为com1.csv
时,它起作用了。我假设它与名称中所有字符类型的复杂性及其解析方式等有关。
我不知道为什么这种差异会有所作为?我是否违反了Linux中文件命名规则?破折号,数字和下划线在Linux文件名中都是合法的,不是吗?
我什至已将名称显式传递为'\'c1/com1_webscrapersolution_new_email-10-02-20.csv\''
,因此提供的参数值将显式括在单引号中。
关于php - 打开: failed to open stream: No such file or directory - but file/directory exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60214782/