文件上传的基本思路是:
1.判断上传是否出错,具体是什么错误。可以通过$_FILES['userfile']['error'] 获得错误信息。
2.判断上传的文件类型是否匹配,可以通过$_FILES['userfile']['type'] 取得信息。
3.如果在php的配置文件里没有修改默认临时存放文件的地址,那么上传后文件将存到系统默认的一个文件夹,这个文件夹系统会隔一段时间清理一次,因此,最后改变存储路径,保存上传的文件。
前台页面:
<html>
<head>
<title>Admin upload</title>
</head>
<body>
<h1>Upload new files</h1>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<p>
Upload this file:<input type="file" name="userfile" maxlength="50">
<input type="submit" value="上传">
</p>
</form>
</body>
</html>
<head>
<title>Admin upload</title>
</head>
<body>
<h1>Upload new files</h1>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<p>
Upload this file:<input type="file" name="userfile" maxlength="50">
<input type="submit" value="上传">
</p>
</form>
</body>
</html>
后台代码:
<html>
<head>
<title>资料上传Upload</title>
</head>
<body>
<h1>Uploading file</h1>
<?php
<head>
<title>资料上传Upload</title>
</head>
<body>
<h1>Uploading file</h1>
<?php
/*
$files['userfile']['error']的结果将输出0,1,2,3,4,5. 其中0表示没有错误!
*/
if ($_FILES['userfile']['error']>0) {
echo 'Problem:';
switch ($_FILES['userfile']['error'])
{
case 1: echo 'File exceeded max in phi.ini!';break;//1表示文件超过php配置里的大小限制
Case 2: echo 'File exceeded max_file_size';break;//2表示超过最大限制
case 3: echo 'File only partially uploaded';break;//3表示部分上传
case 4: echo 'No file upload'; break;//4表示没有上传
}
exit;
}
/*
如果文件类型非纯文本,输出提示 */
if ($_FILES['userfile']['type']!='text/plain') {
echo 'Problem:file is not plain text';
exit;
}
/*
转移文件路径,转移失败,输出错误
*/
$upfile='/uploads';//存储路径
if ( is_uploaded_file($_FILES['userfile']['tmp_name'])) {
if (! move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)) {
echo 'Problem could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem :possible file upload attack file:';
echo $_FILES['userfile']['name'];
exit;
}
echo 'File upload sucessfully<br/>';
//输出上传的文本的内容
$contents= file_get_contents($upfile);
$files['userfile']['error']的结果将输出0,1,2,3,4,5. 其中0表示没有错误!
*/
if ($_FILES['userfile']['error']>0) {
echo 'Problem:';
switch ($_FILES['userfile']['error'])
{
case 1: echo 'File exceeded max in phi.ini!';break;//1表示文件超过php配置里的大小限制
Case 2: echo 'File exceeded max_file_size';break;//2表示超过最大限制
case 3: echo 'File only partially uploaded';break;//3表示部分上传
case 4: echo 'No file upload'; break;//4表示没有上传
}
exit;
}
/*
如果文件类型非纯文本,输出提示 */
if ($_FILES['userfile']['type']!='text/plain') {
echo 'Problem:file is not plain text';
exit;
}
/*
转移文件路径,转移失败,输出错误
*/
$upfile='/uploads';//存储路径
if ( is_uploaded_file($_FILES['userfile']['tmp_name'])) {
if (! move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)) {
echo 'Problem could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem :possible file upload attack file:';
echo $_FILES['userfile']['name'];
exit;
}
echo 'File upload sucessfully<br/>';
//输出上传的文本的内容
$contents= file_get_contents($upfile);
$contents=
strip_tags($contents);//清除html或php标志
file_put_contents($_FILES['userfile']['name'],$contents);
echo '<p>Preview of uploaded file contens:<br/></p>';
echo nl2br($contents);
echo '<br/><hr/>';
?>
</body>
</html>
file_put_contents($_FILES['userfile']['name'],$contents);
echo '<p>Preview of uploaded file contens:<br/></p>';
echo nl2br($contents);
echo '<br/><hr/>';
?>
</body>
</html>
转载于:https://blog.51cto.com/tao1848/197554