2013-02-13 30 views
0

我正在向网站添加图像,但是我想添加的金额需要很长时间,是否有更快速的方式来添加这些图像并使其自动找到新的图像,如果附加文件名+1如image1.jpg,image2.jpg等如何使用jquery/php批量添加具有相似名称的图像

我正在使用PHP。也想到也许这可以使用JavaScript或jQuery循环,for循环也许,即时只是不确定如何。

   <img src="images/other/pic2.png"></a> 

       <img src="images/other/pic3.png"></a> 

       <img src="images/other/pic4.png"></a> 

       <img src="images/other/pic5.png"></a> 

       <img src="images/other/pic6.png"></a> 

       <img src="images/other/pic7.png"></a> 

       <img src="images/other/pic8.png"></a> 
+0

什么意思是自动查找图像。一个简单的循环会让你添加数字到文件名,但是这并不能保证图像存在,如果这是你想要的。 – adeneo 2013-02-13 00:11:00

+0

我会很乐意修改相应的代码,从循环中的10个图像到20个,但它会帮我省下手动添加图像(如image100)的附加图像 – 2013-02-13 00:14:07

+0

下面的PHP答案应该是您正在寻找的内容。 – adeneo 2013-02-13 00:15:22

回答

-1

深受你的描述来看,做这样的事情很可能是合适的:

$initialImageNumber = 2; 
$endingImageNumber = 9; 

for ($i = $initialImageNumber; $i <= $endingImageNumber; $i++) 
    echo '<img src="images/other/pic' . $i . '.png">'; 
+0

非常感谢!尝试+1,但我有低代表抱歉 – 2013-02-13 00:19:20

0

它不是一个好主意,一味循环迭代的希望文件窗帘量在那里并且是一个有效的文件,更好的方法是循环文件目录,检查文件是否是第一个有效的图像并包含期望的文件名前缀。通过这种方式,您可以将图像添加到目录中,知道添加时不会更改代码。

<?php 

$img_path = './images/other/'; 
$prefix = 'img'; 

if ($fh = opendir($img_path)) { 
    while (false !== ($file = readdir($fh))) { 
     if ($file != "." && $file != "..") { 
      //Validate its an image and get size, also check that the image filename starts with the $prefix 
      $attr = getimagesize($img_path.$file); 
      if(isset($attr[3]) && substr($file,0,strlen($prefix)) == $prefix){ 
       echo '<img src="'.$img_path.$file.'" '.$attr[3].' alt="'.$file.'"/>'; 
      } 
     } 
    } 
    closedir($fh); 
} 
?> 
相关问题