简体   繁体   English

上传大图像时出现问题

[英]Issue when uploading large images

Hi I have this which I picked from a tutorial. 嗨,我有从教程中挑选的这个。 Link here Try help me understand this a bit. 链接到这里尝试帮助我稍微了解一下。

  1. I want to know how the coder has attempted to control the size of the image uploading. 我想知道编码人员如何尝试控制图片上传的大小。 (I've seen elsewhere in codes clearly restricting the size ie ($_FILES["file"]["size"] < 20000) ) (我在代码的其他地方已经清楚地限制了大小,即($_FILES["file"]["size"] < 20000)

  2. Reason why im asking the above question is when I upload a smaller image this works fine but a larger image gives a series of Warnings! 我问以上问题的原因是当我上传较小的图像时效果很好,但是较大的图像会发出一系列警告!

Thank you for your help. 谢谢您的帮助。

Code

<?php

function getExtension($str)
{
 $i = strrpos($str,".");
 if (!$i) { return ""; }

 $l = strlen($str) - $i;
 $ext = substr($str,$i+1,$l);
 return $ext;
}

$image =$_FILES["imagefile"]["name"];
$tempext = getExtension($image);
$extfile= mt_rand().".".$tempext;
$uploadedfile = $_FILES['imagefile']['tmp_name'];

if ($extfile)
{
$filename = stripslashes($extfile);
$extension = getExtension($filename);   // return the type of image
$extension = strtolower($extension);    // convert result to lowercase
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
$errors=1;
}
else
{
$size=filesize($_FILES['imagefile']['tmp_name']);

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['imagefile']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['imagefile']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

list($width,$height)=getimagesize($uploadedfile);

$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0, $newwidth, $newheight, $width, $height);

$newwidth1=185;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0, $newwidth1, $newheight1, $width, $height);

$filename = "upload/". $extfile;
$filename1 = "upload/thumbs/". $extfile;  
$etest1=imagejpeg($tmp,$filename,100);
$etest2=imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
?>

maybe this is better and more zipped 也许这更好,更拉链

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2097152)) //2 MB

尝试使用下面的示例,它是标准的示例,它将使您可以控制问题中提到的文件大小(即$ _FILES [“ file”] [“ size”] <20000) 示例。

Without posting your warnings, we cannot determine what the real problem might be. 如果不发布您的警告,我们将无法确定真正的问题所在。 However I suspect that is related to your php.ini file (it is the file where PHP keeps configurations). 但是,我怀疑这与您的php.ini文件(PHP用来保存配置的文件)有关。

The coder you mention on your link hasn't attempted to restrict the file size. 您在链接上提到的编码器尚未尝试限制文件大小。 Although you do not have an explicit statement in your code stating that the file size should not be larger than 20MB, this doesn't mean that you can upload a file of any size you wish. 尽管您的代码中没有明确声明文件大小不应大于20MB,但这并不意味着您可以上传任何大小的文件。

In your case, there must be some directive in php.ini that prevents you from uploading files larger than 20M. 在您的情况下,php.ini中必须有一些指令可以阻止您上传大于20M的文件。 Once you find php.ini (google where to find it, based on your OS) you can change the configuration of php.ini like below: 找到php.ini(根据您的操作系统,在Google哪里找到它),您可以更改php.ini的配置,如下所示:

; Max size allowed for file upload
upload_max_filesize = 20M

; This must be greater than or equal to upload_max_filesize
post_max_size = 21M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM