move()
public function move(string $source_path, string $destination_path): bool
Description
Moves (renames) a file from one location to another. Validates access to the source path before moving.
Parameters
| Parameter | Type | Description |
| $source_path |
string |
Path to the source file. |
| $destination_path |
string |
Path to the destination. |
Return Value
| Type | Description |
| bool |
Returns true on success. |
Example #1
The code sample below demonstrates the most basic file move operation.
$source_path = 'modules/uploads/temp/document.pdf';
$destination_path = 'modules/uploads/permanent/document.pdf';
// Check if source file exists
if ($this->file->exists($source_path)) {
$result = $this->file->move($source_path, $destination_path);
if ($result === true) {
echo 'File moved successfully';
}
} else {
echo 'Source file not found';
}
Move vs Copy: The move() method relocates the file, removing it from the source location. If you need to keep the original file in place, use copy() instead. Move operations are faster than copy operations because they don't duplicate the file data - they simply update the file system's reference to the file's location.
Example #2
The example above shows how to rename a file while keeping it in the same directory.
$source_path = 'modules/documents/old_name.pdf';
$destination_path = 'modules/documents/new_name.pdf';
// Check if source file exists
if ($this->file->exists($source_path)) {
$result = $this->file->move($source_path, $destination_path);
if ($result === true) {
echo 'File renamed successfully';
}
} else {
echo 'Source file not found';
}
Renaming Files: To rename a file without changing its location, use move() with a destination path in the same directory. For example, moving from uploads/old_name.pdf to uploads/new_name.pdf effectively renames the file. Always preserve the file extension unless you have a specific reason to change it.
Example #3
The example above demonstrates how to move a user's uploaded file from a temporary directory to permanent storage.
public function confirm_upload(): void {
$temp_file_id = segment(3, 'int');
// Get the temporary file record
$temp_file = $this->db->get_where($temp_file_id, 'temp_uploads');
if ($temp_file === false) {
redirect('uploads/not_found');
}
// Build source and destination paths
$source_path = 'modules/uploads/temp/' . $temp_file->filename;
$destination_path = 'modules/uploads/permanent/' . $temp_file->filename;
// Check if source file exists before moving
if ($this->file->exists($source_path)) {
// Ensure destination directory exists
if (!$this->file->exists('modules/uploads/permanent')) {
$this->file->create_directory('modules/uploads/permanent', 0755);
}
// Move the file
$result = $this->file->move($source_path, $destination_path);
if ($result === true) {
// Update database with new location
$data['file_path'] = $destination_path;
$data['status'] = 'permanent';
$this->model->update($temp_file_id, $data, 'temp_uploads');
set_flashdata('File moved to permanent storage');
redirect('uploads/view/' . $temp_file_id);
}
} else {
redirect('uploads/not_found');
}
}
Example #4
The example above shows how to organize uploaded files into date-based subdirectories.
public function organize_by_date(): void {
$file_id = segment(3, 'int');
// Get file record
$file = $this->db->get_where($file_id, 'uploads');
if ($file === false) {
redirect('uploads/not_found');
}
// Current location
$source_path = 'modules/uploads/incoming/' . $file->filename;
if (!$this->file->exists($source_path)) {
redirect('uploads/not_found');
}
// Create date-based directory structure (e.g., 2024/12)
$year_month = date('Y/m', strtotime($file->created_at));
$organized_dir = 'modules/uploads/organized/' . $year_month;
// Create directory structure if needed
if (!$this->file->exists($organized_dir)) {
$this->file->create_directory($organized_dir, 0755, true);
}
// Move file to organized location
$destination_path = $organized_dir . '/' . $file->filename;
$result = $this->file->move($source_path, $destination_path);
if ($result === true) {
// Update database with new path
$data['file_path'] = $destination_path;
$data['organized_date'] = date('Y-m-d H:i:s');
$this->model->update($file_id, $data, 'uploads');
set_flashdata('File organized successfully');
redirect('uploads/manage');
}
}
The "Check Before You Act" Pattern: Always verify the source file exists using $this->file->exists($source_path) before calling move(). Additionally, ensure the destination directory exists or create it first. The move operation will fail if the destination directory doesn't exist, potentially leaving your application in an inconsistent state.
Database Consistency: When moving files that are referenced in your database, always update the database record with the new file path after a successful move operation. Failing to do this will result in broken file references and "file not found" errors when users try to access the files later.