我在使用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
告诉您那里有东西,即使您通常看不到它也是如此。使用之前,请从字符串中去除字符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句