php 文件移动、拷贝和删除
warning:
这篇文章距离上次修改已过205天,其中的内容可能已经有所变动。
<?php
// 文件移动
$sourceFile = 'source.txt';
$targetFile = 'destination.txt';
if (rename($sourceFile, $targetFile)) {
echo "文件已被移动";
} else {
echo "文件移动失败";
}
// 文件复制
$sourceFile = 'source.txt';
$targetFile = 'copy.txt';
if (copy($sourceFile, $targetFile)) {
echo "文件已被复制";
} else {
echo "文件复制失败";
}
// 文件删除
$fileToDelete = 'destination.txt';
if (file_exists($fileToDelete)) {
if (unlink($fileToDelete)) {
echo "文件已被删除";
} else {
echo "文件删除失败";
}
} else {
echo "文件不存在";
}
?>
这段代码展示了如何在PHP中使用rename
函数进行文件移动、copy
函数进行文件复制以及unlink
函数进行文件删除。同时,代码中包含了对文件是否存在的检查,以确保在尝试删除文件之前它确实存在。
评论已关闭