php实现文件上传

本文介绍了HTML页面中实现文件上传的基本代码,并详细解释了如何在服务器端进行文件类型和大小的验证,确保上传文件符合预期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

简单的文件上传代码

index.html

<html>
<head>
<title>文件上传</title>
</head>
<body>
 <table>
   <form action="file_upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file">
    <input type="submit" name="submit" value="提交">
   </form>
 </table>
</body>
</html>

  

 

file_upload.php

<?php

if ($_FILES['file']['size'] < 10000000){

  /*此处可对上传文件类型做约束

  if (!stristr($_FILES['file']['type'], 'image')){

    echo "文件类型上传错误,请重新上传...";

    echo '<meta http-equiv="refresh" content="3;url=index.html">';

  }

  */

 

  //文件上传服务器端存储目录

  $upload_file = './upload/';

  if (!is_dir($upload_file)){

    mkdir($upload_file);

  }

  

  $path = $upload_file . $_FILES['file']['name'];

  if (move_uploaded_file($_FILES['file']['tmp_name'], $path)){

    echo "文件上传成功,请稍等...";

    echo '<meta http-equiv="refresh" content="3;url=index.html">';

  }else{

    echo "文件上传失败,请重传...";

    echo '<meta http-equiv="refresh" content="3;url=index.html">';

  }

}else{

  echo "文件大小超过上限,请重传...";

  echo '<meta http-equiv="refresh" content="3;url=index.html">';

}

?>
//最后修改日期:2011-10-27 2:40

  

转载于:https://www.cnblogs.com/xshark/archive/2011/09/24/2188337.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值