在CodeIgniter中重命名文件 [英] Rename file in CodeIgniter
问题描述
我在tmp文件夹中有一个文件 1353683037.jpg
,如下所示:
C:\xampp\htdocs\ci_project\public\images\tmp\1353683037.jpg
我需要将此文件重命名为:
C:\xampp\htdocs\\ \\ c_project \public\images\main\1353683037.jpg
为了做到这一点,我已执行以下操作:
$ file ='1353683037.jpg';
$ dir ='./public/images/';
rename($ dir.'tmp\'。$ file,$ dir.'main\'。$ file);
我从服务器收到以下回复:
遇到PHP错误
严重性:警告
消息:rename(./ public / images / tmp / 1353683037.jpg,.. / public / images / main / 1353683037.jpg)[function.rename]:系统找不到指定的路径。 (代码:3)
文件名:controllers / test.php
行号:1
可能是什么问题?感谢
您的代码无法使用的原因有两种:
首先 - 您的目录路径使用反斜线。反斜杠转义了一些字符,包括 tmp
和 main
后的单引号。即使您的Windows环境使用反斜杠,您应该使用向前。
第二<可能不正确。
常量(在index.php中设置) FCPATH
因此,我们应该可以将您的代码修改为以下内容:
$ file = $ file ='1353683037.jpg';
$ oldDir = FCPATH。 'public / images / tmp /';
$ newDir = FCPATH。 'public / images / main /';
rename($ oldDir。$ file,$ newDir。$ file);
如果此代码不起作用,您可能在CodeIgniter的index.php文件中错误地设置了FCPATH 。如果不正确,您应该将其更改为正确的路径,即 / xampp / htdocs / ci_project /
I have a file 1353683037.jpg
in the tmp folder as shown below:
C:\xampp\htdocs\ci_project\public\images\tmp\1353683037.jpg
I need to rename this file to:
C:\xampp\htdocs\ci_project\public\images\main\1353683037.jpg
To do this, I have done the following:
$file = '1353683037.jpg';
$dir = './public/images/';
rename($dir.'tmp\'. $file, $dir.'main\'. $file);
I'm having the follwing reply from the server:
A PHP Error was encountered
Severity: Warning
Message: rename(./public/images/tmp/1353683037.jpg,../public/images/main/1353683037.jpg) [function.rename]: The system cannot find the path specified. (code: 3)
Filename: controllers/test.php
Line Number: 1
What could possibly be the problem? Thanks
There are two possible reasons that your code will not work:
First - You are using backslashes for your directory path. The backslash is escaping some of your characters including the single quote after tmp
and main
. Even though your windows environment uses backslashes, you should use forward.
Second - Your path is likely incorrect. Rather than use a relative path, try using an absolute path.
The constant (as set in index.php) FCPATH
will give you the server path to your base CodeIgniter directory.
So, we should be able to modify your code to the following:
$file = $file = '1353683037.jpg';
$oldDir = FCPATH . 'public/images/tmp/';
$newDir = FCPATH . 'public/images/main/';
rename($oldDir.$file, $newDir.$file);
If this code does not work, you probably have FCPATH incorrectly set in your index.php file for CodeIgniter. If it is incorrect, you should change it to the proper path, i.e. /xampp/htdocs/ci_project/
这篇关于在CodeIgniter中重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!