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>查看更多关于php遍历读取文件夹/目录图片信息 - php文件操作的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did27846