发新话题
打印

简单的文件上传的方法(07/13/8)

简单的文件上传的方法(07/13/8)

简单的文件上传的方法,主要就是$_FILES的用法!
等我挣了100万我就找个人娶了!哈哈!

TOP

复制内容到剪贴板
代码:
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>
手册里面的  帖了大家学习一下
交流QQ群2:16142493
智能手机软件下载
PHP新手不可错过一帖
PHP新手如何获得积分
论坛需要你,我们大家需要你!

TOP

贴个直接可以用的
复制内容到剪贴板
代码:
<?php
//如果是需要登录使用,这里加个是否登录的判断
if($_POST["Submit"]){
    $file_name = $_FILES["file"]["name"];
    $file_size = $_FILES["file"]["size"];
    $file_type = $_FILES["file"]["type"];
    $file_tn   = time().$file_name;
    $save_path = "upfiles/";
    $messg     = "<p>上传文件发生以外:</p><a href='?id=".$id."'>返回重试</a>";
    $messg_sr  = $messg;
    if($file_type != "application/msword"){//淡水河边提示,这里限制上传格式为word
        $messg .= "<p>本次上传文件格式为MS WORD,通常扩展名为.doc</p>";
    }
    if($file_size > 1048576){//淡水河边提示,这里可写成"if($file_size > 1*1024*1024){"方便修改
        $messg .= "<p>本次上传文件大小不能超过1MB,本文件大小为".round(($file_size/1024/1024),2)."MB</p>";
    }
    if($messg != $messg_sr){
        echo $messg;
    }else{
        if(move_uploaded_file($_FILES["file"]["tmp_name"],$save_path.$file_tn)){
            //以下是上传成功的提示及跳转
            echo "<meta http-equiv=\"refresh\" content=\"3;URL=/\"><div id=\"container\" style=\"margin:40px; padding:40px;text-align:center; font-size:12px; border:#0099FF double;\">上传成功!</div>";
        }else{
            echo $messg;
        }
    }
}else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传</title>
<style type="text/css">
<!--
.pt11 {
    font-size: 11pt;
    color: #333333;
}
-->
</style>
</head>

<body>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="">
  <table width="500" border="0" align="center" cellspacing="1" bgcolor="#666699" class="pt11">
    <tr>
      <td height="25" bgcolor="#9999FF">上传</td>
    </tr>
    <tr>
      <td height="60" align="center" bgcolor="#F1F1F1">
          <input name="file" type="file" id="file" size="32" />
          </td>
    </tr>
    <tr>
      <td height="25" align="center" bgcolor="#E1E1E1">
        <input type="submit" name="Submit" value="提 交" /></td>
    </tr>
  </table>
</form>

</body>
</html>
<?php }?>
本帖最近评分记录
  • leo 威望 +4 2007-8-14 11:23

TOP

占个做,  正在弄~~~~~~~~~~~~~~~~~~
*********************************
天地自在,任我逍遥
http://www.dophp.net/
dophp@qq.com
**********************************

TOP

发新话题