文件上载警告帖子内容 - 字节长度超出限制

时间:2012-11-29 16:41:17

标签: php javascript file-upload localhost

我正在制作文件上传系统。用户使用动态下拉列表选择上载位置。这就是我现在面临的问题。 这是我的表格。

<form action="uploader.php" method="POST" enctype="multipart/form-data" name="uploads">

            <label for="file">Choose a file: </label> 

                <input type="file" name="userfile" id="userfile"><br/><br/>

                    <select id="text-one" name="one"> 

                        <option selected value="base">Select Department</option>
                        <option value="CSE" name="cse">Computer Science Engineering</option>
                        <option value="ECE" name="ece">Electronics & Communication Engineering</option>
                        <option value="MECH" name="mech">Mechanical Engineering</option>

                    </select>

                    <br /><br/>

                    <select id="text-two" name="two">
                        <option>Select Semester</option>
                    </select>

                    <br /><br/>

                    <select id="text-three" name="three">
                        <option>Select Subject</option>
                    </select>

                    <br/><br/>

            <button class ="btn btn-primary" button type="submit" name="upload" value="Upload" onClick="val()">Upload</button>

        </form>

这是我链接的其他php文件。

<?php
if(isset($_POST['upload']))
{ 
  $path1=$_POST['one']."/"; 
  $path2=$_POST['two']."/"; 
  $path3=$_POST['three']."/";   
  $upload_path=$path1.$path2.$path3;
 }
else
 {
  echo "Select a Subject";
  echo "<br>";
}
$allowed_filetypes = array('.doc','.docx','.jpg','.jpeg','.png','.ppt','.pptx','.xls','.xlsx','.pdf','.txt','.zip','.rar'); 
$max_filesize = 20000000; 
$filename = $_FILES['userfile']['name']; 
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
  die("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('You cannot upload the following type of file!')
        window.location.href='upload.php';
     </SCRIPT>");
if(filesize($_FILES['userfile']['size']) > $max_filesize)
  die("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('The file you attempted to upload is too large!')
        window.location.href='upload.php';
     </SCRIPT>");
if(!is_writable($upload_path))
  die("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('You cannot upload to the specified directory!')
        window.location.href='upload.php';
     </SCRIPT>");
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
  echo ("<SCRIPT LANGUAGE='JavaScript'>
          window.alert('Your file has been uploaded successfully')
          window.location.href='upload.php';
        </SCRIPT>");
else
  echo ("<SCRIPT LANGUAGE='JavaScript'>
          window.alert('There was an error during the file upload!')
          window.location.href='upload.php';
        </SCRIPT>");
?>

我已经使用以下设置编辑了phpini文件,并在php文件夹中创建了一个.htaccess文件。

upload_max_filesize 25M post_max_size 25M memory_limit 64M

但是当我故意上传大于25 MB的文件时,我会收到标题中给出的错误。此外,考虑到违反了最大文件大小,它不会给出与文件大小相关的错误,即您尝试上传的文件太大,它表示您无法上传以下类型的文件。并且在后台发布了长度警告事件。

请帮我解决这个问题。我在我的本地主机上。

1 个答案:

答案 0 :(得分:1)

post_max_size表示所有文件大小+其他帖子字段的总和 post_max_size应始终大于upload_max_filesize

尝试将post_max_size设置为26M

添加了: 所有Firs都检查是否设置了$ _FILES ['userfile'],即:

if(!isset($_FILES['userfile'])) 
  die("<SCRIPT LANGUAGE='JavaScript'> 
       window.alert('No file upload !');
       window.location.href='upload.php'; 
      </SCRIPT>");