<!--
上传文件需要考虑的细节:
1.如何控制用户上传的文件大小<2M
2.如何控制用户上传的文件类型
3.如何防止用户图片覆盖问题
4.如何防止同一个用户上传的文件名相同问题
一般:图片存放在服务器文件夹里
-->
<?php
//print_r($_FILES);
/*
Array
(
[myfile] => Array
(
[name] => css.css
[type] => text/css
[tmp_name] => C:\xampp\tmp\phpC1EA.tmp
[error] => 0
[size] => 11580
)
) */
$file_size = $_FILES["myfile"]["size"];
$file_type = $_FILES['myfile']['type'];
if($file_size >= 2*1024*1024){
echo "upload file big";
exit();
}
if(($file_type!='image/img') && ($file_type!='image/jpeg') && ($file_type!='image/png')){
echo "upload file type must img/pjeg";
exit();
}
if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
$name = $_POST['name']; //get name set folder
$upload = $_FILES['myfile']['tmp_name'];
$file_true_name = $_FILES['myfile']['name'];
//给每个用户动态create一个folder+userName
$user_path = $_SERVER['DOCUMENT_ROOT'].'/wp_php/style/image/upload/'.$name;
if(!file_exists($user_path)){
mkdir($user_path,0777,true);
}else{
@rmdir($user_path,0777,true);
echo "upload folder exist<br>";
}
//命名规则
//substr($file_true_name, strripos($file_true_name, '.') 文件扩展名: .jpg
$move_to_file = $user_path.'/'.time().rand(1, 1000).substr($file_true_name, strripos($file_true_name, '.'));
if(move_uploaded_file($upload, iconv('utf-8', 'gb2312', $move_to_file))){
echo 'upload success';
}else{
echo 'upload wrong';
}
}else{
echo "upload wrong";
}
?>
PHP----上传文件
最新推荐文章于 2024-07-24 06:45:00 发布