php上传错误消息不正确

时间:2017-11-19 11:58:05

标签: javascript php

我有这两个文件,包含upload.php,用于上传文件,其中各自的错误保存在会话变量中;第二个文件,用于检查会话变量错误,以显示相应错误的警报消息。但我得到的错误是“文件类型不受支持”,即使支持而不是获取“上传大小应小于10MB”左右。

upload.php的=>

<?php
session_start();
include_once 'dbh.inc.php';
$sessionid = $_SESSION['u_id'];

  $file= $_FILES['file'];
  $fileName= $file['name'];
  $fileTmpName= $file['tmp_name'];
  $fileSize= $file['size'];
  $fileError= $file['error'];
  $fileType= $file['type'];
  $fileExt = explode('.',$fileName);
  $fileActualExt = strtolower(end($fileExt));

  $allowed = array('jpg','jpeg','png','pdf','tiff','bmp');

  if(in_array($fileActualExt,$allowed)){
    if($fileError=== 0){
      if($fileSize<15728640){
        $fileDestination = '../files/'.$sessionid.'/'.$fileName;
        move_uploaded_file($fileTmpName,$fileDestination);
        header("Location: ../index.php?upload=success");
      } else {
        $_SESSION['errors']['size'] = 1;
        session_write_close();
        header("Location: ../index.php?upload=size_exceeded_10MB");
      }
    } else {
      $_SESSION['errors']['upload'] = 1;
      session_write_close();
      header("Location: ../index.php?upload=error");
    }
  } else{
    $_SESSION['errors']['type'] = 1;
    session_write_close();
    header("Location: ../index.php?upload=typeerror");
  }

error.php =&GT;

<?php
          if(isset($_SESSION['errors']['size']) && $_SESSION['errors']['size'] == 1){ ?>
              <script type="text/javascript">
                alert('File should be less than 3MB!');
              </script>
          <?php }
          else if(isset($_SESSION['errors']['upload']) && $_SESSION['errors']['upload'] == 1){ ?>
              <script type="text/javascript">
                alert('Error uploading the file!');
              </script>
          <?php }
          else if(isset($_SESSION['errors']['type']) && $_SESSION['errors']['type'] == 1){ ?>
              <script type="text/javascript">
                alert('Filetype not supported!');
              </script>
<?php } ?>

<?php
if(isset($_SESSION['errors'])){
    unset($_SESSION['errors']);
} ?>

1 个答案:

答案 0 :(得分:0)

For&#34; Filetype不受支持&#34;错误,请尝试在$allowed

中使用此代码
$allowed = array('image/jpg','image/jpeg','image/png','image/pdf','image/tiff','image/bmp');

For&#34;上传大小应小于10MB&#34;错误,检查你的php文件中的php.ini

upload_max_filesize = 20M
post_max_size = 21M