【问题标题】:Failed to open stream when uploa image in php // using Mysql在php中上传图片时打开流失败//使用Mysql
【发布时间】:2018-04-02 23:37:16
【问题描述】:

我正在尝试上传产品图片并将所有信息(id prod、name、category ...)插入到表 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") ;    
}
?>

【问题讨论】:

    标签: php file-upload image-upload


    【解决方案1】:

    使用$_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"];
    

    【讨论】:

    • 非常感谢@RiggsFolly
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2014-07-22
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    相关资源
    最近更新 更多