PHP dynamically change filename

2019-06-03 16:09发布

问题:

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='' />\n";
    }
  }
  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:

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]."\n"; 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);