文件上传
1.图片文件上传,文件夹必须存在才能上传成功,可以再之前添加一个功能“判断文件夹是否存在,不存在时,就创建文件夹,存在则不作任何处理”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <body> <table> <?php function getImageExt($fileName) { $arrFile=explode('.',strtolower($fileName)); $fileExt=$arrFile[count($arrFile)-1]; return $fileExt; } 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"]; $error = $upfile["error"]; switch ($type) { case 'image/jpg' : if(getImageExt($name)=="jpg" || getImageExt($name)=="jpeg") $ok=1; break; case 'image/jpeg' : if(getImageExt($name)=="jpg" || getImageExt($name)=="jpeg") $ok=1; break; case 'image/pjpeg' : if(getImageExt($name)=="jpg" || getImageExt($name)=="jpeg") $ok=1; break; case 'image/gif' : if(getImageExt($name)=="gif") $ok=1; break; case 'image/png' : if(getImageExt($name)=="png") $ok=1; break; case 'image/x-png' : if(getImageExt($name)=="png") $ok=1; break; } } ?> <form action="" enctype="multipart/form-data" method="post" name="upform"> <tr> <td height="110" bgcolor="#FFFFFF" class="STYLE2">LOGO上传:</td> <td height="110" bgcolor="#FFFFFF" class="STYLE2"> <?php if($ok && $error=='0'){ if(move_uploaded_file($tmp_name,'img/'.$name)){echo "<span style=\"color:red\">上传成功</span><br/>";} else{echo "<span style=\"color:red\">上传失败!</span><br/>";} } if(is_uploaded_file($_FILES['upfile']['tmp_name']) && $ok!=1){ echo "<span style=\"color:red\">上传失败,请确认您上传的图片为:jpg jpeg gif png格式!</span><br>"; } ?> <input name="upfile" type="file"> <input type="submit" value="上传"><br /> <span style="color:#F00">标准尺寸:长<240px 宽<100px</span><br /> <span style="color:#36C">文件会上传到站点相对路径的img/目录</span> </td> <td height="80" bgcolor="#FFFFFF" class="STYLE4"> 支持上传的图片类型为:jpg jpeg gif png格式!<br> <span style="color:#F00">注意:如果上传的文件名与原目录的文件名相同,此操作会自动覆盖原来的图片。</span> </td> </tr> </form> </table> </body> </html>