PHP rename
Move an existing draft to a new name in the same directory.
A simple example
rename
- Start with A file named a.txt
- Do this Rename it to b.txt
- Result b.txt holds the original contents
Syntax
rename($draft, $destination)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpPublish a signed attendance sheet
A class representative edits a draft until the names have been checked. Once the sheet is signed, rename it in the same workspace and confirm the final name is available for publication.
<?php
$base = sys_get_temp_dir() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(12));
if (!mkdir($base, 0700)) throw new RuntimeException("Cannot create workspace");
try {
$draft = $base . '/draft';
$destination = $base . '/attendance.txt';
if (file_put_contents($draft, 'signed') === false || !rename($draft, $destination)) {
throw new RuntimeException('Cannot publish sheet');
}
if (is_file($destination) && !file_exists($draft)) echo basename($destination), PHP_EOL;
} finally {
foreach (['draft', 'attendance.txt'] as $name) {
$path = $base . '/' . $name;
if (is_link($path) || is_file($path)) unlink($path);
elseif (is_dir($path)) rmdir($path);
}
rmdir($base);
}
attendance.txt
After the successful rename the final entry identifies the published sheet.
Prepare a gallery label
A curator stores a short UTF-8 caption beside an exhibit. Rename the draft to its final name. What text can the next shift read under that name?
Prepare a studio handover
A stage manager saves a two-word instruction for the next shift. Rename the draft to its final name. What text can the next shift read under that name?
What to watch for
Moving across storage volumes can have different failure and atomicity guarantees.