标签归档 PHP上传图片

通过清心醉

PHP上传图片文件方法

function uploadfile()
{
?>
<form action=”” method=”post” name=”uploadfile” enctype=”multipart/form-data”>
<center>上传文件:<input type=”file” name=”upfile” /></center><br>
<center><input type=”submit” value=”上传” /></center></form>
<?php
if(is_uploaded_file(@$_FILES[“upfile”][“tmp_name”]))
{
$upfile=$_FILES[“upfile”];    //获取上传文件数组里面的值
$name=$upfile[“name”];//获取上传文件数组里面的文件名
$type=$upfile[“type”];//获取上传文件数组里面的类型
$size=$upfile[“size”];//获取上传文件数组里面的大小
$tmp_name=$upfile[“tmp_name”];//获取上传文件数组里面的临时存放路径
switch ($type)
{//判断type类型
case ‘image/pjpeg’:$fileok=true; //$fileok的值为true(真)
break;
case ‘image/jpeg’:$fileok=true;        break;
case ‘image/gif’:$fileok=true;        break;
case ‘image/png’:$fileok=true;        break;
}
if($size>500000)
{
echo “文件过大”;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;;
exit;
}
if($fileok) //如果返回为真,执行下面代码:
{
$error=$upfile[“error”];//上传后系统返回的值
//把上传的临时文件移动到image目录下面
move_uploaded_file($tmp_name,’img/’.$name);
$destination=”img/”.$name;
echo “上传信息:<br/>”;
switch($error)
{
case 0:
echo “文件上传成功啦!”;
//如果做幻灯因为有多图片的处理,到时修改幻灯片就要根据图片的ID来WHERE指定
mysql_query(“UPDATE bbs SET bbslogo= ‘$name'”); //写入sql
echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;break;
case 1:echo “超过了文件大小,在php.ini文件中设置”;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;break;
case 2:echo “超过了文件的大小MAX_FILE_SIZE选项指定的值”;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;break;
case 3:echo “文件只有部分被上传”;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;break;
case 4:echo “没有文件被上传”;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;break;
default:echo “上传文件大小为0”;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;break;
}
}
else
{echo “请上传jpg,gif,png等格式的图片!”;exit;echo “<script>setTimeout(\”this.location=’index.php’\”,2000);</script>”;}
}

}