Oihana PHP

moveFile.php

Table of Contents

Functions

moveFile()  : bool
Moves (or renames) a single file to a destination path.

Functions

moveFile()

Moves (or renames) a single file to a destination path.

moveFile(string $source, string $destination[, bool $overwrite = true ][, bool $createDirectory = true ]) : bool

Shares the destination semantics of copyFile():

  • the source is validated with assertFile() (must exist and be readable);
  • if $destination is an existing directory, the file is moved inside it, keeping the source basename;
  • when $overwrite is false, an existing destination raises a FileException;
  • the destination's parent directory is created on demand when $createDirectory is true.

The move is performed with rename() (atomic on the same filesystem). When the source and destination live on different filesystems, rename() cannot span devices, so the function transparently falls back to copyFile() + deleteFile().

Parameters
$source : string

Path to the source file to move.

$destination : string

Destination file path, or an existing directory to move into.

$overwrite : bool = true

Whether to overwrite an existing destination file (default: true).

$createDirectory : bool = true

Whether to create the destination directory if missing (default: true).

Tags
throws
FileException

If the source is invalid, or the destination exists and $overwrite is false.

DirectoryException

If the destination directory is missing and $createDirectory is false, or cannot be created.

author

Marc Alcaraz (ekameleon)

since
1.2.0
example
use function oihana\files\moveFile;

moveFile( '/tmp/upload.tmp' , '/data/final.pdf' ) ;  // move + rename
moveFile( '/data/final.pdf' , '/archive' ) ;         // into a directory
Return values
bool

Returns true on success.

On this page

Search results