Tutoriel de développement de site Web d'entreprise PHP - ajout de produits
Regardons le code pour ajouter une page?:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>添加產(chǎn)品</title> <style type="text/css"> #cnt{ width:400px; height:400px; margin-top:15px; margin-left: 15px; } </style> </head> <body> <div id="cnt"> <form method="post" action="addproduct.php" enctype="multipart/form-data"> 產(chǎn)品名稱:<input type="text" name="title" id="title"></br></br> 上傳文件:<input type="file" name="myFile"></br></br> 產(chǎn)品價(jià)格:<input type="text" name="price"></br></br> <input type="submit" value="添加產(chǎn)品"> </form> </div> </body> </html>
Ce n'est rien d'ajouter cette pièce, c'est principalement le code html. Faites attention au téléchargement de fichiers. Nous devons écrire enctype="multipart/. form-data"< dans le formulaire ??>Il s'agit d'un code nécessaire pour télécharger des fichiers
<?php require_once('conn.php'); //接受文件,臨時(shí)文件信息 $fileinfo=$_FILES["myFile"];//降維操作 $filename=$fileinfo["name"]; $tmp_name=$fileinfo["tmp_name"]; $size=$fileinfo["size"]; $error=$fileinfo["error"]; $type=$fileinfo["type"]; //服務(wù)器端設(shè)定限制 $maxsize=10485760;//10M,10*1024*1024 $allowExt=array('jpeg','jpg','png','gif');//允許上傳的文件類型(拓展名 $ext=pathinfo($filename,PATHINFO_EXTENSION);//提取上傳文件的拓展名 //目標(biāo)存放文件夾 $path="../uploads"; if (!file_exists($path)) { //當(dāng)目錄不存在,就創(chuàng)建目錄 mkdir($path,0777,true);//創(chuàng)建目錄 chmod($path, 0777);//改變文件模式,所有人都有執(zhí)行權(quán)限、寫權(quán)限、度權(quán)限 } //得到唯一的文件名!防止因?yàn)槲募嗤a(chǎn)生覆蓋 $uniName=md5(uniqid(microtime(true),true)).".$ext"; //md5加密,uniqid產(chǎn)生唯一id,microtime做前綴 //目標(biāo)存放文件地址 $destination=$path."/".$uniName; //當(dāng)文件上傳成功,存入臨時(shí)文件夾,服務(wù)器端開始判斷 if ($error==0) { if ($size>$maxsize) { exit("上傳文件過大!"); } if (!in_array($ext, $allowExt)) { exit("非法文件類型"); } if (!is_uploaded_file($tmp_name)) { exit("上傳方式有誤,請(qǐng)使用post方式"); } //判斷是否為真實(shí)圖片(防止偽裝成圖片的病毒一類的 if (!getimagesize($tmp_name)) {//getimagesize真實(shí)返回?cái)?shù)組,否則返回false exit("不是真正的圖片類型"); } //move_uploaded_file($tmp_name, "uploads/".$filename); if (@move_uploaded_file($tmp_name, $destination)) {//@錯(cuò)誤抑制符,不讓用戶看到警告 echo "文件".$filename."上傳成功!"; }else{ echo "文件".$filename."上傳失敗!"; } }else{ switch ($error){ case 1:echo "超過了上傳文件的最大值,請(qǐng)上傳2M以下文件";break; case 2:echo "上傳文件過多,請(qǐng)一次上傳20個(gè)及以下文件!";break; case 3:echo "文件并未完全上傳,請(qǐng)?jiān)俅螄L試!";break; case 4:echo "未選擇上傳文件!";break; case 7:echo "沒有臨時(shí)文件夾";break; } } $title = $_POST['title']; $imagename = $uniName; $price = $_POST['price']; $goodtime = time(); $sql = "insert into `product`(title,imgname,price,goodtime) values('$title','$imagename','$price','$goodtime')"; $res = mysql_query($sql); if($res){ echo "<script>alert('添加產(chǎn)品成功');location.href='product.php';</script>"; }else{ echo "<script>alert('添加產(chǎn)品失敗');location.href='product.php';</script>"; } ?>Voici le fichier téléchargé. Après le traitement, puis son ajout, si le fichier téléchargé n'est pas un fichier légal, un message d'erreur s'affichera