简体   繁体   English

PHP移动文件目录

[英]PHP move files directory

I am trying to move my jpg image file from one directory to another using the rename() function. 我正在尝试使用rename()函数将jpg图像文件从一个目录移动到另一个目录。 However it keeps on giving the error saying No such file or directory. 但是,它一直在提示错误,提示没有此类文件或目录。 I have changed it to the copy() function with the following error failed to open stream: No such file or directory. 我将其更改为copy()函数,但以下错误导致无法打开流:没有此类文件或目录。

I am trying this as following: 我正在尝试这样做如下:

rename('/_upload/1.image.jpg', '/_accepted/test/1.image.jpg');

The file is orginally already in my htdocs/_upload folder. 该文件原本已经在我的htdocs / _upload文件夹中。 This PHP file is already in my htdocs folder. 这个PHP文件已经在我的htdocs文件夹中。 All permissions are set to 777 but giving the same error. 所有权限都设置为777,但给出相同的错误。

Instead of starting / in your path, use ./ : 不要在路径中开始/ ,而使用./

rename('./_upload/1.image.jpg', './_accepted/test/1.image.jpg');

/ means that you are giving path starting the server's root directory. /表示您正在提供启动服务器根目录的路径。

./ starts with your current directory. ./从当前目录开始。

In your example, the arguments to rename are file names. 在您的示例中, rename的参数是文件名。 /_upload/1.image.jpg is an absolute file name. /_upload/1.image.jpg是绝对文件名。 That means, it is relative to the root directory. 这意味着它是相对于根目录的。 It is not relative to your server root, document root or current dierctory. 它与您的服务器根目录,文档根目录或当前目录无关。

试试这个给出完整的绝对路径

rename($_SERVER['DOCUMENT_ROOT'].'/_upload/1.image.jpg', $_SERVER['DOCUMENT_ROOT'].'/_accepted/test/1.image.jpg');

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

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