Heim > Artikel > Backend-Entwicklung > PHP文件上传效能 - 单文件上传
PHP文件上传功能 -- 单文件上传
这一节内容主要介绍PHP上传文件的单文件上传功能。
PHP主要通过POST方法来上传文件,文件上传之后存放在服务器的临时目录下(可查看PHP配置文件php.ini中的选项upload_tmp_dir)
接下来,我们来看一例子:
----------------------------------------------------------------------
if(isset($_POST['submit'])){
switch($_FILES['upload_file']['error']){
case UPLOAD_ERR_INI_SIZE:
echo "文件大小超过了服务器的限制";
break;
case UPLOAD_ERR_FORM_SIZE:
echo "文件大小超过了浏览器的限制";
break;
case UPLOAD_ERR_PARTIAL:
echo "只上传了部分文件";
break;
case UPLOAD_ERR_NO_FILE:
echo "没有文件被上传";
break;
case UPLOAD_ERR_NO_TMP_DIR:
echo "找不到临时文件夹";
break;
case UPLOAD_ERR_CANT_WRITE:
echo "文件写入失败";
break;
case UPLOAD_ERR_OK:
$upload_dir = './'.iconv("UTF-8","gb2312",$_FILES['upload_file']['name']);
if(file_exists($upload_dir)){
echo '<script>alert(已经存在同名文件)</script>';
}else{
if(move_uploaded_file($_FILES['upload_file']['tmp_name'],$upload_dir)){
echo '<script>alert(文件上传成功)</script>';
}else{
echo '<script>alert(文件移动失败)</script>';
}
}
break;
}
}
?>
---------------------------------------------------------------------