使用Mysql

时间:2018-04-02 23:37:16

标签: php file-upload image-upload

我正在尝试上传产品的图片,并将所有信息(id prod,name,category ...)插入到table produit中。

我的代码恢复错误的探测器:

  

警告:file_get_contents(26754765_956068941216475_1243103994_n.jpg):无法打开流:第17行的C:\ xampp \ htdocs \ PFE \ upload.php中没有此类文件或目录

任何帮助请? 吼我的代码html php

<html>
<head>
<meta charset="utf-8" />
<title>Ajout produit</title>
</head>
<body >
<form  method="POST" action="upload.php" enctype="multipart/form-data">
<fieldset>
<legend align="center" ><h2><font color="#990000" face="Georgia, Times New Roman, Times, serif" >Décrivez votre produit!</font></h2></legend>
<p>
       <label for="idpro">Identifiant de produit </label>
       <input type="text" name="idpro" id="idpro" >
       <br />
   </p>
   <p>
       <label for="nom">Nom de produit</label>
       <input type="text" name="nom" id="nom"/>
       <br />
   </p>
   <p>
      <label for="fileToUpload">Photo pour votre produit : </label>
     <input  type="file"  name="file" id="file"/>
    </p>
  <p>
       <label for="descrp">Description votre Produit:</label>
       <input type="text" name="descrip" id="descrip" />
       <br />
   </p>
  <label for="prix">Votre prix minimal</label>
 <input type="text" name="prix" id="prix"/>
 <br/>
 <p>
       <label for="categ">Categorie de produit</label>
       <input type="text" name="categ" id="categ"/>
       <br />
   </p>
 </fieldset>
 <div align="center">
<button type="submit" name="submit" id="submit" >Ajout</button>
<button type="reset">Annuler</button>
</div>
</body>
</html>

PHP代码

<?php
$nom_base_donnees=new mysqli('localhost','root','','enchere') or die ("impossible de se connecter") ; 
if(isset($_POST['submit'])){
    $idpro=$_POST['idpro']; 
    $nom=$_POST['nom'];
    $descrip=$_POST['descrip'];
    $prix=$_POST['prix'];  
    $categ=$_POST['categ'];
    $datetime = date("Y-m-d-H-i-s");
    echo $_FILES["file"]["tmp_name"];
    //$sql="SELECT * FROM produit where produit.idpro=$idpro"; 
    //$res=mysqli_query($nom_base_donnees,$sql);
    //$nb=mysqli_fetch_array($res,MYSQLI_NUM);

    //if ( $nb>0 ) die ("identifiant deja utilisee");
    // else {
    $image = addslashes(file_get_contents($_FILES["file"]["name"]));
    $image_name = addslashes($_FILES["file"]["tmp_name"]);

    $req = "INSERT INTO produit VALUE ('$idpro','$nom','$nameprod','$descrip','$prix','$categ','$image_name','$image')"; 
    $res= mysqli_query($nom_base_donnees,$req);
    if ($res) die ("insertion de produit avec succées") ;    
}
?>

1 个答案:

答案 0 :(得分:0)

使用<div> {{total length of students who passed}} </div>

找到该文件

用户PC上的文件名称位于$_FILES["file"]["tmp_name"]

所以改变这个

$_FILES["file"]["name"]

$image = addslashes(file_get_contents($_FILES["file"]["name"]));
$image_name = addslashes($_FILES["file"]["tmp_name"]);