Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 高度和对上传机的要求_Php_File Upload - Fatal编程技术网

Php 高度和对上传机的要求

Php 高度和对上传机的要求,php,file-upload,Php,File Upload,我目前已经创建了这个脚本 <?php $allowedExts = array("png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_F

我目前已经创建了这个脚本

<?php
$allowedExts = array("png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];

    $path = "/path/to/file";
    move_uploaded_file($_FILES["file"]["tmp_name"], $path."/".$_SESSION['Username'].".png");

    }
  }
else
  {
  echo "Invalid file";
   }
?>


我希望为上传的图像指定最大高度和宽度。我该怎么做?

您需要使用PHP函数
getimagesize()

getimagesize()函数不需要GD库

试试这个:

<?php
    // Set maximum width and height in pixels
    $maxwidth = 4000;
    $maxheight = 4000;

    $allowedExts = array("png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($extension, $allowedExts))
      {
        // List the width, height, image type, img attributes of the uploaded file into the specified variables
        list($imgwidth, $imgheight, $imgtype, $imgattr) = getimagesize($_FILES["file"]["tmp_name"]);

        // If the image is too wide, or if the image is too tall, don't upload and tell the user.
        if($imgwidth < $maxwidth || $imgheight < $maxheight){
            if ($_FILES["file"]["error"] > 0)
            {
                echo "Error: " . $_FILES["file"]["error"] . "<br>";
            }
            else
            {
                echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                echo "Type: " . $_FILES["file"]["type"] . "<br>";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                echo "Stored in: " . $_FILES["file"]["tmp_name"];

                $path = "/path/to/file";
                move_uploaded_file($_FILES["file"]["tmp_name"], $path."/".$_SESSION['Username'].".png");

            }
        }else{
            echo "File width or height is too large.";
        }
    }
    else
    {
        echo "Invalid file";
    }
?>