如何在不同的文件夹中保存不同大小的不同文件

时间:2013-05-14 07:10:44

标签: php

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,那么它应该保存在上传文件夹中,否则它应该保存在图像中。请帮忙

5 个答案:

答案 0 :(得分:0)

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

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

在所有情况下。我还删除了一些冗余括号,因为==<=运算符的优先级高于逻辑运算符(!除外)。另请参阅php operators precedence

答案 1 :(得分:0)

您写道:if(a || b || c && d)。这被解释为if(a || b || (c && d) ),因此只有PNG将存储在'image'文件夹中。 试试这段代码:

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";
  }

答案 2 :(得分: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";
}

答案 3 :(得分: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";
  }

答案 4 :(得分: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();

    }

}