dsbtwy1329 2011-10-28 17:31
浏览 54
已采纳

PHP动态更改文件名

I was able to get some help earlier today, but I didn't have everything from the original script for it to work. Basically I have a list of image file names in a .txt file. They each load in a slideshow, and change with the pagination on the page.

What I would like to do, is if I have a file that has a .mov extension, for example, the php script will load a movie player instead.

Here is the original slideshow script

<div id='jessslide'>
  <?php
  echo"
<div id='slider-wrapper'>
<div id='slider' class='nivoSlider'>";
  $photos = file("work.txt");
  foreach ($photos as $image) {
    $item = explode("|", $image);
    if ($item[0] == $fields[0]) {
      $photo = trim($item[1]);
      echo"<img src='images/work/$photo' alt='' />
";
    }
  }
  echo"
</div></div>"
  ?>
</div>

And here is my bad attempt at trying to make this work...

<div id='jessslide'>
  <?php
  $photos = file("work.txt");
  $img = array('jpg', 'png', 'gif');
  $vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
  foreach ($photos as $image) {
  $item = explode("|", $image);
  if ($item[0] == $fields[0]) {
  $photo = trim($item[1]);
  $ext = explode(".", $image);

  if (in_array($ext[1], $img)) 
  {
  echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'><img src='images/work/$photo' alt='' /> </div></div>";  
   } 
   elseif (in_array($ext[1], $vid)) 
   {
   echo "<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>";
   }
   }
   }
   ?>
   </div>

I would really appreciate if someone could help me out to finally bring this script to life! :)

  • 写回答

1条回答 默认 最新

  • donglu5612 2011-10-28 19:25
    关注

    The most likely problem I see is the possibility that the assignment to $ext should be tied to $item or $photo instead of $image. If that solves it, then great. Otherwise, read below for a more complete analysis and some suggestions for steps to debug until you narrow in on the cause of the problem.


    Assuming all of the data is accurate, the script you've written looks like it should work. I've reformatted it here to conduct some analysis:

    <div id='jessslide'>
    <?php
    $photos=file("work.txt");
    $img = array('jpg', 'png', 'gif');
    $vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
    foreach($photos as $image){
      $item=explode("|",$image);
      if($item[0]==$fields[0]){
        $photo=trim($item[1]);
        $ext = explode(".", $image);
    
        if(in_array($ext[1], $img))
        { echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'><img src='images/work/$photo' alt='' /> </div></div>"; }
        elseif(in_array($ext[1], $vid))
        { echo "<iframe src='$photo' width='800' height='450' frameborder='0'     webkitAllowFullScreen allowFullScreen></iframe>"; }
      }
    }
    ?>
    </div>
    

    There are three major places in this script for problems to occur that would cause nothing to be output.

    The first is in the foreach() loop. If the $photos array has nothing in it, you would skip the entire block of code. You can test for this condition by adding a print_r($photos); before the foreach() and then as the first line in the body of the foreach() add echo $image." "; to verify that all the files are listed as you expect. If that looks correct, remove that debugging code and move on.

    The 2nd potential for problems is if the $item[0] is not equal to $fields[0]. To test this, add echo 0; as the first line inside if($item[0]==$fields[0]). If you see zeros as expected when the script is run, then you can remove this debugging code and move on.

    The 3rd potential for problems is pull/examination of the extension. One likely candidate for problems here is if the assignment to $ext should be tied to $item or $photo instead of $image but there are definitely other possible issues. To test this, add echo $image." ".$ext[1]." "; print_r($img); print_r($vid); before the if(in_array($ext[1], $img)). Then add a temporary else clause with the body of echo 3; as well. Verify that th

    Once you figure out which condition in the code is causing problems, you will be well on the way toward solving it. My guess is that you'll get through the tests and either find an obvious mistake in one of the early sections that we are assuming works right, or you will end up with 3 being printed out a lot. In the later case, one possible issue could come from lower/upper-case differences, which could be solved via changing the $item assignment to $item=strtolower(explode("|", $image);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数
  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题
  • ¥15 matlab基于鲸鱼算法优化vmd
  • ¥20 PCB设计与制作问答
  • ¥20 课堂跑如何下载ppt
  • ¥15 STM32阿里云平台显示不了传感器采集的数据