<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>上傳相片</title>
<style>
* {
margin: 0;
padding: 0;
}
/*圖片上傳*/
html,
body {
width: 100%;
height: 100%;
}
.container {
width: 100%;
height: 100%;
overflow: auto;
clear: both;
}
.z_photo {
width: 5rem;
height: 5rem;
padding: 0.3rem;
overflow: auto;
clear: both;
margin: 1rem auto;
border: 1px solid #555;
}
.z_photo img {
width: 1rem;
height: 1rem;
}
.z_addImg {
float: left;
margin-right: 0.2rem;
}
.z_file {
width: 1rem;
height: 1rem;
background: url('images/addxp.png') no-repeat;
background-size: 100% 100%;
float: left;
margin-right: 0.2rem;
}
.z_file input::-webkit-file-upload-button {
width: 1rem;
height: 1rem;
border: none;
position: absolute;
outline: 0;
opacity: 0;
}
.z_file input#file {
display: block;
width: auto;
border: 0;
vertical-align: middle;
}
/*遮罩層*/
.z_mask {
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .5);
position: fixed;
top: 0;
left: 0;
z-index: 999;
display: none;
}
.z_alert {
width: 3rem;
height: 2rem;
border-radius: .2rem;
background: #fff;
font-size: .24rem;
text-align: center;
position: absolute;
left: 50%;
top: 50%;
margin-left: -1.5rem;
margin-top: -2rem;
}
.z_alert p:nth-child(1) {
line-height: 1.5rem;
}
.z_alert p:nth-child(2) span {
display: inline-block;
width: 49%;
height: .5rem;
line-height: .5rem;
float: left;
border-top: 1px solid #ddd;
}
.z_cancel {
border-right: 1px solid #ddd;
}
</style>
</head>
<body>
<div class="container">
<!-- 照片添加 -->
<div class="z_photo">
<div class="z_file">
<form method="post" action="scxp.php" enctype="multipart/form-data">
<input type="file" name="u_file[]" id="file" value="" accept="image/*" multiple onchange="imgChange('z_photo','z_file');" />
<br/> <br/>
<input type="submit" value="上傳" name="submitBtn" />
</form>
</div>
</div>
<!--遮罩層-->
<div class="z_mask">
<!--彈出框-->
<div class="z_alert">
<p>確定要刪除這張圖片嗎?</p>
<p>
<span class="z_cancel">取消</span>
<span class="z_sure">確定</span>
</p>
</div>
</div>
</div>
<script type="text/javascript">
//px轉換為rem
(function(doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if (clientWidth >= 640) {
docEl.style.fontSize = '100px';
} else {
docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
function imgChange(obj1, obj2) {
//獲取點擊的文本框
var file = document.getElementById("file");
//存放圖片的父級元素
var imgContainer = document.getElementsByClassName(obj1)[0];
//獲取的圖片文件
var fileList = file.files;
//文本框的父級元素
var input = document.getElementsByClassName(obj2)[0];
var imgArr = [];
//遍歷獲取到得圖片文件
for (var i = 0; i < fileList.length; i++) {
var imgUrl = window.URL.createObjectURL(file.files[i]);
imgArr.push(imgUrl);
var img = document.createElement("img");
img.setAttribute("src", imgArr[i]);
var imgAdd = document.createElement("div");
imgAdd.setAttribute("class", "z_addImg");
imgAdd.appendChild(img);
imgContainer.appendChild(imgAdd);
};
imgRemove();
};
function imgRemove() {
var imgList = document.getElementsByClassName("z_addImg");
var mask = document.getElementsByClassName("z_mask")[0];
var cancel = document.getElementsByClassName("z_cancel")[0];
var sure = document.getElementsByClassName("z_sure")[0];
for (var j = 0; j < imgList.length; j++) {
imgList[j].index = j;
imgList[j].onclick = function() {
var t = this;
mask.style.display = "block";
cancel.onclick = function() {
mask.style.display = "none";
};
sure.onclick = function() {
mask.style.display = "none";
t.style.display = "none";
};
}
};
};
</script>
</body>
</html>
<?php
if($_POST['submitBtn'] == '上傳'){ //判斷提交按鈕是否為空
/*echo "OK<br/>";
echo "數組:".$_FILES."<br/>";
if($_FILES){
echo "是數組<br/>";
$file = $_FILES["u_file"];
echo "數組的個數:".count($file)."<br/>";//5
$name = $file["name"];
echo "數組的個數:".count($name)."<br/>";//1
}*/
//$num = $_FILES["u_file"]["name"];
//echo "個數是:".count($num);
//上傳多張圖:同時上傳多張圖的上傳原理和單張圖上傳原理是相同的,只不過在對上傳字段進行出來了時候增加了一個數組的應用。注意input的name是一個數組的形式,php中獲取數組個數的時候,才能獲取到有效的數組長度。
for($i=0; $i<count($_FILES["u_file"]["name"]); $i++){
$file_path = "images/upload1/"; //定義圖片在服務器中的存儲位置
$picture_name=$_FILES['u_file']['name'][$i]; //獲取上傳圖片的名稱
$picture_name=strstr($picture_name , "."); //通過strstr()函數截取上傳圖片的后綴
if($picture_name!= ".jpg"){ //根據后綴判斷上傳圖片的格式是否符合要求
echo "<script>alert('上傳圖片格式不正確,請重新上傳');</script>";//這里有刷新頁面
}else if($_FILES['u_file']['tmp_name'][$i]){
echo "臨時路徑:".$_FILES['u_file']['tmp_name'][$i]."<br/>";
$dataImgName[$i] = date('Ymdhis').$i;//同時上傳,后面的會覆蓋前面的,所有再在后面加上一個變量區別開,避免覆蓋。
echo "實際路徑:".$file_path.$dataImgName[$i]."<br/>";
move_uploaded_file($_FILES['u_file']['tmp_name'][$i],$file_path.$dataImgName[$i]);
//執行圖片上傳
//echo '<script>alert("圖片上傳成功!");</script>';
}
else
echo '<script>alert("圖片上傳失敗!");</script>';
}
/*
//上傳單張圖
$file_path = "images/upload1/"; //定義圖片在服務器中的存儲位置
$picture_name=$_FILES['u_file']['name']; //獲取上傳圖片的名稱
$picture_name=strstr($picture_name , "."); //通過strstr()函數截取上傳圖片的后綴
if($picture_name!= ".jpg"){ //根據后綴判斷上傳圖片的格式是否符合要求
echo "<script>alert('上傳圖片格式不正確,請重新上傳');</script>";//這里有刷新頁面
}else if($_FILES['u_file']['tmp_name']){
$dataImgName = date('Ymdhis');
move_uploaded_file($_FILES['u_file']['tmp_name'],$file_path.$dataImgName);
//執行圖片上傳
echo '<script>alert("圖片上傳成功!");</script>';
}
else
echo '<script>alert("圖片上傳失敗!");</script>';
}*/
}
?>
$_FILES函數:
當客戶端提交后,我們獲得了一個$_FILES 數組
$_FILES數組內容如下:
$_FILES['myFile']['name'] 客戶端文件的原名稱。
$_FILES['myFile']['type'] 文件的 MIME 類型,需要瀏覽器提供該信息的支持,例如"image/gif"。
$_FILES['myFile']['size'] 已上傳文件的大小,單位為字節。
$_FILES['myFile']['tmp_name'] 文件被上傳后在服務端儲存的臨時文件名,一般是系統默認。可以在php.ini的upload_tmp_dir 指定,但 用 putenv() 函數設置是不起作用的。
$_FILES['myFile']['error'] 和該文件上傳相關的錯誤代碼。['error'] 是在 PHP 4.2.0 版本中增加的。下面是它的說明:(它們在PHP3.0以后成了常量)
UPLOAD_ERR_OK
值:0; 沒有錯誤發生,文件上傳成功。
UPLOAD_ERR_INI_SIZE
值:1; 上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值。
UPLOAD_ERR_FORM_SIZE
值:2; 上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。
UPLOAD_ERR_PARTIAL
值:3; 文件只有部分被上傳。
UPLOAD_ERR_NO_FILE
值:4; 沒有文件被上傳。
值:5; 上傳文件大小為0.
/*******************************************************************/
題目:怎么樣通過php使用html5實現多文件上傳?(php多圖上傳)
答案:
定義和用法
multiple 屬性規定輸入字段可選擇多個值。如果使用該屬性,則字段可接受多個值。
實例:
<form action="demo_form.asp" method="get">
Select images: <input type="file" name="img" multiple="multiple" />
<input type="submit" />
</form>
上面實例中的input file 可接受多個文件上傳字段。
了解了html5中file的multiple屬性,下面我們開始講解使用html5實現多文件上傳。
實例代碼:
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
<p><input name="upload[]" type="file" multiple="multiple" /></p>
<input type="submit" value="Upload all files">
</form>
</body>
</html>
php代碼:
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}