2017-08-07 59 views
0

用戶通過表單上傳文件。我試圖將文件存儲在一個變量在PHP中。將上傳的文件從HTML表單存儲到變量中 - PHP

HTML代碼:

<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();  
} 

的錯誤是:

說明:未定義指數:在 C輸入文件:\ XAMPP \ htdocs中\第10行上的mod \ sub \ save.php

警告:file_get_contents()函數:文件名不能在 C是空的:\ XAMPP \ htdocs中\ MOD \子\上線10

save.php我會很感激的任何幫助,你們可以提供。

+0

您需要使用'move_uploaded_file()以'(如在文檔中說明),然後獲取文件的內容。 –

+0

還需要表單的'enctype'屬性。 –

+1

更改至 –

回答

0

哥們你已經錯過了在表單標記一個屬性ENCTYPE =「的multipart/form-data的」只想補充一點,因爲文件,我們要添加這個屬性