使用fopen的文件名上的问号

时间:2016-05-17 23:12:41

标签: php for-loop curl fopen

我在使用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 

当我在我的服务器上时,我会这样做:

-rw-r--r-- 1 www-data www-data 29408 May 17 23:01 xxx.xlsx?

当我删除文件时,文件没有任何问题?我可以正常检索并打开它。 这是什么?我怎么能这样做,所以最终没有添加?

1 个答案:

答案 0 :(得分:4)

您用来命名文件的字符串末尾有一个不可打印的字符,而ls告诉您那里有某些,即使您通常也是如此无法看到它。在使用之前从字符串中去除字符。

相关问题