php实例:文件上传脚本

发布时间:2019-09-25编辑:脚本学堂
本文介绍下,php实现文件上传的一段脚本,有需要的朋友,可以作个参考。

php文件上传代码分享,如下所示:

<html>
 <head>
 <title>文件上传脚本-www.jb200.com</title>
 </head>
 <body>
 <div>
 <?php
 if ( isset( $_FILES['fupload'] ) ) {
     print "文件名称: ".     $_FILES['fupload']['name']       ."<br />";
     print "文件大小: ".     $_FILES['fupload']['size'] ." bytes<br />";
     print "临时文件名称: ".$_FILES['fupload']['tmp_name']   ."<br />";
     print "文件类型: ".     $_FILES['fupload']['type']       ."<br />";
     print "错误消息: ".    $_FILES['fupload']['error']      ."<br />";

     if ( $_FILES['fupload']['type'] == "image/gif" ) {

         $source = $_FILES['fupload']['tmp_name'];
         $target = "upload/".$_FILES['fupload']['name'];
         move_uploaded_file( $source, $target );// or die ("Couldn't copy");
         $size = getImageSize( $target );

         $imgstr = "<p><img width="$size[0]" height="$size[1]" ";
         $imgstr .= "src="$target" alt="uploaded image" /></p>";

         print $imgstr;
     }
 }
 ?>
 </div>
 <form enctype="multipart/form-data"
     action="<?php print $_SERVER['PHP_SELF']?>" method="post">
 <p>
 <input type="hidden" name="MAX_FILE_SIZE" value="102400" />
 <input type="file" name="fupload" /><br/>
 <input type="submit" value="上传文件" />
 </p>
 </form>
 </body>
 </html>
附:简单的文件上传表单
<html>
<head>
<title>简单的文件上传表单</title>
</head>
<body>
<form enctype="multipart/form-data"
   action="<?print $_SERVER['PHP_SELF']?>" method="post">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="102400" />
<input type="file" name="fupload" /><br/>
<input type="submit" value="上传文件" />
</p>
</form>
</body>
</html>

以上这个是最基础的文件上传表单,其中设置了最大上传文件的大小,通过$_SERVER['PHP_SELF']获得当前php文件名。