文件上传代码练习:
单文件上传
<?php
// printf("<pre>%s</pre>",print_r($_FILES,true));
class upException extends Exception
{
public function __toString()
{
return '错误信息:'.$this->message.';错误代码:'.$this->code;
}
}
try{
// echo $type;
// echo $name,$type,$tmp_name,$error,$size;
switch($error)
{
case UPLOAD_ERR_OK:
// var_dump(trim(strstr($type,'/'),'/'));
// var_dump(trim(strstr($type,'/',true),'/')!=='jpeg');
$ty=trim(strstr($type,'/'),'/');
if($ty==='jpeg'){
if(file_exists($tmp_name)&&is_uploaded_file($tmp_name))
{
$obj_type=$ty;
$obj_file='images/'.time().'.'.$obj_type;
move_uploaded_file($tmp_name,$obj_file);
}
exit("<script>alert('上传成功');location.href='demo1.php'</script>");
}else{
exit("<script>alert('格式不正确,请重新上传!');location.href='demo1.php'</script>");
}
break;
case UPLOAD_ERR_INI_SIZE:
throw new upException('文夹过大,超过php限制',1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new upException('文件过大,超过表单限制',2);
break;
case UPLOAD_ERR_PARTIAL:
throw new upException('上传文件不完整',4);
break;
case UPLOAD_ERR_NO_FILE:
throw new upException('没有文夹上传',5);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new upException('无临时目录',6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new upException('权限不够,文件写入失败',7);
break;
default:
echo "未知错误";
}}catch(upException $e){
echo $e;
}
多文件上传
<?php
// printf("<pre>%s</pre>",print_r($_FILES,true));
class upException extends Exception
{
public function __toString()
{
return '错误信息:'.$this->message.';错误代码:'.$this->code;
}
}
foreach($_FILES as $file){
extract($file);
// echo $name;
try{
// echo $type;
// echo $name,$type,$tmp_name,$error,$size;
switch($error)
{
case UPLOAD_ERR_OK:
// var_dump(trim(strstr($type,'/'),'/'));
// var_dump(trim(strstr($type,'/',true),'/')!=='jpeg');
$ty=trim(strstr($type,'/'),'/');
if($ty==='jpeg'){
if(file_exists($tmp_name)&&is_uploaded_file($tmp_name))
{
$obj_type=$ty;
$obj_file='images/'.trim($name,'.jpg').time().'.'.$obj_type;
// echo $obj_file,'<br>';
move_uploaded_file($tmp_name,$obj_file);
}
}else{
echo ("<script>alert('格式不正确,请重新上传!');location.href='demo2.php'</script>");
}
break;
case UPLOAD_ERR_INI_SIZE:
throw new upException("{$name}文夹过大,超过php限制",1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new upException("{$name}文件过大,超过表单限制",2);
break;
case UPLOAD_ERR_PARTIAL:
throw new upException("{$name}上传文件不完整",4);
break;
case UPLOAD_ERR_NO_FILE:
throw new upException("{$name}没有文夹上传",5);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new upException("{$name}无临时目录",6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new upException("{$name}权限不够,文件写入失败",7);
break;
default:
echo "未知错误";
}
// exit("<script>alert('上传成功');location.href='demo2.php'</script>");
}catch(upException $e){
echo ("<script>alert('{$e}');</script>");
}}
exit("<script>alert('上传完毕!');location.href='demo2.php'</script>");
批量上传:
<?php
// printf("<pre>%s</pre>",print_r($_FILES,true));
// die();
class upException extends Exception
{
public function __toString()
{
return '错误信息:'.$this->message.';错误代码:'.$this->code;
}
}
$files=$_FILES['my_file'];
foreach($files['error'] as $key=>$value){
try{
switch($value)
{
case UPLOAD_ERR_OK:
// var_dump(trim(strstr($type,'/'),'/'));
// var_dump(trim(strstr($type,'/',true),'/')!=='jpeg');
$ty=trim(strstr($files['type'][$key],'/'),'/');
if($ty==='jpeg'){
if(file_exists($files['tmp_name'][$key])&&is_uploaded_file($files['tmp_name'][$key]))
{
$obj_type=$ty;
$obj_file='images/'.trim($files['name'][$key],'.jpg').time().'.'.$obj_type;
// echo $obj_file,'<br>';
move_uploaded_file($files['tmp_name'][$key],$obj_file);
}
}else{
echo ("<script>alert('{$files['name'][$key]}格式不正确,请重新上传!');</script>");
}
break;
case UPLOAD_ERR_INI_SIZE:
throw new upException("{$files['name'][$key]}文夹过大,超过php限制",1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new upException("{$files['name'][$key]}文件过大,超过表单限制",2);
break;
case UPLOAD_ERR_PARTIAL:
throw new upException("{$files['name'][$key]}上传文件不完整",4);
break;
case UPLOAD_ERR_NO_FILE:
throw new upException("{$files['name'][$key]}没有文夹上传",5);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new upException("{$files['name'][$key]}无临时目录",6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new upException("{$files['name'][$key]}权限不够,文件写入失败",7);
break;
default:
echo "未知错误";
}}catch (upException $e){
echo ("<script>alert('{$e}');</script>");
}}
exit("<script>alert('上传完毕!');location.href='demo3.php'</script>");
演示结果
总结:
1、上传文件信息关键字:$_FILES
(name、type、tmp_name、error、size);
2、把上传文夹从临时目录移动过到目标目录:move_uploaded_file('tmp_name','目标文夹目录')
;
3、is_uploaded_file();
判断上传文件的合法性
4、文件前端以表单的形式上传:
<form action="act3.php" method="POST" enctype="multipart/form-data">
//文件上传,上传方式:post,上传编码:multipart/form-data
<fieldset>
<legend>文件上传</legend>
//隐藏域,用来限制上传文件大小
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
//上传文件:multiple:多选
<input type="file" name="my_file[]" multiple>
<button>上传</button>
</fieldset>
</form>
5、在上传文件时:需查看设置php.ini中:file_uploads
、upload_tmp_dir
、max_file_uploads
、post_max_size
、upload_max_filesize
等相关设置
6、php常用函数:strstr($str,'分割字符',true|flase)
:分割字符trim($str,'去掉的字符')
:去掉字符两端的不需要的字符
file_exists($file);
检测文件试问存在
7、script函数:alert('提示内容')
:单传提示函数;location.href=''
;自动跳转至……;