2015-09-17 87 views
2

我想上傳我的表單中的PDF文件,但沒有任何反應。它不會保存數據庫中的文件名,並且文件不會保存到目錄中。 我的PHP代碼是上傳PDF文件PHP

if (isset($_POST['submit'])) { 

    $folder_path = 'health/'; 


    $filename = basename($_FILES['healthfile']['name']); 
    $newname = $folder_path . $filename; 


     if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname)) { 
      if ($_FILES['healthfile']['type'] != "application/pdf") { 

       echo "<p>Class notes must be uploaded in PDF format.</p>"; 
      } else { 
      $filesql = "INSERT INTO tbl_health (link) VALUES ('{$filename}')".die(mysql_error()); 
      $fileresult = mysql_query($filesql, $con).die(mysql_error()); 

      } 

      if ($fileresult) { 
       echo 'Success'; 
      } else { 
       echo 'fail'; 
      } 
     } 

} 

我的形式是

<form action="allhealth.php" method="post" enctype="multipart/form-data"> 

      <label>Upload Your Health Certificate</label> 
       <span class="btn btn-default btn-file"> 
        Browse <input name="healthfile" type="file"> 
       </span> 
      <br/><br/> 
      <button type="button" name="submit" class="btn-success">Submit</button> 
     </form> 

請幫助!

+0

這是一個獨特的'$ filesql'你去那裏 – Drew

+0

這是什麼意思?獨特? – Andrew

+0

'.die(mysql_error())'是語法錯誤。用'或'替換點。我們也不知道你連接的是哪個MySQL API。還要確保該文件夾具有適當的寫入權限。 –

回答

1

使用此

if(isset($_POST["submit"])) 
{ 
    $folder_path = 'health/'; 

    $filename = basename($_FILES['healthfile']['name']); 
    $newname = $folder_path . $filename; 

    $FileType = pathinfo($newname,PATHINFO_EXTENSION); 

    if($FileType == "pdf") 
    { 
     if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname)) 
     { 

      $filesql = "INSERT INTO tbl_health (link) VALUES('$filename')"; 
      $fileresult = mysql_query($filesql); 

      if (isset($fileresult)) 
      { 
       echo 'File Uploaded'; 
      } else 
      { 
       echo 'Something went Wrong'; 
      } 
     } 
     else 
     { 

      echo "<p>Upload Failed.</p>"; 
     } 


    } 
    else 
    { 
     echo "<p>Class notes must be uploaded in PDF format.</p>"; 
    } 

} 

注意上傳文件之前,您應檢查其是否以正確的格式

MySQL擴展已被廢棄在PHP 5.5.0,它是在去除PHP 7.0.0。應該使用MySQLiPDO_MySQ L擴展名。

+0

它仍然不起作用 – Andrew

+0

但是,它似乎沒有工作 – Andrew

+0

有任何錯誤嗎? –

1

使用它現在工作我已經在當地進行過測試。

<?php 
if (isset($_POST['submit'])) { 

    $folder_path = 'health/'; 

    $filename = basename($_FILES['healthfile']['name']); 
    $newname = $folder_path . $filename; 

    if ($_FILES['healthfile']['type'] == "application/pdf") 
    { 
     if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname)) 
     { 

      $filesql = "INSERT INTO tbl_health (link) VALUES('$filename')"; 
      $fileresult = mysql_query($filesql); 
     } 
     else 
     { 

      echo "<p>Upload Failed.</p>"; 
     } 

     if (isset($fileresult)) 
     { 
      echo 'Success'; 
     } else 
     { 
      echo 'fail'; 
     } 
    } 
    else 
    { 
     echo "<p>Class notes must be uploaded in PDF format.</p>"; 
    } 

} 
?> 
<form action="" method="post" enctype="multipart/form-data"> 

      <label>Upload Your Health Certificate</label> 
       <span class="btn btn-default btn-file"> 
        Browse <input name="healthfile" type="file"> 
       </span> 
      <br/><br/> 
      <input type="submit" name="submit" class="btn-success" value="submit"> 
     </form> 
+0

謝謝,它的工作,但文件名doesn' t保存到數據庫 – Andrew

+0

@Andrew你必須保存文件名稱而不是完整的文件。當您訪問該文件時,請提供該目錄的路徑並從數據庫中獲取文件名。 –

+0

@Andrew明白了嗎?建立數據庫連接然後嘗試它。 –