VUE+axios+php實現(xiàn)圖片上傳
本文實例為大家分享了VUE+axios+php實現(xiàn)圖片上傳的具體代碼,供大家參考,具體內(nèi)容如下
前端部分
執(zhí)行函數(shù),注意file那有個await,獲取到文件數(shù)據(jù)后,裝在FormData里面,然后調(diào)用下文axios的uploadFile
async uploadFiles() { ? ? ? let inputObj = document.createElement("input"); ? ? ? let file = await new Promise((res) => { ? ? ? ? inputObj.setAttribute("id", "file"); ? ? ? ? inputObj.setAttribute("type", "file"); ? ? ? ? inputObj.setAttribute("name", "file"); ? ? ? ? inputObj.setAttribute("style", "visibility:hidden"); ? ? ? ? document.body.appendChild(inputObj); ? ? ? ? inputObj.click(); ? ? ? ? document.querySelector("#file").addEventListener("change", (e) => { ? ? ? ? ? console.log(e.target.files); ? ? ? ? ? res(e.target.files) ? ? ? ? }); ? ? ? }); ? ? ? let formdata = new FormData() ? ? ? formdata.append('myFile', file[0]) ? ? ? await this._http.uploadFile(formdata).then(console.log); ? ? ? document.body.removeChild(inputObj) ? ? },
http請求集中管理
async uploadFile(file) { ? ? return axios.post(`/Upload.php`,file,{ ? ? ? headers: {"Content-Type": "multipart/form-data"} ? ? }) ? }
php后端部分
接收參數(shù),調(diào)用uploadFile
<?php header('content-type:text/html;charset=utf-8'); $fileInfo=$_FILES["myFile"]; $maxSize=10485760;//10M,10*1024*1024 $allowExt=array('jpeg','jpg','png','tif'); $path="uploads"; include_once 'UploadFile.php'; uploadFile($fileInfo, $path, $allowExt, $maxSize);
封裝uploadFile函數(shù)
<?php function uploadFile($fileInfo, $path, $allowExt, $maxSize) { ? $filename = $fileInfo["name"]; ? $tmp_name = $fileInfo["tmp_name"]; ? $size = $fileInfo["size"]; ? $error = $fileInfo["error"]; ? $type = $fileInfo["type"]; ? //服務(wù)器端設(shè)定限制 ? $ext = pathinfo($filename, PATHINFO_EXTENSION); ? //目的信息 ? if (!file_exists($path)) { ? ? mkdir($path, 0777, true); ? ? chmod($path, 0777); ? } ? $uniName = md5(uniqid(microtime(true), true)) . '.' . $ext; ? $destination = $path . "/" . $uniName; ? if ($error == 0) { ? ? if ($size > $maxSize) { ? ? ? exit("上傳文件過大!"); ? ? } ? ? if (!in_array($ext, $allowExt)) { ? ? ? exit("非法文件類型"); ? ? } ? ? if (!is_uploaded_file($tmp_name)) { ? ? ? exit("上傳方式有誤,請使用post方式"); ? ? } ? ? //判斷是否為真實圖片(防止偽裝成圖片的病毒一類的 ? ? if (!getimagesize($tmp_name)) { //getimagesize真實返回數(shù)組,否則返回false ? ? ? exit("不是真正的圖片類型"); ? ? } ? ? if (@move_uploaded_file($tmp_name, $destination)) { //@錯誤抑制符,不讓用戶看到警告 ? ? ? echo "文件" . $filename . "上傳成功!"; ? ? } else { ? ? ? echo "文件" . $filename . "上傳失敗!"; ? ? } ? } else { ? ? switch ($error) { ? ? ? case 1: ? ? ? ? echo "超過了上傳文件的最大值,請上傳2M以下文件"; ? ? ? ? break; ? ? ? case 2: ? ? ? ? echo "上傳文件過多,請一次上傳20個及以下文件!"; ? ? ? ? break; ? ? ? case 3: ? ? ? ? echo "文件并未完全上傳,請再次嘗試!"; ? ? ? ? break; ? ? ? case 4: ? ? ? ? echo "未選擇上傳文件!"; ? ? ? ? break; ? ? ? case 7: ? ? ? ? echo "沒有臨時文件夾"; ? ? ? ? break; ? ? } ? } ? return $destination; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue突然報錯doesn‘t?work?properly?without?JavaScript?enabled
最近在做項目的時候遇到了些問題,所以這篇文章主要給大家介紹了關(guān)于Vue突然報錯doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下2023-01-01vue項目開發(fā)中setTimeout等定時器的管理問題
這篇文章主要介紹了vue項目開發(fā)中setTimeout等定時器的管理問題,需要的朋友可以參考下2018-09-09詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新
這篇文章主要介紹了詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04