我在使用fopen保存文件时遇到问题。由于某些原因,保存的文件最后会有一个问号。
我正在尝试从远程服务器检索文件列表,并将它们下载到我的服务器。这是我代码中完成任务的部分:
$arrlength = count($reports);
for ($x = 0; $x < $arrlength; ++$x) {
$report = $reports[$x];
$thefilepath = returnfilename($report);
echo 'the filepath : '.$thefilepath;
echo '<br>';
$thefilename = basename($thefilepath).PHP_EOL;
echo 'the filename : '.$thefilename;
echo '<br>';
$localfile = 'incoming/'.$thefilename;
echo 'local file to save : '.$localfile;
echo '<br>';
curl_setopt($ch, CURLOPT_URL, $thefilepath);
$out = curl_exec($ch);
$fp = fopen($localfile, 'w');
fwrite($fp, $out);
fclose($fp);
}
这个脚本返回以下内容(我已经隐藏了实际地址-保留空格等):
the filepath : https://example.com.com/xxx/xxx.xlsx
the filename : xxx.xlsx
local file to save : incoming/xxx.xlsx
当我在服务器上做ls时,我得到:
-rw-r--r-- 1 www-data www-data 29408 May 17 23:01 xxx.xlsx?
文件没有任何问题,当我删除?我可以正常取回并打开它。这是什么?我该怎么做才能不添加呢?
您用来命名文件的字符串末尾有一个不可打印的字符,ls
会告诉您那里有东西,即使您通常看不到它。在使用它之前,请从字符串中剥离该字符。