【发布时间】:2020-02-13 19:00:49
【问题描述】:
编辑:最初我简化了原始 csv 文件(从 com1_webscrapersolution_new_email-10-02-20.csv 到 products.csv)以最小化/简化问题,但事实证明文件名在这里很重要,并且是解决方案的关键。我已经以这种方式编辑了这个问题,以使这一点显而易见。我还添加了一个没有已知解释的答案,我希望其他人可以提供。
对于以下问题,我可以确认
-
/folder/custom_csv_parsing_functions.php存在,相关摘录如下所示 -
/c1/products.csv/c1/com1_webscrapersolution_new_email-10-02-20.csv 存在,可以查看全文here
我在 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'): 无法打开流:中没有这样的文件或目录 /文件夹/custom_csv_parsing_functions.php 第 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 时它不起作用?
【问题讨论】:
-
('../c1/products.csv') -
我认为你在重复我多次犯的错误。请记住,
/folder/custom_csv_parsing_functions.php和./folder/custom_csv_parsing_functions.php是两条完全不同的路径。换句话说,尝试在路径的开头添加一个点. -
@PavelJanicek 但就我而言,
/folder/custom_csv_parsing_functions.php是绝对路径。这意味着folder目录位于服务器根目录中,那么您的建议是否仍然适用? -
@user136649 不是这种情况。我立即怀疑它不在文档根目录中,如前所述,我多次无法发现这一点,以至于我立即认为您被同样的错误所困扰
-
还有第二件事。下面您从
/c1文件夹调用函数enclose_headers_dquote,但在您的示例中,您从/folder文件夹中包含它。两个功能一样吗?如在,我假设根本原因仍然是一些难以在代码中找到拼写错误
标签: php bash error-handling fopen interactive-shell