php源码之将文件上传到服务器

时间:2016-08-23
php 文件上传到服务器主要使用到is_uploaded_file和move_uploaded_file这两个函数,本文章先给大家分享php上传文件到服务器的源码,需要的朋友可以参考一下。

html form表单代码:

<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
   Last Name:<br /> <input type="text" name="name" value="" /><br />
   Homework:<br /> <input type="file" name="homework" value="" /><br />
   <p><input type="submit" name="submit" value="Submit Notes" /></p>
</form>

 

php处理表单数据并上传文件到服务器源码:

<?php
   define ("FILEREPOSITORY","./");

   if (isset($_FILES['homework'])) {

      if (is_uploaded_file($_FILES['homework']['tmp_name'])) {

         if ($_FILES['homework']['type'] != "application/pdf") {
            echo "<p>Homework must be uploaded in PDF format.</p>";
         } else {
            $today = date("m-d-Y");
            if (! is_dir(FILEREPOSITORY.$today)) {
               mkdir(FILEREPOSITORY.$today);
            }
            /* http://www.manongjc.com/article/1389.html */
            $name = $_POST['name'];
            $result = move_uploaded_file($_FILES['homework']['tmp_name'], FILEREPOSITORY.$today."/"."$name.pdf");

            if ($result == 1) 
               echo "<p>File successfully uploaded.</p>";
            else 
               echo "<p>There was a problem uploading the homework.</p>";
         }
      }
   }
?>

上一页 下一页

原文地址:http://www.manongjc.com/article/1389.html