PHP 根据文件名制作幻灯片/媒体播放器

发布于 2024-12-12 05:39:19 字数 863 浏览 3 评论 0原文

我很抱歉发布了不可靠的代码! :( 但我在编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-12-19 05:39:19

一个明显的错误是您忘记在要输出内容的位置添加 echo 语句。您还应该在输出的 HTML 周围使用双引号而不是单引号,否则其中的单引号会终止字符串并导致语法错误。在 echo 语句末尾使用分号来完成此操作,您的代码应该可以工作。

if(in_array($ext[1], $img))
{ echo "<div id='thejqueryslider'><div class='slider'><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>"; }

编辑: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.

if(in_array($ext[1], $img))
{ echo "<div id='thejqueryslider'><div class='slider'><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>"; }

EDIT: You also have an unmatched curly bracket opened after the foreach, you'll probably want to close it after the echo statements.

ANOTHER EDIT: You are using the variable $photo which is not defined. Did you mean $item[1]?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文