PHP 重命名不起作用

发布于 2024-09-12 10:31:41 字数 611 浏览 5 评论 0原文

我使用选项 CURLOPT_FILE 通过 cURL 下载文件,然后尝试重命名下载的文件,例如从“1.txt”重命名为“2.txt”。无法重命名该文件。

PHP 抛出错误:

“警告:重命名(E:\.../test/1.txt,E:\.../test/2.txt) [function.rename]: E:\ 中没有这样的文件或目录。 ../test\lib\CURL\Download.php 第 51 行”

之后我只运行一行脚本:

<?php rename("E:\.../test/1.txt","E:\.../test/2.txt");

并且重命名成功。

为什么现在有效?同样的重命名操作。

其他一些事情:

  1. Windows操作系统

  2. 文件“1.txt”确实存在

  3. 重命名时我使用绝对路径

  4. 重命名之前,我使用 fclose() 关闭 cURL 使用的文件句柄

有什么问题吗?如何在第一个脚本中重命名下载的文件而不出现错误?

I download a file with cURL using option CURLOPT_FILE and then try to rename the downloaded file, for example, from "1.txt" to "2.txt". It fails to rename the file.

PHP throws an error:

"Warning: rename(E:\.../test/1.txt,E:\.../test/2.txt) [function.rename]: No such file or directory in E:\.../test\lib\CURL\Download.php on line 51"

After that I run just one-line-script:

<?php rename("E:\.../test/1.txt","E:\.../test/2.txt");

and renaming succeeds.

Why does it work now? The same renaming operation.

Some other thing:

  1. Windows OS

  2. File "1.txt" indeed exists

  3. I use absolute path when renaming

  4. before renaming i close file handle used by cURL with fclose()

What is wrong? How can I rename the downloaded file in the first script without an error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-09-19 10:31:41

我不认为 PHP 支持 3 点语法 (...),这是 Windows 命令行特有的东西。

另外:您可能想尝试在初始名称上使用 realpath 以确保它存在

编辑:

作为解决方案,只需执行

<?php rename("E:\..\../test/1.txt","E:\..\../test/2.txt");

应该可以解决您的问题:)

I don't think PHP supports the 3 dots syntax (...), that is a windows command line specific thing.

Also: you might want to try using realpath on the initial name to make sure it exists

Edit:

as a solution, just do

<?php rename("E:\..\../test/1.txt","E:\..\../test/2.txt");

Should solve your problem :)

む无字情书 2024-09-19 10:31:41

您必须小心字符串中的 Windows 样式目录分隔符 (\)。您使用的是双引号字符串,因此任何单个反斜杠都将被解释为转义序列,而不是路径分隔符。要么使用正斜杠,要么使用单引号:

$src = "E:\\xampp\\htdocs\\test\\1.txt";
$src = 'E:\xampp\htdocs\test\1.txt';
$src = "E:/xampp/htdocs/test/1.txt";

所有结果都是相同的,但如果你尝试:

$src = "E:\xampp\htdocs\test\1.txt";

PHP 将评估为:

$src = "E:xampphtdocstest1.txt";

You have to be careful with Windows-style directory separators (\) in strings. You're using double-quoted strings, so any single backslash will be interpreted as an escape sequence, not a path separator. Either use forward slashes, or single quotes:

$src = "E:\\xampp\\htdocs\\test\\1.txt";
$src = 'E:\xampp\htdocs\test\1.txt';
$src = "E:/xampp/htdocs/test/1.txt";

all come out to the same thing, but if you try:

$src = "E:\xampp\htdocs\test\1.txt";

PHP will evaluate to that to:

$src = "E:xampphtdocstest1.txt";
雨后咖啡店 2024-09-19 10:31:41

由于OP说随后使用相同的重命名行运行第二个脚本,因此我不知道其他当前答案有何相关性。我的猜测是您正在使用 fopen 创建文件资源,运行curl_exec($ch);然后尝试在不调用 fclose(); 的情况下重命名文件,因为离开脚本时文件将自动关闭,这可以解释为什么具有相同代码的第二个脚本可以工作。

Since the OP says that running a second script afterwards, with the same rename line, works I don't see how either other current answer is relevant. My guess is you are using fopen to create a file resource, running curl_exec($ch); then attempting to rename the file without calling fclose(); since the file will be closed automatically when leaving the script this would explain why a second script with the same code would work.

妥活 2024-09-19 10:31:41

我有这个问题,我用这段代码解决了它:

$oldname ='E:\.../test/1.txt';
$newname ='E:\.../test/2.txt';
rename($oldname,$newname);

I have this problem and i solve it by this code:

$oldname ='E:\.../test/1.txt';
$newname ='E:\.../test/2.txt';
rename($oldname,$newname);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文