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

ParameterTypeDescription
$source_path string Path to the source file.
$destination_path string Path to the destination.

Return Value

TypeDescription
bool Returns true on success.

Example #1

The code sample below demonstrates the most basic file move operation.

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.

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.

Example #4

The example above shows how to organize uploaded files into date-based subdirectories.

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.