2015-10-14 59 views
1

使用PHP上傳文件時遇到問題,但大小限制。我設置了上傳的最大大小:5MB,但是當我上傳超過5MB的文件時,它仍然被授予繼續進程的權限,但文件未上傳,如何解決該問題,以便在有人上傳更多內容時出現錯誤超過5MB。這裏是我的代碼:使用PHP上傳文件:運行到大小限制

輸入方式:

<form action="kegiatan_upload_tambah.php" method="post" enctype="multipart/form-data"> 
    <div class="box-body"> 
     <div class="form-group"><label>ID Kegiatan</label> 
     <input type="text" name="id_kgt" class="form-control"> 
     </div> 
     <div class="form-group"><label>Nama Kegiatan</label> 
     <input type="text" name="nama_kgt" class="form-control"> 
     </div> 
     <div class="form-group"><label>Keterangan</label> 
     <textarea name="ket" class="form-control"></textarea> 
     </div> 
     <div class="row"> 
     <div class="col-xs-5"><label>Tanggal Mulai</label> 
      <input type="text" name="tg_mulai" id="tg_mulai" class="form-control" > 
     </div> 
     <div class="col-xs-5"><label>Tanggal Akhir</label> 
      <input type="text" name="tg_akhir" id="tg_akhir" class="form-control" > 
     </div> 
     </div> 
     <br/> 
     <div class="form-group"><label>Pilih File</label> 
     <input type="file" name="file" required> 
     </div> 
    </div></form> 

PHP腳本:

$allowed_ext = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'rar', 'zip'); 
    $file_name = $_FILES['file']['name']; // File adalah nama_kgt dari tombol input pada form 
    $file_ext  = strtolower(end(explode('.', $file_name))); 
    $file_size = $_FILES['file']['size']; 
    $file_tmp  = $_FILES['file']['tmp_name']; 

    $lokasi  = 'file/kegiatan/'.$nama_kgt.'-'.$file_name; 

    if(in_array($file_ext, $allowed_ext) === true) 
    { 
     if($file_size > 5000000) 
     { 
     echo "<script>alert('ERROR: Max File only 5 Mb!');history.go(-1)</script>"; 
     } 
     else 
     { 
     move_uploaded_file($file_tmp, $lokasi); 

     $sql = "INSERT INTO kegiatan (id_kgt,nama_kgt,ket,tg_mulai,tg_akhir,nm_pengirim,type,size,file,jamlog,buatlog) 
     VALUES('$id_kgt','$nama_kgt','$ket','$tg_mulai','$tg_akhir','{$_SESSION['nama']}','$file_ext','$file_size','$lokasi',now(),now())"; 

任何幫助,將這麼多的讚賞。

+0

那麼什麼樣的價值是裏面** $ FILE_SIZE **? – Naruto

+0

你能告訴我''標籤,並且還打印出'$ file_name','$ file_ext','$ file_size'和'$ file_tmp',因爲我認爲文件並沒有真正上傳。 – AkiEru

+0

@AkiEru我更新了輸入表格sir –

回答

1

現在試試這個:

$file_exts = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'rar', 'zip'); 
    $upload_exts = end(explode(".", $_FILES["file"]["name"])); 
       if ((($_FILES["file"]["type"] == "file/doc") 
       || ($_FILES["file"]["type"] == "file/docx") 
       || ($_FILES["file"]["type"] == "file/xls") 
       || ($_FILES["file"]["type"] == "file/xlsx")) 
       || ($_FILES["file"]["type"] == "file/ppt")) 
        || ($_FILES["file"]["type"] == "file/pptx")) 
         || ($_FILES["file"]["type"] == "file/pdf")) 
          || ($_FILES["file"]["type"] == "file/rar")) 
          || ($_FILES["file"]["type"] == "file/zip")) 

       && ($_FILES["file"]["size"] < 5000000) 
       && in_array($upload_exts, $file_exts)) 
         { 
          if ($_FILES["file"]["error"] > 0) 
          { 
           header("Location:index.php?err=filepro1"); 
           //echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
          } 
          else 
          { 
           $tempupname = time().$_FILES["file"]["name"]; 
           $imgpathtostore="files/".$tempupname; 
           $imgpathtostoreDB="files/".$tempupname; 
           //Query 

           } 

           } 
           else 
           { 
            echo "file err"; 
           } 
+0

仍然不行sir –

+0

@AzmiColeJr。試試我的編輯的代碼,我希望它會幫助你。 –

0

嘗試扭轉你的邏輯......

與其說

if($file_size > 5000000) 
{ 
    //do something... 
} 
else 
    die("File cannot exceed 5MB in size"); 

說的....

if($file_size <= 5000000) 
{ 
    //do something.... 
} 
else 
    die("File cannot exceed 5MB in size"); 

而且,確保你的表單標籤具有屬性method =「POST」enctype =「multipart/form-data」。上傳之前,您可能還想檢查文件中是否有錯誤。要做到這一點...

$file_error = $_FILES['file']['error']; 
if($file_error === 0) 
{ 
    //upload file and do more cool things here.... 
} 

希望這有助於...

+0

仍然不工作先生 –