2016-06-13 92 views
1

形式:文件信息沒有傳遞到PHP

<form enctype="multipart/form-data" action="FileUpload.php" method="post"> 
        <label for="file" class="myLabel">Select File</label> 
        <input type="file" id="file" name="fileUpload"> 
        <input type="submit" class="submit" value="Upload File"> 

      </form> 

PHP:

if(isset($_FILES['fileUpload'])){ 
     $uploadName = $_FILES['fileUpload']['name']; 
     $uploadTmp = $_FILES['fileUpload']['tmp_name']; 
     $fileSize = $_FILES['fileUpload']['size']; 
     $fileType = pathinfo($uploadName, PATHINFO_EXTENSION); 
     $uploadName = trim($uploadName, "." . $fileType); 

     $uploadName = preg_replace("#[^a-z0-9.]#i","",$uploadName);//removes all spaces in their name 

     $uploadName = mt_rand(100000,999999) . "____" . $uploadName;//generates random number in front of name so multiple files of the smae name can be uploaded 

     if(($fileSize > 100024352638512)){//check file is less than 10gb 
      die("Error - File to big"); 
     } 

     if(!$uploadTmp){//checks to see if file is selected 
      die("No File Selected, Please Upload Again"); 
     }else{ 
      move_uploaded_file($uploadTmp,"uploads/$uploadName");//puts file into uploads directory 
     } 

    $sql = $con->query("INSERT INTO uploads (ID, Time ,Name, Type, Owner) VALUE 
    (NULL,CURRENT_TIMESTAMP(),'{$uploadName}','{$fileType}','{$_SESSION['UserID']}')")or die(mysqli_error($con));//inserts file into database 

     header("Location:Profile.php");//sends you to profile page 
     } 

的代碼被執行到結束,但該文件沒有上傳到數據庫中,我有PHP之前工作,但我改變了輸入按鈕的工作方式,現在它不工作。我會很感激任何幫助,因爲我還是一位新開發人員。

+0

你期待實際的文件進入數據庫或只是文件名? – developerwjk

回答

0

沿着Lucha Laura的回答 - 如果文件上傳併成功移動,我會懷疑你插入的值違反了數據庫字段的約束。

將NULL插入主鍵在MySQL中有效,但其他字段可能沒有正確配置。檢查字段的字段類型,長度和not_null要求,並確保它們接受您發送的值。預覽實際值的簡單方法是使用error_log()將信息發送到錯誤日誌。

有點無關 - trim()不會做你正在努力完成的事情。看看strstr()preg_replace()