PHP copy() 函数无法处理 txt 文件中的项目列表

PHP copy() function not working with list of items from txt file

我有一个 txt 文件,其中包含大约 400 个图像的 url(摘录如下)。我编写了以下脚本来快速将文件从一台服务器下载到另一台服务器(无 ssh 访问权限)。

文本文件 - pics.txt

http://www.domain.com/pictures/name.jpg
http://www.domain.com/pictures/name1.jpg
http://www.domain.com/pictures/name2.jpg
http://www.domain.com/pictures/name3.jpg
http://www.domain.com/pictures/name4.jpg

我的简单PHP脚本:

$file = fopen("pics.txt", "r");

while(!feof($file)){

    $line = fgets($file);
    $filename = basename($line);
    $imagename = "tmp/$filename";
    echo "Trying to copy ".$line." to: ".$imagename;

    if (copy($line,$imagename)) {
        echo "Done file ".$line;
    } else {
        echo "Error occured";
    }

}
fclose($file);

不过好像不行。我刚得到:

Trying to copy http://www.domain.com/pictures/name.jpg to: tmp/name.jpg Error occured
Trying to copy http://www.domain.com/pictures/name1.jpg to: tmp/name1.jpg Error occured
Trying to copy http://www.domain.com/pictures/name2.jpg to: tmp/name2.jpg Error occured

如果我手动输入 copy('http://www.domain.com/pictures/name.jpg', 'filename.jpg');

它工作正常吗?

知道我做错了什么吗?

根据要求,并让未来的读者了解问题的真正原因。

您的文件每行包含 \n 并且是一个隐藏文件字符,将 space 添加到您的 URL,进而破坏它的真实路径。

使用 trim() 会去掉多余的 space。

使用报错:


来自评论:

"Thanks both, I turned on the error reporting and noticed it was putting a space after the url, even though there isnt one in the text file. – Chris"

"@Chris that's great Chris (you're welcome) and I'm glad that it was resolved. However and for future readers to the question, the answer given really wasn't the solution. I don't want to sound as the bad man here or bust everybody's balloon; don't get me wrong. However, that being the case, you should have used trim() to get rid of the trailing spaces. The spaces come from the hidden \n in the file which adds a space. – Fred -ii-"