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/
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
use
$this->config->item('base_url')
instead of FCPATH