Home > php教程 > php手册 > body text

传说中的...文件上传

WBOY
Release: 2016-06-13 10:53:25
Original
822 people have browsed it

文件上传中的图片上传

   首先要设定图片的格式,大小(MAX_size),属性(type)。

   然后再用逻辑判断。

   最后建一个

表单,进行上传。

源码:

file.php

print_r($_FILES);
define('MAX_SIZE',100000);
$arr_filetype = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif','application/octet-stream');
if ($_FILES["file"]["error"]>0){
 echo "上传文件失败!";
 }
 
else {
  if(!in_array($_FILES["file"]["type"],$arr_filetype)){
   echo "<script>alert(&#39;文件格式不正确!请重新上传!&#39;);history.back();</script>";
   exit;
   }
 
  /*switch($_FILES["file"]["type"]){
   case 'image/jpeg':break;
   case 'image/pjpeg':break;
   case 'image/gif':break;
   case 'image/png':break;
   case 'image/x-png':break;
   default:echo "<script>alert(&#39;文件格式不正确!请重新上传!&#39;);history.back();</script>";   
  
      } */    
  else{
   if($_FILES["file"]["size"]>100000){
   echo "<script>alert(&#39;文件太大!请重新上传!&#39;);history.back();</script>";
   exit;
   } 
  }
  if(move_uploaded_file($_FILES["file"]["tmp_name"],"d:/wamp/tmp/".time().$_FILES["file"]["name"])){
 echo "文件上传成功!";
 }
 }

 
 

?>

 

file.html


上传图片


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template