Home > php教程 > PHP源码 > body text

php遍历读取文件夹/目录图片信息

WBOY
Release: 2016-06-08 17:22:20
Original
1252 people have browsed it

今天帮助一个客户做一上企业网站,发现企业网站做好之后它准备了几百张图片让我上传,这个对于我来讲非常的不想做了,但后来发现可以直接使用程序读取目录然后保存到mysql中就可以解决了,下面我只找到一个函数关于mysql操作部份呆会我再写。

<script>ec(2);</script>
 代码如下 复制代码

 
    $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     {
       if(substr($filen,strpos($filen,"."))==$fileFormat[$i])
       {
        //echo '
php遍历读取文件夹/目录图片信息
';  
        $img_arr[] = $dir.$filen;   //存入数组
        break ;
       } www.111cn.net
    }
    }
    closedir($dir_res);
    //print_r( json_encode($img_arr) );//转json格式
    $s=json_encode($img_arr);
    echo $s;
?>

script代码

 代码如下 复制代码

<script><br /> $(function(){<br /> $.ajax({<br /> url: 'img.php',<br /> type: 'POST',<br /> dataType: 'JSON',<br /> data: {param1: 'value1'},<br /> })<br /> .done(function(data) {<br /> //console.log("success");<br /> for(attr in data) {<br /> <br /> $("body").append("<img src="+ data[attr] +" / alt="php遍历读取文件夹/目录图片信息" >");<br /> }<br /> })<br /> .fail(function() {<br /> console.log("error");<br /> })<br /> .always(function() {<br /> console.log("complete");<br /> });<br /> <br /> }) <br /> </script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!