文件名上的问号使用fopen [英] Question mark on filename using fopen

查看:272
本文介绍了文件名上的问号使用fopen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用fopen保存文件时遇到问题。由于某种原因,保存的文件最后有一个问号。

I am having a problem saving a file using fopen. For some reason the saved file has a question mark in the end.

我正在尝试从远程服务器检索文件列表并将其下载到我的服务器。
这是我的代码的工作部分:

I am attempting to retrieve a list of files from a remote server and download them to my server. This is the part of my code that does the job :

$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);
}

此脚本返回以下内容(我已隐藏实际地址 - 空格等):

This script returns the following (I've hidden the actual addresses - retain the spaces etc):

the filepath : https://example.com.com/xxx/xxx.xlsx
the filename : xxx.xlsx 
local file to save : incoming/xxx.xlsx 

在我的服务器上我做一个ls我得到:

When on my server I do a ls I get :

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

文件没有问题,当我删除?我可以检索它正常和打开它。
这是什么?

There is nothing wrong with the file, when I remove the ? I can retrieve it as normal and open it. What is this? And how can I do it so it is not added in the end?

推荐答案

你用来命名文件的字符串在末尾有一个不可打印字符, ls 告诉您有某些 ,即使您通常无法看到它。在使用之前,从字符串中取出字符。

The string you're using to name the file has a non-printable character at the end, and ls is telling you that there is something there, even if you would normally be unable to see it. Strip the character from the string before using it.

这篇关于文件名上的问号使用fopen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆