PHP unlink
Remove an existing file and check that its name is gone.
A simple example
unlink
- Start with A practice file exists
- Do this Delete that file
- Result The path is absent
Syntax
unlink($preview)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpClear a disposable print preview
A library kiosk creates a preview whenever a borrower asks for a printout. After printing, the kiosk must remove that scratch file and confirm that the job has finished cleaning up.
<?php
$base = sys_get_temp_dir() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(12));
if (!mkdir($base, 0700)) throw new RuntimeException("Cannot create workspace");
try {
$preview = $base . '/preview';
if (!touch($preview) || !unlink($preview)) throw new RuntimeException('Cleanup failed');
if (!file_exists($preview)) echo "removed", PHP_EOL;
} finally {
foreach (['preview'] as $name) {
$path = $base . '/' . $name;
if (is_link($path) || is_file($path)) unlink($path);
elseif (is_dir($path)) rmdir($path);
}
rmdir($base);
}
removed
The check runs after removal and confirms the preview path is absent.
Prepare a gallery label
A curator stores a short UTF-8 caption beside an exhibit. The desk removes the temporary note after handover. Does its original name still identify an entry?
Prepare a studio handover
A stage manager saves a two-word instruction for the next shift. The desk removes the temporary note after handover. Does its original name still identify an entry?
What to watch for
Deletion and missing-file behavior vary by API. Limit cleanup to paths created by this job.