Rename file in CodeIgniter

烂漫一生 提交于 2019-12-11 12:10:48

问题


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


回答1:


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/




回答2:


it depends from - are you running this commands from codeigniter instanse or from simple php script. in 1st case, usually, folder where ci looking files is public - its site / and path to file is images/tmp/1353683037.jpg

in 2 case path will be correct if for example /var/www/public/... and you script in /var/www/ because ./ in path means that in current directory where is public dir




回答3:


use

$this->config->item('base_url')

instead of FCPATH



来源:https://stackoverflow.com/questions/13532633/rename-file-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!