文件上傳很有意思的
1.文件上傳html
(1)文件上傳
<div class="form-group row ">
<label class="control-label col-md-1 col-sm-1 ">icon</label>
<div class="col-md-5 col-sm-5 ">
<input type="file" name="user_name<?php echo $i; ?>" placeholder="icon" onchange="readURL(this,<?php echo $i;?>);" required="">
</div>
</div>
<div class="form-group row ">
<label class="control-label col-md-1 col-sm-1 ">圖片顯示</label>
<div class="col-md-5 col-sm-5 ">
<img id="image<?php echo $i;?>" src="#" alt="" />
</div>
</div>
(2)選擇文件後顯示文件(圖片顯示圖片)
//點擊選擇的文件後 顯示再頁面上
function readURL(input,i) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#image'+i)
.attr('src', e.target.result)
.width(100)
.height(100);
};
reader.readAsDataURL(input.files[0]);
}
}
2.php後端代碼
//圖片處理
$fileInfo = $_FILES['ad_image'.$i];
$image_tem = explode('.',$fileInfo["name"]);
//圖片名稱
$fileName = $image_tem[0].time().$i.'.'.$image_tem[1]; //文件名稱
$tmpName = $fileInfo['tmp_name']; //臨時文件
move_uploaded_file($tmpName, "libraries/admin/images/" . $fileName);
3.一般圖片處理都放在後端,具體的驗證方法後續提供
(文件類型,文件真實類型,錯誤號,文件大小,文件夾是否存在-創建與賦予權限...)