這篇文章主要介紹了關於用PHP處理圖片檔案的上傳,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
1.html檔案
form表單注意。enctype屬性
<form action="goods/goodsAdd.action.php" method="POST" enctype="multipart/form-data">
代碼:
<?phprequire('../../public/common/config.php');$sqlClass = "select * from sort";$rstClass = mysqli_query($conn,$sqlClass);?><p><h4>添加商品</h4><form action="goods/goodsAdd.action.php" method="POST" enctype="multipart/form-data"><ul><li><label for="goodsname">商品名</label><input type="text" name="goodsname" id="goodsname"></li><li><label for="goodsprice">商品價格</label><input type="text" name="goodsprice" id="goodsprice"></li><li><label for="goodsort">商品庫存</label><input type="text" name="goodsort" id="goodsort"></li><li><p>產品分類</p><select name="brand_id"><?phpwhile ($rowClass=mysqli_fetch_assoc($rstClass)) {echo "<option disabled>{$rowClass['name']}</option>";$sqlBrand = "select * from brand where sort_id='{$rowClass[id]}' ";$rstBrand = mysqli_query($conn,$sqlBrand);while ($rowBrand=mysqli_fetch_assoc($rstBrand)) {echo "<option value='{$rowBrand['id']}'> |-{$rowBrand['name']}</option>";}}?></select></li><li><p>貨架</p><label for="upshelf">上架s</label><input type="radio" name="shelf" id="upshelf" value="1" checked><label for="downshelf">下架d</label><input type="radio" name="shelf" id="downshelf" value="0"></li><li><input type="file" name="img" value=""></li></ul><input type="submit" value="添加商品"></form></p>
查看資料:
echo "<pre>"; print_r($_POST); echo "</pre>"; echo "<pre>"; print_r($_FILES); echo "</pre>"; exit;
測試
。php檔案代碼:
<?php require('../../public/common/config.php'); // echo "<pre>"; // print_r($_POST); // echo "</pre>"; // echo "<pre>"; // print_r($_FILES); // echo "</pre>"; // exit; $name = $_POST['goodsname']; $price = $_POST['goodsprice']; $sort = $_POST['goodsort']; $shelf = $_POST['shelf']; $brand_id = $_POST['brand_id']; //圖片上傳 $src = $_FILES['img']['tmp_name']; $name = $_FILES['img']['name']; $ext = array_pop(explode('.', $name)); $dst = '../../public/uploads/'.time().mt_rand().'.'.$ext; if(move_uploaded_file($src, $dst)){ echo "upload success"; //圖片縮放200*200 }else{ echo "upload error"; }?>