单张图片上传=>
---HTML 代码---
<HTML>
<HEAD>
<TITLE>File upload</TITLE>
</HEAD>
<BODY>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</BODY>
</HTML>
---upload.php代码---
<?php
error_reporting(7);
if(isset($_FILES['file'])){
$file=$_FILES['file'];
// file properties
$file_name=$file['name'];
$file_temp=$file['tmp_name'];
$file_size=$file['size'];
$file_error=$file['error'];
// file extension
$file_ext= explode('.',$file_name);
$file_ext=strtolower(end($file_ext));
$allow= array('txt','jpg');
if(in_array($file_ext,$allow)){
if($file_error===0){
if($file_size<=2097152){
$file_name_new= uniqid('',true) . '.' .$file_ext;
$file_dest ='c:\upload/'.$file_name_new;
if(move_uploaded_file($file_temp,$file_dest)){
echo " file is saved successfully at - ". $file_dest;
}
}
}
}
}
?>
图片文件的基本属性有:name/type/tmp_name/size...
![]()
补充:
php多文件上传的三种方法=》
方法一:当多文件上传表单中多个上传文件的name属性为“pic[]”形式时,也即表单为这种形式。
HTML代码:
1 |
<form action="" method="post" enctype="multipart/form-data"> |
2 |
<input type="hidden" name="MAX_FILE_SIZE" value=""> |
3 |
<input type="file" name="pic[]" /><br><br> |
4 |
<input type="file" name="pic[]" /><br><br> |
5 |
<input type="file" name="pic[]" /><br><br> |
6 |
<input type="file" name="pic[]" /><br><br> |
7 |
<input type="submit" value="upload"/> |
方法二:当多文件上传表单中多个上传文件的name属性为“pic,text,doc,pdf”形式时也即表单为这种形式:
1 |
<form action="" method="post" enctype="multipart/form-data"> |
2 |
<input type="hidden" name="MAX_FILE_SIZE" value=""> |
3 |
<input type="file" name="pic" /><br><br> |
4 |
<input type="file" name="text" /><br><br> |
5 |
<input type="file" name="doc" /><br><br> |
6 |
<input type="file" name="pdf" /><br><br> |
8 |
<input type="submit" value="upload"/> |
方法三:先定义单个文件上传的函数,再循环调用这个函数,每次传入一个文件的上传信息,也可以到达多文件上传的目的,而且这种方式,更容易理解。
1 |
<form action="" method="post" enctype="multipart/form-data"> |
2 |
<input type="hidden" name="MAX_FILE_SIZE" value=""> |
3 |
<input type="file" name="pic1" ><br> |
4 |
<input type="file" name="pic2"><br> |
5 |
<input type="file" name="pic3"><br> |
6 |
<input type="submit" value="upload"> |