PHP 重命名不起作用
我使用选项 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");
并且重命名成功。
为什么现在有效?同样的重命名操作。
其他一些事情:
Windows操作系统
文件“1.txt”确实存在
重命名时我使用绝对路径
重命名之前,我使用 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:
Windows OS
File "1.txt" indeed exists
I use absolute path when renaming
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为 PHP 支持 3 点语法 (...),这是 Windows 命令行特有的东西。
另外:您可能想尝试在初始名称上使用 realpath 以确保它存在
编辑:
作为解决方案,只需执行
应该可以解决您的问题:)
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
Should solve your problem :)
您必须小心字符串中的 Windows 样式目录分隔符 (
\
)。您使用的是双引号字符串,因此任何单个反斜杠都将被解释为转义序列,而不是路径分隔符。要么使用正斜杠,要么使用单引号:所有结果都是相同的,但如果你尝试:
PHP 将评估为:
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:all come out to the same thing, but if you try:
PHP will evaluate to that to:
由于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.我有这个问题,我用这段代码解决了它:
I have this problem and i solve it by this code: