用PHP处理图片文件的上传
程序员文章站
2022-04-09 13:17:46
...
这篇文章主要介绍了关于用PHP处理图片文件的上传,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1.html文件
form表单注意。enctype属性
<form action="goods/goodsAdd.action.php" method="POST" enctype="multipart/form-data">
代码:
<?php require('../../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"> <?php while ($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"; } ?>
相关推荐:
以上就是用PHP处理图片文件的上传的详细内容,更多请关注其它相关文章!