1. <?php
2.
3. $allowedExts = array("gif", "jpeg", "jpg", "png");
4. $temp = explode(".", $_FILES["file"]["name"]);
5. // var_dump($_FILES["file"]);
6.
7. $extension = end($temp); // 获取文件后缀名
8. if (in_array($extension, $allowedExts))
9. {
10. if ($_FILES["file"]["error"] > 0)
11. {
12. echo "错误:: " . $_FILES["file"]["error"] . "<br>";
13. }
14. else
15. {
16. if (file_exists("upload/" . $_FILES["file"]["name"]))
17. {
18. echo $_FILES["file"]["name"] . " 文件已经存在。 ";
19. }
20. else
21. {
22. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
23. $uploadimg="upload/" . $_FILES["file"]["name"];
24.
25. $dbhost = 'localhost'; // mysql服务器主机地址
26. $dbuser = 'root'; // mysql用户名
27. $dbpass = '123456'; // mysql用户名密码
28. $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
29. mysqli_select_db( $conn, 'mysql' );
30. mysqli_query($conn , "set names utf8");
31. //插入数据库
32. $sql = "INSERT INTO imgs ".
33. "(img) ".
34. "VALUES ".
35. "('$uploadimg')";
36. $retval = mysqli_query( $conn, $sql );
37.
38.
39. $select = "select * from imgs where img='$uploadimg'";
40.
41. $retvals = mysqli_query( $conn, $select );
42. while($row = mysqli_fetch_array($retvals,MYSQLI_ASSOC)){
43. echo "<img src='{$row['img']}'>";
44.
45. }
46. if(! $retval )
47. {
48. die('无法插入数据: ' . mysqli_error($conn));
49. }
50. }
51. }
52. }
53. else
54. {
55. echo "非法的文件格式";
56. }
57.
58.
59.
60.
61.
62.
63.
64. ?>