我编写了一个脚本,用于将图像上传到我的服务器。用户可以使用它来上传他们的头像或他们的作品。
这是functions.php中的函数
function save_image($image, $uploadedfile)
{
error_reporting(0);
$change="";
$abc="";
define ("MAX_SIZE","40000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if ($image)
{
$filename = stripslashes($image);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}
else
{
$size=filesize($uploadedfile);
if ($size > MAX_SIZE*1024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $uploadedfile;
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $uploadedfile;
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=$width;
$newheight=$height;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "images/vcard_images/". $image;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
}
所以现在我在 create.php 页面上有一个表单,他们可以在其中上传 6 个作品。所以他们从硬盘中选择了 6 个文件,然后按保存。然后我像这样调用函数 6 次:
$work1 = $_FILES["work1"]["name"];
save_image($_FILES["work1"]["name"], $_FILES['work1']['tmp_name']);
$work2 = $_FILES["work2"]["name"];
save_image($_FILES["work2"]["name"], $_FILES['work2']['tmp_name']);
$work3 = $_FILES["work3"]["name"];
save_image($_FILES["work3"]["name"], $_FILES['work3']['tmp_name']);
$work4 = $_FILES["work4"]["name"];
save_image($_FILES["work4"]["name"], $_FILES['work4']['tmp_name']);
$work5 = $_FILES["work5"]["name"];
save_image($_FILES["work5"]["name"], $_FILES['work5']['tmp_name']);
$work6 = $_FILES["work6"]["name"];
save_image($_FILES["work6"]["name"], $_FILES['work6']['tmp_name']);
但奇怪的是,如果我按 SAVE,页面就会变成白色。如果我只调用一次函数,那么只有这个:
$work1 = $_FILES["work1"]["name"];
save_image($_FILES["work1"]["name"], $_FILES['work1']['tmp_name']);
它工作得很好..
不知道我在做什么错..
亲切的问候
编辑:
错误报告告诉我:
Notice: Constant MAX_SIZE already defined in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php on line 11
Fatal error: Cannot redeclare getExtension() (previously declared in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php:12) in /Applications/XAMPP/xamppfiles/htdocs/vcard2/user/functions.php on line 12