<?php
echo"<strong>使用目录函数读取目录</strong><br>";
$dir="html";
//使用opendir()函数打开目录
$handle=opendir($dir);
if($handle==false){
echo"打开目录失败!";
}else{
echo"目录句柄:".$handle."<br>";
echo"目录名称:".$dir."<br>";
//使用readdir()读取目录信息
while($file=readdir($handle)){//使用readdir()函数读取目录句柄的内容赋值给$file变量,
并把目录指针向下移动一位
if($file!==false){//当readdir()函数返回的值不等于false时,显示返回值
echo$file."<br>";
}
}
}
//使用closedir()关闭目录句柄
closedir($handle);
echo"<strong>使用Directory类读取目录</strong><br>";
$dh=dir($dir);
echo"目录句柄:".$dh->handle."<br>";
echo"目录名称:".$dh->path."<br>";
while($file=$dh->read()){//使用read()方法获取当前指针指向的文件名
if($file!==false){//当read()方法获取的值不等于false时,显示返回值
echo$file."<br>";
}
}
$dh->close();
?>
<?php
// 定义一个变量,其值为要打开的文件
$file = "do.txt";
// 使用fopen()函数打开文件,打开方式是w,并使用判断打开状态
if (false === ($fp = fopen($file, "w"))) {
echo "打开文件失败!<br>";
} else {
echo "文件打开成功!<br>";
// 定义变量,存储写入文件的内容
$c = "写入do.txt文件的内容";
// 使用fwrite()函数写入文件,并判断写入状态
if (fwrite($fp, $c, strlen($c)) === false) {
echo "文件写入失败!<br>";
} else {
echo "文件写入成功!<br>";
}
// 关闭文件句柄
fclose($fp);
}
// 使用fopen()打开文件,打开方式是r
$fp = fopen($file, "r");
if ($fp === false) {
echo "读取文件失败!<br>";
} else {
// 使用fread()读取文件的前8192个字节
echo "<br>显示读取的文件内容:<br>" . fread($fp, 8192);
fclose($fp); // 关闭文件句柄
}
// 访问远程文件 (注意:需确保PHP配置允许远程访问)
$handle = fopen("http://www.baidu.com", "r");
if ($handle === false) {
echo "<br>访问远程文件失败!<br>";
} else {
$contents = "";
// 循环读取文件
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle); // 关闭文件句柄
echo $contents; // 输出获取的内容
}
// 使用file_put_contents()函数向文件写入内容,并判断写入状态
if (file_put_contents($file, "使用file_put_contents()函数写入的内容。") > 0) {
echo "<br>使用file_put_contents()函数写入文件成功!<br>";
} else {
echo "<br>使用file_put_contents()函数写入文件失败!<br>";
}
// 使用file_get_contents()函数读取文件内容,并显示
$get = file_get_contents($file); // 使用file_get_contents()获取文件的内容
if ($get === false) {
echo "<br>使用file_get_contents()函数读取文件失败!<br>";
} else {
echo "<br>使用file_get_contents()函数读取的文件内容:<br>" . $get;
}
?>
<?php
// 初始化
$album = "album";
if (!is_dir($album)) { // 检测文件夹是否存在
mkdir($album); // 如果不存在则创建
}
// 处理上传文件
if (isset($_POST["action"]) && $_POST["action"] == "upload") { // 检测上传请求
if (isset($_FILES["file"]["tmp_name"]) && $_FILES["file"]["error"] === 0) { // 检测文件上传成功
// 定义文件存放路径
$filename = $_FILES["file"]["name"];
$targetPath = $album . "/" . $filename;
// 安全检查:只允许特定文件类型
$allowedTypes = array("jpg", "jpeg", "png", "gif");
$fileType = pathinfo($filename, PATHINFO_EXTENSION);
if (in_array(strtolower($fileType), $allowedTypes)) {
// 使用move_uploaded_file()移动临时文件
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetPath)) {
echo "上传文件成功!";
} else {
echo "上传文件失败!";
}
} else {
echo "不允许的文件类型!";
}
} else {
echo "上传过程出错!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>相册</title>
<style>
body { margin:0; padding:0; background-color:#EFEFEF; font-size:12px; }
ul { margin:0; padding:0; list-style:none; }
a { color:#333; text-decoration:none; }
a:hover { color:#999; }
.album_out { width:98%; margin:10px; }
.album_out img { margin:4px; border:1px solid #ccc; }
.album_out li { float:left; width:180px; text-align:center; margin:5px; }
</style>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<label>上传图像
<input type="file" name="file" />
</label>
<label>
<input type="submit" name="Submit" value="提交" />
<input type="hidden" name="action" value="upload" />
</label>
</form>
<hr size="1" />
<div class="album_out">
<ul>
<?php
$dh = dir($album);
echo "相册目录: " . $dh->path . "<br>";
while (($file = $dh->read()) !== false) {
if ($file != "." && $file != "..") {
$filePath = $album . "/" . $file;
// 安全检查:只显示图片文件
$fileType = pathinfo($filePath, PATHINFO_EXTENSION);
if (in_array(strtolower($fileType), $allowedTypes)) {
echo '<li><a href="' . $filePath . '" target="_blank">
<img src="' . $filePath . '" width="160" height="120" border="0" />
<br>' . htmlspecialchars($file) . '</a>
</li>';
}
}
}
$dh->close();
?>
</ul>
</div>
</body>
</html>
|