将HTML表单中的上传文件存储到变量中 - PHP

时间:2017-08-07 12:50:21

标签: php file-upload html-form-post

用户通过表单上传文件。我试图将文件存储在php中的变量中。

HTML code:

<form class="generalForm" action="save.php" method="POST">
    <label class="headLabel">DODAJ</label>
    <br>
    <br>
    <input class="inputTextSub" id="naziv" name="naziv" placeholder="naziv" onfocus="this.placeholder = ''" onblur="this.placeholder = 'naziv'" type="text">
    <br>
    <br>
    <input class="inputTextSub" id="opis" name="opis" placeholder="opis" onfocus="this.placeholder = ''" onblur="this.placeholder = 'naziv'" type="text">
    <br>
    <br>
    <input class="inputTextSub" id="datumz" name="datumz" type="date">
    <br>
    <br>
    <input class="inputTextSub" id="datumk" name="datumk" type="date">
    <br>
    <br>
    <label class="inputTextSub">prioriteta</label>
    <select class="inputSelSub" id="pr" name="pr">
        <option value=1>1</option>
        <option value=2>2</option></select>
    <br>
    <br>
    <label class="inputTextSub">format</label>
    <select class="inputSelSub" id="format" name="format">
        <option value=1>A3</option>
        <option value=2>A4</option></select>
    <br>
    <br>
    <input type="file" name="inputfile" id="file" class="inputfile" />
    <br>
    <br>
    <input class="button" style="margin-top:2em;" name="save" type="submit" value=" shrani ">
</from>

PHP代码:

if(isset($_POST['save'])){
   $naziv = $_POST['naziv'];
   $opis = $_POST['opis'];
   $datumz = $_POST['datumz'];
   $datumk = $_POST['datumk'];
   $pr = $_POST['pr'];
   $format = $_POST['format'];
   $file = file_get_contents($_FILES['inputfile']['tmp_name']); //error occurs here!
   $modId = $_SESSION['mod_id'];
   $stmt = $GLOBALS['conn']->prepare(
   "INSERT INTO `deska`(`mod_id`, `naziv`, `opis`, `datumz`, `datumk`, 
   `prioriteta`, `slika`, `tip`, `datumSpremembe`) 
   VALUES (?,?,?,?,?,?,?,?,CURRENT_DATE)"
   );
   if(!$stmt->bind_param("mod_id",$modId) ||
      !$stmt->bind_param("naziv",$naziv) ||
      !$stmt->bind_param("opis",$opis) ||
      !$stmt->bind_param("datumz",$datumz) ||
      !$stmt->bind_param("datumk",$datumk) ||
      !$stmt->bind_param("prioriteta",$pr) ||
      !$stmt->bind_param("slika",$file) ||
      !$stmt->bind_param("tip",$format)){
          die("Opa, nekaj je šlo narobe! (add)");
      }
   if(!$stmt->execute()){
       die("Opa, nekaj je šlo narobe! (addEx)");
   }
   $stmt->close();    
}

错误是:

  

注意:未定义的索引:输入文件       第10行的C:\ xampp \ htdocs \ mod \ sub \ save.php

     

警告:file_get_contents():文件名不能为空           第10行的C:\ xampp \ htdocs \ mod \ sub \ save.php

我非常感谢你们提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

Buddy您错过了表单标记中的一个属性 enctype =“multipart / form-data”只需添加,因为对于文件我们必须添加此属性