代码之家  ›  专栏  ›  技术社区  ›  ahmed

使用Mysql在php//中上载图像时无法打开流

  •  -1
  • ahmed  · 技术社区  · 7 年前

    我正在尝试上载产品的图像并插入所有信息(id prod、名称、类别…)进入表produit。

    我的代码返回错误的问题:

    警告:file\u get\u contents(26754765\u 956068941216475\u 1243103994\u n.jpg):无法打开流:C:\xampp\htdocs\PFE\upload中没有此类文件或目录。php在线17

    有什么帮助吗?? bellow my code 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 回复  |  直到 7 年前
        1
  •  0
  •   RiggsFolly    7 年前

    通过使用 $_FILES["file"]["tmp_name"]

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

    所以改变这个

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

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