一些文件上传时,PHP会打开一个空白的PHP页面,而不是运行脚本

一些文件上传时,PHP会打开一个空白的PHP页面,而不是运行脚本,php,file-upload,Php,File Upload,我在profile.php中有这个表单 <form action="script/fileupload.php" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="fileToUpload">Select image to upload:</label> <input type="file" name="f

我在profile.php中有这个表单

<form action="script/fileupload.php" method="post" enctype="multipart/form-data">
    <div class="form-group">
    <label for="fileToUpload">Select image to upload:</label>
    <input type="file" name="fileToUpload" id="fileToUpload">
  </div>
    <input type="hidden" name="UploadProfilePicture" value="UploadProfilePicture">
  <input type="submit" class="btn btn-primary" value="Upload Image" name="submit">
</form>

所以它确实会因为文件太大而崩溃。现在是否可以在发出POST请求之前读取该文件,以便php不会崩溃?

如果您还没有,我建议您阅读您的httpd错误日志,以查看是否有更多信息。除此之外,打开php.ini文件并查找upload\u max\u file\u size,尝试将该值增加到128M左右

编辑(见评论):


是的,我检查了日志,在PHP错误日志中发现了这一点:
PHP警告:POST内容长度14755394字节超过了第0行未知中3145728字节的限制
因此它确实会崩溃,因为文件太大。现在可以在发出POST请求之前读取文件了吗?这样php就不会崩溃?听起来好像php.ini配置文件中的max_upload_filesize指令限制了上传文件大小限制,增加了该值(wampmanager icon->php->php.ini)应该可以解决脚本崩溃的问题,并允许您上传更大的文件而不会出现问题。或者,检查文件大小并通过执行以下操作抛出错误(编辑我的原始答案以包含代码)。我认为
upload\u max\u filesize
post\u max\u size
是很好的开始。我不清楚你在描述什么。请详细说明“打开空白页”。我假设加载的页面是您发布到的页面,但没有上载文件?使用浏览器开发工具查看实际发生的情况。警告是有道理的。撞车不算什么。
<?php
//open  session
session_start();
//Server requests
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if(isset($_POST['UploadProfilePicture'])){
    upload_profile_picture();
  }
}
function upload_profile_picture(){
  //Set where to put picture and the file name after upload
  $target_dir = dirname(__DIR__)."/uploads/profile-pictures/";
  $imageFileType = pathinfo(basename($_FILES["fileToUpload"]["name"]),PATHINFO_EXTENSION);
  $genereateFileName = 'profile_' . date('Y-m-d-H-i-s') . '_' . uniqid() .".".$imageFileType;
  $target_file = $target_dir . $genereateFileName;
  $location = "uploads/profile-pictures/".$genereateFileName;
  $uploadOk = 1;
  // Check if image file is a actual image or fake image
  if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        $error = "File is not an image.";
        $uploadOk = 0;
    }
  }
  // Check if file already exists
  if (file_exists($target_file)) {
      $error = "Sorry, file already exists. Please change the name and upload again.";
      $uploadOk = 0;
  }
  // Check file size
  if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo 'yes';
      $error = "Sorry, your file is too large. Maximum file dize 500kb";
      $uploadOk = 0;
  }
  // Allow certain file formats
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
      $error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
      $uploadOk = 0;
  }
  //Check if user is logged in
  if(!isset($_SESSION['id'])){
    $error = "Only registered users can upload pictures.";
    $uploadOk = 0;
  }
  // if everything is ok, try to upload file
  if ($uploadOk == 1) {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        $text = "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        $error = "Sorry, there was an error uploading your file. Try again or contact administration";
        $uploadOk = 0;
    }
  }
  //Redirect back to page
  if($uploadOk == 1){
    $_SESSION['text'] = $text;
  } else {
    $_SESSION['error'] = $error;
  }
  header("Location: ../profile.php");
  die();
}
?>
PHP Warning: POST Content-Length of 14755394 bytes exceeds the limit of 3145728 bytes in Unknown on line 0   
<?php
  if(isset($_FILES['file']) {
      if($_FILES['file']['size'] > 3145728) { //3 MB
          // error
      } else {
          // fine
      }
  }