3
if (($_FILES["fileToUpload"]["type"] == "image/gif")
  || ($_FILES["fileToUpload"]["type"] == "image/jpeg")
  || ($_FILES["fileToUpload"]["type"] == "image/png" )
   &&($_FILES["fileToUpload"]["size"] <= 100))
  {
    echo $_FILES["fileToUpload"]["size"] ;
  move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "C:/upload/" . $_FILES["fileToUpload"]["name"]);
  }
elseif (($_FILES["fileToUpload"]["type"] == "image/gif")
  || ($_FILES["fileToUpload"]["type"] == "image/jpeg")
  || ($_FILES["fileToUpload"]["type"] == "image/png" )
  &&($_FILES["fileToUpload"]["size"] <= 1000000))
  {
    echo $_FILES["fileToUpload"]["size"] ;
   move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "C:/image/" . $_FILES["fileToUpload"]["name"]);
  }

   else
  {
  echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
  }

但我的文件只保存在上传文件夹中。我想如果文件大小小于 100kb,那么它应该保存在上传文件夹中,否则它应该保存在图像中。请帮忙

4

5 回答 5

0

因为&&运算符的优先级高于||. 使用这些括号

if (($_FILES["fileToUpload"]["type"] == "image/gif"
  || $_FILES["fileToUpload"]["type"] == "image/jpeg"
  || $_FILES["fileToUpload"]["type"] == "image/png")
   && $_FILES["fileToUpload"]["size"] <= 100)

在你所有的条件下。我还删除了一些多余的括号,因为==and<=运算符的优先级高于逻辑运算符(除了!)。另请参阅php 运算符优先级

于 2013-05-14T07:14:30.203 回答
0

你写道:if(a || b || c && d)。这被解释为if(a || b || (c && d) ),因此只有 PNG 将存储在“图像”文件夹中。试试这个代码:

if (($_FILES["fileToUpload"]["type"] == "image/gif")
  || ($_FILES["fileToUpload"]["type"] == "image/jpeg")
  || ($_FILES["fileToUpload"]["type"] == "image/png" )
  {
    if($_FILES["fileToUpload"]["size"] <= 100){
      ...
    }elseif($_FILES["fileToUpload"]["size"] <= 1000000){
      ...
    }else{
      echo "Files must be less than 10,000 kb";
    }    
  }else{
    echo "Files must be either JPEG, GIF, or PNG";
  }
于 2013-05-14T07:16:19.433 回答
0

尝试类似:

if($_FILES["fileToUpload"]["type"] == "image/gif" || $_FILES["fileToUpload"]["type"] == "image/jpeg" || $_FILES["fileToUpload"]["type"] == "image/png"){
    if($_FILES["fileToUpload"]["size"] <= 100){
        echo $_FILES["fileToUpload"]["size"];
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "C:/upload/" . $_FILES["fileToUpload"]["name"]);
    }elseif($_FILES["fileToUpload"]["size"] <= 1000000){
        echo $_FILES["fileToUpload"]["size"] ;
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "C:/image/" . $_FILES["fileToUpload"]["name"]);
    }else{
        #file size error
    }
}else{
    echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
}
于 2013-05-14T07:16:40.247 回答
0

size 的值可以是一个字符串。我没有测试过。但你可以试试这个:

if (in_array($_FILES["fileToUpload"]["type"], array("image/gif", "image/jpeg", "image/png")) {
  if (((int) $_FILES["fileToUpload"]["size"]) <= 100) {

    echo $_FILES["fileToUpload"]["size"] ;
  move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "C:/upload/" . $_FILES["fileToUpload"]["name"]);
  }
elseif (((int) $_FILES["fileToUpload"]["size"]) <= 1000000)
  {
    echo $_FILES["fileToUpload"]["size"] ;
   move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "C:/image/" . $_FILES["fileToUpload"]["name"]);
  }
   else
  {
  echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
  }
于 2013-05-14T07:17:19.873 回答
0

全面检查错误。

<?php

$upload_key = 'fileToUpload';

if (isset($_FILES[$upload_key])) {

    try {

        $error = $_FILES[$upload_key]['error'];
        switch ($error) {
            case UPLOAD_ERR_INI_SIZE:
                throw new Exception('Exceeded upload_max_filesize');
            case UPLOAD_ERR_FORM_SIZE:
                throw new Exception('Exceeded MAX_FILE_SIZE');
            case UPLOAD_ERR_PARTIAL:
                throw new Exception('Incomplete file uploaded');
            case UPLOAD_ERR_NO_FILE:
                throw new Exception('No file uploaded');
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new Exception('No tmp directory');
            case UPLOAD_ERR_CANT_WRITE:
                throw new Exception('Can\'t write data');
            case UPLOAD_ERR_EXTENSION:
                throw new Exception('Extension error');
        }

        $finfo    = new finfo(FILEINFO_MIME);
        $name     = $_FILES[$upload_key]['name'];
        $tmp_name = $_FILES[$upload_key]['tmp_name'];
        $size     = $_FILES[$upload_key]['size'];

        if ($size > 1000000)
            throw new Exception('Exceeded 10000KB limit');
        if (!is_uploaded_file($tmp_name))
            throw new Exception('Not an uploaded file');

        $type = $finfo->file($tmp_name);

        if ($type === false)
            throw new Exception('Failed to get MimeType');
        if (
            $type !== 'image/png; charset=binary'  &&
            $type !== 'image/jpeg; charset=binary' &&
            $type !== 'image/gif; charset=binary'
        )
            throw new Exception('Only PNG, JPEG, GIF available');

        $dir = ($size <= 100) ? 'C:/upload/' : 'C:/image/';
        $new_name = $dir.$name;

        if (!move_uploaded_file($tmp_name,$new_name))
            throw new Exception('Failed to move uploaded file');

        echo 'File successfully uploaded';

    } catch (Exception $e) {

        echo 'Error: '.$e->getMessage();

    }

}
于 2013-05-14T07:46:07.660 回答