25行实现文件上传功能(PHP)
1 <form action="#" method="post" enctype="multipart/form-data">
2 <label for="file">file:</label>
3 <input type="file" name="file" id="file"/><br/>
4 <input type="submit" name="submit" value="submit">
5 </form>
6 <?php
7 if (!isset($_REQUEST["file"])) {
8 return;
9 }
10 $file = $_FILES["file"];
11 if ($file["error"] > 0 ) {
12 echo "Error:".$_FILES["file"]["error"]."<br/>";
13 } else {
14 $dir = "upload";
15 if (!file_exists($dir)) {
16 mkdir($dir);
17 }
18 if (file_exists("$dir/" . $file["name"])) {
19 echo "<h1>".$file["name"] . " already exists .</h1>";
20 } else {
21 move_uploaded_file($file["tmp_name"], "$dir/".$file["name"]);
22 echo "<h1>upload success !</h1> <br/> stored in " . "$dir/" . $file["name"];
23 }
24 }
25 ?>
2 <label for="file">file:</label>
3 <input type="file" name="file" id="file"/><br/>
4 <input type="submit" name="submit" value="submit">
5 </form>
6 <?php
7 if (!isset($_REQUEST["file"])) {
8 return;
9 }
10 $file = $_FILES["file"];
11 if ($file["error"] > 0 ) {
12 echo "Error:".$_FILES["file"]["error"]."<br/>";
13 } else {
14 $dir = "upload";
15 if (!file_exists($dir)) {
16 mkdir($dir);
17 }
18 if (file_exists("$dir/" . $file["name"])) {
19 echo "<h1>".$file["name"] . " already exists .</h1>";
20 } else {
21 move_uploaded_file($file["tmp_name"], "$dir/".$file["name"]);
22 echo "<h1>upload success !</h1> <br/> stored in " . "$dir/" . $file["name"];
23 }
24 }
25 ?>