2014-12-28 60 views
-1

我試圖從兩天的文件上傳表單,但似乎無法得到這個工作。我的代碼是檢查文件的擴展名,但沒有檢查文件大小。我GOOGLE了一下,嘗試了不同的方法,但無法讓這個工作。有人可以幫忙嗎?PHP文件上傳 - 沒有檢查文件大小

下面的代碼 -

<?php 
      if(isset($_POST['carsubmit'])) 
      {        
       foreach($_POST as $key=>$val) 
       ${$key}=addslashes($val); 

       $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 

       $max_filesize = 2097152; 

       $upload_path = "resumes/"; 

       $filename = $_FILES['attachresume']['name'];      

       $file_tmp =$_FILES['attachresume']['tmp_name']; 

       $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 


       $cardupcheck = "select * from `careers` where `email` = '$email'"; 
       $cardupresult = mysql_query($cardupcheck); 

       if(mysql_num_rows($cardupresult)==1) 
       { 
        ?> 
        <script type="text/javascript">   
        notification('You have already sent us!','error'); 
        </script> 
        <?php 
       } 
       else 
       { 
        if(!in_array($ext,$allowed_filetypes)){ 
        ?> 
        <script type="text/javascript">   
         notification('Please check the file extension. Only jpg, png and gif are allowed!','error'); 
        </script> 
        <?php 
        } 
        else if($file_tmp > $max_filesize){ 
        ?> 
        <script type="text/javascript">   
         notification('too large!','error'); 
        </script> 
        <?php 
        } 
        else 
        { 
         move_uploaded_file($file_tmp,"resumes/".$filename); 
         $carquery = "INSERT INTO `careers` (`name`, `email`, `phone`, `aoi`, `qual`, `resume`) VALUES ('$name', '$email', '$phone', '$aoi', '$qual', '$filename')"; 
         $carresult = mysql_query($carquery); 
         if($carresult) 
         { 
          ?> 
          <script type="text/javascript">   
          notification('Thank you! We will get back to you soon!','success'); 
          </script>   
          <?php 
         } 
         else 
         { 
          ?> 
          <script type="text/javascript">   
          notification('There was an error. Please try after some time!','error'); 
          </script> 
          <?php 
         } 
        }         
       } 
      } 
      ?> 
+0

http://php.net/manual /en/features.file-upload.php - http://php.net/manual/en/function.filesize.php - 其中一個代碼不包含也不檢查。你會在其中一個鏈接中找到你的答案。 –

+0

首先查看你使用的任何httpd的錯誤日誌。如果您使用的是Apache,請查看/var/log/apache2/error.log,查看該腳本運行時記錄的錯誤。 – Aroll605

+0

我也認爲你的代碼中有一個'notification()'函數,未顯示? –

回答

1

您正在使用的文件名比較文件大小。通過$_FILES["attachresume"]["size"]獲取上傳文件的大小。用這個代碼,而不是

<?php 
      if(isset($_POST['carsubmit'])) 
      {        
       foreach($_POST as $key=>$val) 
       ${$key}=addslashes($val); 

       $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); 

       $max_filesize = 2097152; 

       $upload_path = "resumes/"; 

       $filename = $_FILES['attachresume']['name'];      

       $file_tmp =$_FILES['attachresume']['tmp_name']; 

       $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 
$file_size = $_FILES["attachresume"]["size"]; // Here is the size of the uploaded file 

       $cardupcheck = "select * from `careers` where `email` = '$email'"; 
       $cardupresult = mysql_query($cardupcheck); 

       if(mysql_num_rows($cardupresult)==1) 
       { 
        ?> 
        <script type="text/javascript">   
        notification('You have already sent us!','error'); 
        </script> 
        <?php 
       } 
       else 
       { 
        if(!in_array($ext,$allowed_filetypes)){ 
        ?> 
        <script type="text/javascript">   
         notification('Please check the file extension. Only jpg, png and gif are allowed!','error'); 
        </script> 
        <?php 
        } 
        else if($file_size > $max_filesize){ 
        ?> 
        <script type="text/javascript">   
         notification('too large!','error'); 
        </script> 
        <?php 
        } 
        else 
        { 
         move_uploaded_file($file_tmp,"resumes/".$filename); 
         $carquery = "INSERT INTO `careers` (`name`, `email`, `phone`, `aoi`, `qual`, `resume`) VALUES ('$name', '$email', '$phone', '$aoi', '$qual', '$filename')"; 
         $carresult = mysql_query($carquery); 
         if($carresult) 
         { 
          ?> 
          <script type="text/javascript">   
          notification('Thank you! We will get back to you soon!','success'); 
          </script>   
          <?php 
         } 
         else 
         { 
          ?> 
          <script type="text/javascript">   
          notification('There was an error. Please try after some time!','error'); 
          </script> 
          <?php 
         } 
        }         
       } 
      } 
      ?> 

希望這有助於你

+1

好你轉換我的評論作爲答案。我認爲這個問題不好,因爲OP只是犯了個愚蠢的錯誤。 –

+0

@Shaiful,Hudixt - 感謝您的回覆。 –

+0

不客氣 –

0

這一個工作 -

else if(($_FILES['attachresume']['size'] >= $max_filesize) || ($_FILES["attachresume"]["size"] == 0))

+0

你不應該寫評論作爲答案。 –