简体   繁体   中英

Create an automatic RSS feed from a server folder

I have a VPS web server with PHP installed and have several folders with audio recordings from school. I would like to generate an RSS feed for each folder, listing the .mp3 files in alphabetical order (since they contain a number prefix like 001-filename). Is there any way to do this that currently works? I have found these 2 scripts online but none of them seem to work for me.

  1. http://dircaster.org/index.html

  2. https://gist.github.com/vsoch/4898025919365bf23b6f

N°2 seems like the most simple script, but if I edit the variables and upload it to the appropriate folder, it does not seem to work. This is the result I get navigating to my page:

?xml version="1.0"?> 0) { $files[]['name'] = $file; $files[]['timestamp'] = filectime($file); } } closedir($dir); // natcasesort($files); - we will use dates and times to sort the list. for($i=0; $i\\n"; echo " \\n"; echo " ". $feedBaseURL . $files[$i]['name'] . "\\n"; echo " ". $feedBaseURL . $files[$i]['name'] . "\\n"; // echo " ". date("DM j G:i:s TY", $files[$i]['timestamp']) ."\\n"; // echo " " . $files[$i]['timestamp'] ."\\n"; echo " \\n"; } } } ?>

This is my php -version PHP 5.6.17-0+deb8u1 (cli) (built: Jan 13 2016 09:10:12)

My end result should be this:

website.com/chemistry/ have an RSS feed for all the chemistry audio files

website.com/english/ have an RSS feed for all the english audio files

Begin by taking the original github script, save it to a file (with an extension of .php) and upload that to your server to be sure it works as is. I get the following (in Chrome) (I changed the website name to example.com so I could post it here):

<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>My Audio Feed</title>
        <link>http://www.example.com/audio</link>
        <description>Feed for the my audio files in some server folder</description>
        <atom:link href="http://gogglesoptional.com/bloopers" rel="self" type="application/rss+xml"/>
    </channel>
</rss>

If that works, try replacing <?= '?'; ?> <?= '?'; ?> with <?php echo '?'; ?> <?php echo '?'; ?> . The script from github includes php code mixed with XML, and your output suggests that something (syntax problem, missing <?php , etc) switched it from php-mode to xml when it wasn't intended.

Replace every beginning of any occurence of php code from <? to <?php , and then it'll work. The XML parser suggests, that a <? tag marks the beginning of the XML declaration.

Summary:

Here is what I had to do to make GitHub script work.

First you need to have the proper extensions.

Line 14: $allowed_ext = ".mp4,.MP4,.mp3,.MP3";

Ensure you have a . or some character (could even be a * ) in the front of the first extension.

Example : $allowed_ext = ".mp3";

What happens is the strpos() that checks for the file extension is checking to make sure that mp3 is in the list and it returns the position. Without the . in front it will return 0 if it is the first extension and the code had greater than 0, >0 , so it will return no results and you miss all of the files that have that very first extension listed.

The second thing I did was do away with case sensitive file extensions.

Line 30 : $ext = strtoupper($path_info['extension']);

It automatically makes the extension upper case so having ".mp3, .MP3" for the extension list is redundant and pretty well ignores the "mp3" extension since it is lower case.

Here is my revised line 32 . I forced the $allowed_ext variable to be upper case in the strpos to match the scripts force of the $ext variable to be upper case as well.

if($file !== '.' && $file !== '..' && !is_dir($file) && strpos(strtoupper($allowed_ext), $ext)>0)  

The script is set up for Microsoft with all caps. Also if you get a file with no extension it fails so I made the following changes at line 30

    $ext = pathinfo($sub.$file, PATHINFO_EXTENSION);

    if ( empty($ext)) { $ext = ".bad";}

This insures that the extension is never empty and that it sees files on a non MS server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM