PHP 根据文件名制作幻灯片/媒体播放器
我很抱歉发布了不可靠的代码! :( 但我在编写 PHP 方面很糟糕(刚刚开始学习),我当然需要更多地研究它。但目前,我真的需要这个脚本工作,这样我就可以展示我的作品。:) 我想知道是否有人会愿意能帮忙纠正一下吗?
基本上我是从 .txt 文件获取图像文件名。 txt 文件的设置如下:
1|imagefilename1.jpg
2|imagefilename2.jpg
下面代码的想法是根据文件更改元素。例如,如果文件是 .mov 文件,它将加载视频播放器(来自 vimeo 的视频流)。如果它是图像文件,它将有幻灯片。
<?php
$photos=file("photos.txt");
$img = array('jpg', 'png', 'gif');
$vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
foreach($photos as $image){
$item=explode("|",$image);
$ext = explode(".", $image);
if(in_array($ext[1], $img))
{'<div id='thejqueryslider'><div class='slider'><img src='images/work/$photo' alt='' /> </div></div>'}
elseif(in_array($ext[1], $vid))
{'<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>'}
?>
I am sorry for posting a dodgy code! :( But I'm terrible at writing PHP (just started learning) and I certainly need to study it more. But for the moment, I really need this script working so I can showcase my artwork. :) I was wondering if someone would be able to help correct it?
Basically I am getting image file names from a .txt file. The txt file is set up like this:
1|imagefilename1.jpg
2|imagefilename2.jpg
The idea of the code below is to change an element depending on the file. For example, if the file is a .mov file, it will load a video player (the videos stream from vimeo). And if it's an image file, it will have a slideshow.
<?php
$photos=file("photos.txt");
$img = array('jpg', 'png', 'gif');
$vid = array('swf', 'mp4', 'mov', 'mpg', 'flv');
foreach($photos as $image){
$item=explode("|",$image);
$ext = explode(".", $image);
if(in_array($ext[1], $img))
{'<div id='thejqueryslider'><div class='slider'><img src='images/work/$photo' alt='' /> </div></div>'}
elseif(in_array($ext[1], $vid))
{'<iframe src='$photo' width='800' height='450' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>'}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个明显的错误是您忘记在要输出内容的位置添加
echo
语句。您还应该在输出的 HTML 周围使用双引号而不是单引号,否则其中的单引号会终止字符串并导致语法错误。在 echo 语句末尾使用分号来完成此操作,您的代码应该可以工作。编辑:在
foreach
之后还打开了一个不匹配的大括号,您可能希望在echo
语句之后关闭它。另一个编辑:您正在使用未定义的变量
$photo
。您指的是$item[1]
吗?One obvious error is you've forgotten to add
echo
statements where you want to output stuff. You should also use double quotes instead of single ones around the HTML you're outputting, otherwise the single quotes inside it terminate the string and cause syntax errors. Finish that with semicolons at the end of the echo statements and your code should work.EDIT: You also have an unmatched curly bracket opened after the
foreach
, you'll probably want to close it after theecho
statements.ANOTHER EDIT: You are using the variable
$photo
which is not defined. Did you mean$item[1]
?