PHP search video files in directory and display single video - php

I am Justin and new to Stackoverflow. We have a yearly familyweekend and every year someone else creates the entertainment. This year it is my turn. I am creating a game (treasure hunt) with the use of an iPad.
For this I am looking for a script that can search for a filename in a directory and when found, display the file (video) in the page. I am a little familiar with PHP, but not very.
It does not require any database it can be very simple because it will only contain a few videos. But I can't find any scripts that quite do what I need. Every script is either to complicated and advanced or does not show the video but the filestring.
Can anyone help me by pointing me in the right direction? Thanks so much in advance.
Regards,
Justin

to check if a specific file exists in a directory you can use file_exists() method.
Please check the documentation: http://php.net/manual/en/function.file-exists.php

So after trying and trying, I think I found the answer. Below the script for anyone looking for this.
<?php
$dir = 'directoryname';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['formname']))? strtolower($_GET['formname']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
echo "<video width='320' height='240' controls>";
echo "<source src='$dir/$file'>$file type='video/mp4'>";
echo "</video>";
echo "";
echo "<br>";
}
}
closedir($res);
?>
And the form
<form action="search.php" method="get"><input name="formname"
type="text"> <input type="submit"></form>

Related

echo content data from url source

Hey guys I'm not too knowledgeable about this particular topic in PHP yet. Basically I wanted to get a certain content of the url source so for instance the code below will only echo that specific content from the source page. I wanted to do this for other websites and the script below has errors but that's just like a demo of what I want to accomplished.
<?php
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
$data = getBetween($data,'<p class="c"> <font size="+2">',"</font></p>");
echo $data;
?>
All the information of the script above is located here
Use Simple HTML DOM to do this.
Read the manual to do this from here.
Its pretty simple.
//include simple_html_dom.php file.
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.jokesclean.com/OneLiner/Random/');
foreach($html->find('p[class=c]') as $e)
echo $e;
Just tested on my local system and it worked perfectly generating a random joke everytime i refresh
here's what i got on last refresh of this code.
.
It'd be best to use domdocument but you can also do it using regular expressions like the following.
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
if ( preg_match('/\<font size="\+2"\>(.*?)\<\/font\>/', $data, $match) ) {
echo $match['1'];
}
else {
echo 'couldn\'t find a match';
}

How to while loop images from directory

Surprisingly, Google had nothing.
My old method of uploading images was storing the actual image in the database. I'm trying to change my method to storing the photos in directories. But I'm having troubles figuring out how to display them to the users. I'm trying to while loop the files. Much help is greatly appreciated. Also, PDO is not my strong suit, trying to learn it the best I can. Here is what I have.
$query6 = "SELECT * FROM photos WHERE userID=$memberID";
$result6 = $db->query($query6);
while ($row6 = $result6->fetch(PDO::FETCH_ASSOC)) {
$photo = $row6['photo'];
while ($photo == true) {
echo "<img src=\"images/$memberID/$photo\">";
}
}
The page isn't loading at all. Not even an error. Your help is greatly appreciated. Thank You!
The code does insert the photo name in database and properly upload the photo to the directories. I only need assistance with displaying to the user. :)
while ($photo == true)
{?>
<img src="images/<? echo $memberID;?>/<?echo $photo;?>">
close your img tag.like
echo "<img src=\"images/$memberID/$photo\" />";

How to use wildcard in php

I'm trying to rename a file but I'm having a hard time changing it.
Basically I have a file named scorpion.jpg. What I want is change scorpion.jpg to default.jpg
scorpion.jpg is a uploaded file so it can be any name. This is what I have so far.
rename('path_to_image/' . '*.jpg', 'path_to_image/' . 'default.jpg');
Any help is greatly appreciated.
You should try exec('mv "'.$path.'*.jpg" "'.$path.'".default.jpg');
exec function manual
Hope it helped !
EDIT 1 :
So, to stay full PHP i would do this
$scan = glob($path.'*.jpg');
if(!empty($scan)){
if( !rename($path.$scan[0], $path.$newName)){
echo 'Failed rename';
}
else{
echo 'Renamed!';
}
}

Use search output string to open a local network file

I have been trying for ages with all types of "file, file_get_contents, fopen, opendir and etc" to acomplish what I am triying to do, but just no can do for me, this goes beyong my understanding, sadly. But here I am to learn.
What I want to do? I work with LucidWorks, and I have built an Intranet search that searches the specific path given "C://example/example/..." and does a full text search through all the files. The output of the search on my intranet website is simple:
Document title
Body title with highlighted keyowrds
Path to the file
Now, that not being enough, my lazy fellow Companions would like to be able to click the Document title(which does indeed have a full path to the document behind it, just so you can picture it better "C:/Ex/ex/ex/docs/sap/text.txt(or any other termination)) and open it locally.
Here is the part of the code that I believe to be relevant for what I am trying to acomplish. The "solution" i have built in does not work, but it may give you an idea of what I am trying to accomplish here.
$searchParams = array(
'hl' => 'true',
'hl.fl' => 'body'
);
$response = $LWS->search($query, $offset, $limit, $searchParams);
if ($response->response->numFound > 0) {
foreach ($response->response->docs as $doc) {
?>
<div id="resultbox">
<span id="resulttitle"> <?php echo "<a href={$doc->id}'>{$doc->title}</a><br />"; ?> </span>
<?php
$content = (("{$doc->id}'>{$doc->title}"));
print_r( '<a href= ' . fopen(str_replace('%', ' ', $content), "r+") . '>Open File</a><br />');
?>
<SPAN ID="copytext" >
<?php echo substr($content, -100); ?>
<br></SPAN>
<div id="sbody">
<?php
echo "..." . $response->highlighting->{$doc->id}->body[0] . "...<br /><br />";
}
echo '<br />';
return;
} else {
echo "No results for \"" . $query . "\"";
return;
}
?>
There is a little bit more code above it, but it's irrelevant for the asked question.
So there you go folks, I am hoping for help, and to be able to learn something new :)
It looks as though you're trying to put the contents of the files into the href attribute of you anchor/link (<a>) tag.
What you need to be doing, instead, is using a url which links to the specified file. This could possibly look like this in your implementation;
print_r('Open File<br />');
and the output would look something like;
Open File<br />

php to display file information and download link if files are present - How to adjust for multiple files

I have a piece of php that if a specified folder has a file in, it will display the filename, date created and present a download button, if the folder is empty it will show nothing. This works very well but if I have more than one file in the folder it bunches all the filenames together - what I want is the separate information displayed for every file.
To help you understand the problem here is an image showing the problem and the code. I got very far on my own but its way above my head, I just cant see a simple way to correct the problem. The code may look very awkward and odd as I'm totally new at this but it looks visually right on the browser. I would really appreciate any help thank you.
Here is an image of the problem: http://i46.tinypic.com/m79cvs.png
<?php if (!empty($thelist)) { ?>
<p class="style12"><u>Fix</u></p>
<p class="style12"><?=$thelist?><?php echo " - " ?> <?php $filename = '../../customers/client1/client1.fix.exe';
if (file_exists($filename)) {
echo "" . date ("m/d/Y", filemtime($filename));
}
?> <?php echo " - <a href='download.php?f=client1/client1.fix.exe'><b>Download</b></a> <a href='download.php?f=client1/client1.fix.exe'>
<img src='../css/images/dlico.png' alt='download' width='35' height='32' align='absmiddle' /></a>" ?>
</p>
<?php } ?>
The list ($thelist) contains your files, yes?
You are not working on the $thelist, but on the $filename which is a hardcoded string.
Why? Currently you are outputting <?=$thelist?> and it looks like concatenated string from filenames. I would suggest that $thelist should be something like an array of your files. Then you could iterate over the files and output html dynamically for each entry.
<?php
// define your directory here
$directory = xy;
// fetches all executable files in that directory and loop over each
foreach(glob($directory.'/*.exe') as $file) {
// output each name and mtime
echo $file . '-' . date ("m/d/Y", filemtime($file));
// or you might also build links dynamically
// $directory needs to be added here
echo ''.$file.' - Size: '.filesize($file).'';
}
?>

Categories