欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php文件上传及错误代码总结_PHP教程

程序员文章站 2022-06-05 21:26:54
...
本文章总结了关于php文件上传及错误代码的介绍,有需要学习的朋友可参考一下本文章。

上传操作代码

代码如下 复制代码









if($_FILES['myfile']['error'] > 0) { //判断文件是否可以成功上传到服务器,0表示上传成功
echo 'Error: ';

switch ( $_FILES['myfile']['error'] ) {
case UPLOAD_ERR_OK:
$response = 'There is no error, the file uploaded with success.';
break;
case UPLOAD_ERR_INI_SIZE:
$response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case UPLOAD_ERR_FORM_SIZE:
$response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case UPLOAD_ERR_PARTIAL:
$response = 'The uploaded file was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
$response = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
break;
case UPLOAD_ERR_CANT_WRITE:
$response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
break;
case UPLOAD_ERR_EXTENSION:
$response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
break;
default:
$response = 'Unknown error';
break;
}
echo $response;


exit; //如果$_FILES['myfile']['error']大于0都是有错误,输出错误信息并退出程序
}

//获取上传文件的MIME类型中的主类型和子类型
list($maintype,$subtype)=explode("/",$_FILES['myfile']['type']);

if ($maintype=="text") { //通过主类型限制不能上传文本文件,例如.txt .html .php等文件文件

echo '问题: 不能上传文本文件。';
exit; //如果用户上传文本文件则退出程序

}

$upfile = './uploads/'.time().$_FILES['myfile']['name']; //定义上传后的位置和新文件名

if ( is_uploaded_file($_FILES['myfile']['tmp_name']) ) { //判断是否为上传文件

if ( !move_uploaded_file($_FILES['myfile']['tmp_name'], $upfile) ) { //从移动文件
echo '问题: 不能将文件移动到指定目录。';
exit;
}

}else{

echo '问题: 上传文件不是一个合法文件: ';
echo $_FILES['myfile']['name'];
exit;

}

echo '文件 : '.$upfile.' 上传成功, 大小为 : ' .$_FILES['myfile']['size'].'!
'; //如果文件上传成功则输出
?>

html

代码如下 复制代码

br /> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">








选择文件:



一些常见的上传文件时错误代码

0 | UPLOAD_ERR_OK | 文件成功上传
1 | UPLOAD_ERR_INI_SIZE | Size exceeds upload_max_filesize in php.ini.
2 | UPLOAD_ERR_FORM_SIZE | Size exceeds MAX_FILE_SIZE specified in HTML form.
3 | UPLOAD_ERR_PARTIAL | 文件没有完整上传
4 | UPLOAD_ERR_NO_FILE | 没有上传文件
5 | UPLOAD_ERROR_E | As expliained by @Progman, removed in rev. 81792
6 | UPLOAD_ERR_NO_TMP_DIR | 找不到临时文件夹
7 | UPLOAD_ERR_CANT_WRITE | 磁盘不可写
8 | UPLOAD_ERR_EXTENSION | File upload stopped by extension.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631586.htmlTechArticle本文章总结了关于php文件上传及错误代码的介绍,有需要学习的朋友可参考一下本文章。 上传操作代码 代码如下 复制代码 !DOCTYPE html PUBL...