php遍历读取文件夹/目录图片信息
今天帮助一个客户做一上企业网站,发现企业网站做好之后它准备了几百张图片让我上传,这个对于我来讲非常的不想做了,但后来发现可以直接使用程序读取目录然后保存到mysql中就可以解决了.
PHP实例代码如下:
<?php $dir="images/";//定义路径 $dir_res=opendir($dir);//打开目录 $fileFormat=array(0=>".jpg",1=>".gif",2=>".png",3=>".bmp");//图片格式 while(false !== ( $filen=readdir($dir_res) ) ) { for($i=0;$i<count($fileFormat);$i++) { if(substr($filen,strpos($filen,"."))==$fileFormat[$i]) { //echo '<div class="inner"><img src="'.$dir.$filen.'" width="120" height="90" border="0" align="absmiddle" onmouseover="setImgBorder(this)" onmouseout="clearBorder(this)" style="margin:15px;" onclick="goToBigPage(this)" /></div>'; $img_arr[] = $dir.$filen; //存入数组 break ; } } } closedir($dir_res); //print_r( json_encode($img_arr) );//转json格式 $s=json_encode($img_arr); echo $s; ?>
script代码如下:
<script> $(function(){ $.ajax({ url: 'img.php', type: 'POST', dataType: 'JSON', data: {param1: 'value1'}, }) .done(function(data) { //console.log("success"); for(attr in data) { $("body").append("<img src="+ data[attr] +" />"); } }) .fail(function() { console.log("error"); }) .always(function() { console.log("complete"); }); }) </script>
本文链接:http://www.phprm.com/wenjian/fs3479.html
收藏随意^^请保留教程地址.