简体   繁体   English

PHP重命名不起作用

[英]PHP rename does not work

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". 我使用选项CURLOPT_FILE下载带有cURL的文件,然后尝试将下载的文件重命名,例如,从“ 1.txt”重命名为“ 2.txt”。 It fails to rename the file. 无法重命名该文件。

PHP throws an error: PHP引发错误:

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

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 Windows操作系统

  2. File "1.txt" indeed exists 文件“ 1.txt”确实存在

  3. I use absolute path when renaming 重命名时我使用绝对路径

  4. before renaming i close file handle used by cURL with fclose() 重命名之前,我用fclose()关闭cURL使用的文件句柄

What is wrong? 怎么了? How can I rename the downloaded file in the first script without an error? 如何在第一个脚本中重命名下载的文件而不会出现错误?

I don't think PHP supports the 3 dots syntax (...), that is a windows command line specific thing. 我不认为PHP支持3点语法(...),这是Windows命令行特定的东西。

Also: you might want to try using realpath on the initial name to make sure it exists 另外:您可能想尝试对初始名称使用realpath以确保它存在

Edit: 编辑:

as a solution, just do 作为解决方案,只要做

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

Should solve your problem :) 应该解决你的问题:)

You have to be careful with Windows-style directory separators ( \\ ) in strings. 您必须小心使用Windows样式的字符串中的目录分隔符( \\ )。 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: PHP将评估为:

$src = "E:xampphtdocstest1.txt";

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. 由于OP表示此后使用相同的重命名行运行第二个脚本,所以我看不到其他任何当前答案有何意义。 My guess is you are using fopen to create a file resource, running curl_exec($ch); 我的猜测是您正在使用fopen创建文件资源,并运行curl_exec($ ch); then attempting to rename the file without calling fclose(); 然后尝试重命名文件而不调用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: 我有这个问题,我用下面的代码解决了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM