本文共 2335 字,大约阅读时间需要 7 分钟。
<?php//文件夹$books = "books";//创建目录文件夹if(is_dir($books) != true){ mkdir($books);}if(isset($_POST["action"]) && ($_POST["action"] == "upload")){ //文件有临时名称,说明上传 if(isset($_FILES["file"]["tmp_name"])) { //新文件名加随机数 $file_name = "img".rand(1,3).rand(1,9).rand(0,9)."-".$_FILES["file"]["name"]; //移动至目录 if(move_uploaded_file($_FILES["file"]["tmp_name"],$books."/".$file_name)) { echo "cheng gong.<br>"; } else { echo "shi bai.<br>"; } }}?><html><head> <meta http-equiv="content-type" content="text/html" charset="GB2312"> <title>xiang ce</title> <!--样式--> <style> body { margin: 0; padding: 0; background-color: #efefef; font-size: 12px; } ul { margin: 0; padding: 0; list-style: none; } a { color: #333333; text-decoration: none; } a:hover { color: #999999; } .books_out { width: 98%; margin-left: 10px; margin-top: 10px; } .books_out img { margin: 4px; border: 1px solid #cccccc; } .books_out li { float: left; width: 180px; text-align: center; margin: 5px; } </style></head><body><form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <label for="file">upload image</label> <input type="file" name="file" id="file"> <label> <input type="submit" name="submit" value="ti jiao"> <input type="hidden" name="action" value="upload"> </label></form><hr><div class="books_out"> <ul> <?php //目录类 $dh = dir($books); echo "xiang ce mu lu:".$dh->path."<br>"; //循环读取 while(($file = $dh->read()) !== false) { //排除上级目录 if($file != "." && $file !="..") { //文件路径 $path = $books."/".$file; echo "<li><a href='$path' target='_blank'><img src='$path' width='160' height='120' border='0'><br>$path</a></li>"; } } //关闭句柄 $dh->close(); ?> </ul></div></body></html>
转载地址:http://jsem.baihongyu.com/