2017-04-21 83 views
0

我試圖用一些形式的數據一起上傳文件一起發佈的數據。它對於小文件來說工作得很好。但是,當我試圖將數據與大文件一起提交時,它不捕獲數據。無法與大型文件上傳PHP

這是形式:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <div class="row"> 
      <div class="col-md-2"></div> 
      <div class="col-md-8"> 
       <input type="text" class="form-control" name="title" required placeholder="Title"> 
      </div> 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="row"> 
      <div class="col-md-2"></div> 
      <div class="col-md-8"> 
       <input type="file" class="form-control" name="report" required> 
      </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-md-4"></div> 
     <div class="col-md-4"> 
      <center> 
       <input type="submit" name="upload_report" value="Upload" class="btn btn-primary btn_font"> 
      </center> 
     </div> 
    </div> 
    </form> 

PHP代碼:

if(isset($_POST["upload_report"])) { 


     echo $_POST['title']; // giving title index undefined 



     $max_report_id += 1; 
     $target_dir = "Reports/"; 
     $file_title = mysqli_real_escape_string($connection,trim($_POST['title'])); 
     $file_name = mysqli_real_escape_string($connection,trim($_FILES['report']["name"])); 
     //$file_size = mysqli_real_escape_string($connection,trim($_FILES['report']["size"])); 
     $target_file = $target_dir . basename($file_name); //basename gets filename from path 
     $uploadOk = 1; 
     $file_type = pathinfo($target_file,PATHINFO_EXTENSION); 
     $target_file = $target_dir . $max_report_id . "." . $file_type; 

     // Check if file already exists 
     if (file_exists($target_file)) { 
      //echo "Sorry, file already exists."; 
      $uploadOk = 0; 
     } 
     /* Check file size 
     if ($file_size > 5000000) { 
      echo "Sorry, your file is too large."; 
      $uploadOk = 0; 
     } 
     */ 
     // Check if $uploadOk is set to 0 by an error 
     if ($uploadOk == 0) { 
      $status_fail = true; 
      $fail_message = "Sorry, there was an error uploading your file."; 
     // if everything is ok, try to upload file 
     } else { 
      if (move_uploaded_file($_FILES["report"]["tmp_name"], $target_file)) { 
       $status_success = true; 
       $success_message = "The file ". $file_title. " has been uploaded."; 
       $query = "insert into reports(title,fname,path,platform,added_by,added_on) values ('$file_title','$file_name','$target_file','$platform','$user_id',now())"; 
       $result = mysqli_query($connection, $query) or die($query); 
      } 
      else { 
       $status_fail = true; 
       $fail_message = "Sorry, there was an error uploading your file."; 
      } 
     } 
    } 

我有足夠的更新max_post_size,memory_limit的,max_upload_size在php.ini文件。

+0

旁註:http://bobby-tables.com - 瞭解更多的安全準備好的語句。 real_escape_string不足以防止SQL注入。 – Twinfriends

回答

0

默認PHP值爲upload_max_filesize爲2 MB,post_max_size爲8 MB。根據您的主機上,改變這兩個PHP變量可以在一些最有可能是php.ini或任何的.htaccess地方進行(根據您的主機的情況)。 您需要在php.ini中設置upload_max_filesize和post_max_size的值:

;上傳文件的最大允許大小。

的upload_max_filesize = 40M(前40MB,你可以改變更多的大小)

;必須大於或等於upload_max_filesize的

的post_max_size = 40M(例如,40MB,你可以改變更多的大小)

修改php.ini文件(一個或多個)後,需要重新啓動HTTP服務器使用新的配置。

+0

我試過改變這些。仍然面臨同樣的問題。 –

+0

是否有任何錯誤消息? – Gangga

+0

錯誤就像$ _POST [「標題」],索引(即,標題)是未定義的。它從html表單本身發送郵件時未設置。 –