<?php
// Sample code for the introduction, not related to the main content
echo "This is a PHP script example demonstrating article structure.";
?>
<hr>
<?php
// Main content starts
echo "<h1>Common Usage Scenarios and Tips for is_dir During File Movement</h1>";
echo "<p>In PHP development, file and directory operations are very common tasks, especially file movements (moving or renaming files). During file movement, <code>is_dir";
echo "If a file with the same name already exists in the target directory, you can use is_dir along with file_exists to check and avoid overwriting:
"; echo "$destinationDir = 'backup/2025/';
$destination = $destinationDir . basename($source);
if (is_dir($destinationDir)) {
if (!file_exists($destination)) {
rename($source, $destination);
} else {
echo 'Target file already exists, move failed';
}
} else {
echo 'Target directory does not exist';
}";
echo "When moving multiple files, you can check whether each target directory exists before moving the corresponding file:
"; echo "$files = ['file1.txt', 'file2.txt'];
$baseDir = 'backup/2025/';
foreach ($files as $file) {
$destinationDir = $baseDir . pathinfo($file, PATHINFO_FILENAME) . '/';
if (!is_dir($destinationDir)) {
mkdir($destinationDir, 0777, true);
}
rename('uploads/' . $file, $destinationDir . $file);
}";
echo "In summary, is_dir is an indispensable function in the file movement process. It ensures that the directory exists, prevents operation errors, and, when combined with other functions (like mkdir and file_exists), can create a more robust file movement logic.
"; ?>