1、单个文件上传
代码示例
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
$fileType = $_FILES['pic']['type'];
if (strstr($fileType, '/', true) !== 'image') echo '<p>文件类型错误</p>';
// 临时文件名
$tempFileName = $_FILES['pic']['tmp_name'];
if (is_uploaded_file($tempFileName)) {
// 原始文件名
$originalFileName = $_FILES['pic']['name'];
// 目录文件名
$destFileName = 'uploads/'.strstr($originalFileName, '.');
// 移动文件到目标目录使用的函数
if (move_uploaded_file($tempFileName, $destFileName)) {
echo "<p>$originalFileName: 上传成功~~</p>";
// 预览
echo "<img src='{$destFileName}' width='200'>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单文件上传</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单个文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input type="file" name="pic" id="pic">
<button>提交</button>
</fieldset>
</form>
</body>
</html>
2、多个文件上传
代码示例
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
if ($_FILES['pic'])
foreach ($_FILES['pic']['error'] as $key => $error) {
// 只要判断 error === 0
if ($error === UPLOAD_ERR_OK) {
// 临时文件名
$tmpFileName = $_FILES['pic']['tmp_name'][$key];
// 原始文件名
$originalFileName = $_FILES['pic']['name'][$key];
// 目标文件名
$destFileName = 'uploads/'. $originalFileName;
// 移动文件
move_uploaded_file($tmpFileName, $destFileName);
// 预览
echo "<img src='{$destFileName}' width='200'>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单文件上传</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单个文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input type="file" name="pic[]" id="pic">
<input type="file" name="pic[]" id="pic">
<input type="file" name="pic[]" id="pic">
<button>提交</button>
</fieldset>
</form>
</body>
</html>
3、批量上传
代码示例
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
if ($_FILES['pic'])
foreach ($_FILES['pic']['error'] as $key => $error) {
// 只要判断 error === 0
if ($error === UPLOAD_ERR_OK) {
// 临时文件名
$tmpFileName = $_FILES['pic']['tmp_name'][$key];
// 原始文件名
$originalFileName = $_FILES['pic']['name'][$key];
// 目标文件名
$destFileName = 'uploads/'. $originalFileName;
// 移动文件
move_uploaded_file($tmpFileName, $destFileName);
// 预览
echo "<img src='{$destFileName}' width='200'>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单文件上传</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单个文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input type="file" name="pic[]" id="pic" multiple>
<button>提交</button>
</fieldset>
</form>
</body>
</html>
4、封装常用上传方式
4.1、封装上传方式
代码示例
<?php
class Upload{
//单文件上传
public function one(){
$fileType = $_FILES['pic']['type'];
if (strstr($fileType, '/', true) !== 'image') echo '<p>文件类型错误</p>';
// 临时文件名
$tempFileName = $_FILES['pic']['tmp_name'];
if (is_uploaded_file($tempFileName)) {
// 原始文件名
$originalFileName = $_FILES['pic']['name'];
// 目录文件名
$destFileName = 'uploads/'.strstr($originalFileName, '.');
// 移动文件到目标目录使用的函数
if (move_uploaded_file($tempFileName, $destFileName)) {
echo "我是".__FUNCTION__."方法"."<br>";
// 预览
echo "<img src='{$destFileName}' width='200'>";
}
}
}
//多文件上传
public function two(){
if ($_FILES['pic'])
echo "我是".__FUNCTION__."方法"."<br>";
foreach ($_FILES['pic']['error'] as $key => $error) {
// 只要判断 error === 0
if ($error === UPLOAD_ERR_OK) {
// 临时文件名
$tmpFileName = $_FILES['pic']['tmp_name'][$key];
// 原始文件名
$originalFileName = $_FILES['pic']['name'][$key];
// 目标文件名
$destFileName = 'uploads/'. $originalFileName;
// 移动文件
move_uploaded_file($tmpFileName, $destFileName);
// 预览
echo "<img src='{$destFileName}' width='200'>";
}
}
}
}
$uploads = new Upload();
//判断传入的数据
$files = $_FILES['pic']['name'];
if(!is_array($files)){
$uploads->one();
}else{
$uploads->two();
}
?>
4.2、前端代码——单文件上传
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单文件上传</title>
</head>
<body>
<form action="class.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单个文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pic" id="pic">
<button>提交</button>
</fieldset>
</form>
</body>
</html>
4.3、前端代码——多文件上传
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>单文件上传</title>
</head>
<body>
<form action="class.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单个文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pic[]" id="pic">
<input type="file" name="pic[]" id="pic">
<button>提交</button>
</fieldset>
</form>
</body>
</html>
学习总结
本节课我们学习了文件上传,通过本节课的学习使我学到了文件上传的知识以及如何进行文件上传的操作。希望在以后的实战中巩固和深入理解。