2017-02-23 33 views
0

我有这个形式我profile.phpPHP上的一些文件上传打开一个空白的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> 

这里是脚本/ fileupload.php文件:

<?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(); 
} 
?> 

我在我的本地主机上测试它(我正在使用WAMP),它的所有功能都像一个魅力。它捕获是否是图像,它捕获文件是否大于500kb。如果一切正常,则将图片上传到我所需的目录。

但是我注意到,如果我选择一个大文件(大于3MB),它只会崩溃并打开一个空白页面脚本/ fileupload.php,如果刷新它留在空白页面上没有脚本正在运行。我试图把回声“工作”;作为第一行来查看它是否运行并且不会被执行。

有没有人陷入这个麻烦,你知道任何可能的解决方案吗? 通过测试我注意到,只要我选择一个大于3MB的文件上传不会影响它的图片与否,它只是将uploadfiles.php作为空白页面打开。如果文件低于3MB,它会像魅力一样运行。

现在我不明白为什么它会崩溃,因为我的脚本应该捕获它的大文件,只是给出错误。

非常感谢任何见解!

[编辑]
正如克里斯建议我查了日志,发现这个在PHP错误日志:

PHP Warning: POST Content-Length of 14755394 bytes exceeds the limit of 3145728 bytes in Unknown on line 0 

所以它确实是因为过大的文件大小的崩溃。现在有可能在发出POST请求之前读取文件,以便php不会崩溃?

+0

会认为'upload_max_filesize'和'post_max_size'是开始的好地方。我不清楚你在描述什么。请详细说明'打开空白页面'。我假设加载的页面是您发布的页面,但没有上传文件?使用浏览器开发工具查看实际发生的情况。警告是有道理的。 * *崩溃*不。 – ficuscr

回答

0

如果你还没有,我会建议阅读你的httpd错误日志,看看是否有更多的信息。除此之外,打开你的php.ini文件,并寻找upload_max_file_size,尝试增加值为128M的东西。

编辑(见注释):

<?php 
    if(isset($_FILES['file']) { 
     if($_FILES['file']['size'] > 3145728) { //3 MB 
      // error 
     } else { 
      // fine 
     } 
    } 
+0

是的,我检查了日志,发现这在PHP错误日志中: 'PHP警告:POST内容 - 长度14755394字节超过3145728字节的限制在未知的行0' 因此,它确实崩溃,因为太大的文件尺寸。现在有可能在发出POST请求之前读取文件,以便php不会崩溃? – Marcipanas

+0

听起来好像php.ini配置文件中的max_upload_filesize指令限制了上传文件的大小限制,增加该值(wampmanager图标 - > PHP - > php.ini)应该可以解决脚本崩溃的问题,并允许您上传大文件没有问题。 或者,通过执行以下操作检查文件大小并抛出错误(编辑我的原始答案以包含代码)。 –

相关问题