snipt

Ctrl+h for KB shortcuts

PHP

Pick random file from directory with given extensions

<?php 
//http://stackoverflow.com/questions/4478783/select-random-file-from-directory
function random_video($dirName = "files/videos/")
{
    $files = array_merge(glob($dirName . "*.m4v"), glob($dirName . "*.mp4"), glob($dirName . "*.mov"));
    $file = array_rand($files);
    return $files[$file];
}
?>
<a href="<?php echo random_video(); ?>">Video</a>
https://snipt.net/embed/fe708cb5626c9f84511eb74357197d4f/
/raw/fe708cb5626c9f84511eb74357197d4f/
fe708cb5626c9f84511eb74357197d4f
php
PHP
10
2019-08-21T12:32:39
True
False
False
/api/public/snipt/48908/
pick-random-file-from-directory-with-given-extensions
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">//http://stackoverflow.com/questions/4478783/select-random-file-from-directory</span> </span><span id="L-3"><a name="L-3"></a><span class="k">function</span> <span class="nf">random_video</span><span class="p">(</span><span class="nv">$dirName</span> <span class="o">=</span> <span class="s2">&quot;files/videos/&quot;</span><span class="p">)</span> </span><span id="L-4"><a name="L-4"></a><span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nv">$files</span> <span class="o">=</span> <span class="nb">array_merge</span><span class="p">(</span><span class="nb">glob</span><span class="p">(</span><span class="nv">$dirName</span> <span class="o">.</span> <span class="s2">&quot;*.m4v&quot;</span><span class="p">),</span> <span class="nb">glob</span><span class="p">(</span><span class="nv">$dirName</span> <span class="o">.</span> <span class="s2">&quot;*.mp4&quot;</span><span class="p">),</span> <span class="nb">glob</span><span class="p">(</span><span class="nv">$dirName</span> <span class="o">.</span> <span class="s2">&quot;*.mov&quot;</span><span class="p">));</span> </span><span id="L-6"><a name="L-6"></a> <span class="nv">$file</span> <span class="o">=</span> <span class="nb">array_rand</span><span class="p">(</span><span class="nv">$files</span><span class="p">);</span> </span><span id="L-7"><a name="L-7"></a> <span class="k">return</span> <span class="nv">$files</span><span class="p">[</span><span class="nv">$file</span><span class="p">];</span> </span><span id="L-8"><a name="L-8"></a><span class="p">}</span> </span><span id="L-9"><a name="L-9"></a><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-10"><a name="L-10"></a><span class="x">&lt;a href=&quot;</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nx">random_video</span><span class="p">();</span> <span class="cp">?&gt;</span><span class="x">&quot;&gt;Video&lt;/a&gt;</span> </span></pre></div> </td></tr></table>
extension, file, php, video