Issue while embedding css in the php code? - php

I want to display 2 images with their created time to be displayed parallely in wordpress . So tried to write css (display flex) using 'echo' command . Although logic is correct but tag of the image is not working and there may be other syntactical error.As the beginners please review the code and help me.
In this below, if i close table tag before div it is not working properly.
<?php
$dir = opendir('/var/www/html/data1/products/images/avhrr/rgb');
$files = array();
while (($file = readdir($dir)) !== false)
{
if ($file == '.' || $file == '..' ) continue;
if (is_dir("/var/www/html/data1/products/images/avhrr/rgb/$file")) continue;
$lastModified = filemtime("/var/www/html/data1/products/images/avhrr/rgb/$file");
$files[$file] = $lastModified;
}
closedir($dir);
asort($files);
$files=array_reverse($files);
$count = 0;
foreach ($files as $file => $lastModified)
$name = basename($file);
$str = $name;
$pattern = "/top/i";
if(preg_match_all($pattern, $str, $matches))
{
$file_stats = stat("/var/www/html/data1/products/images/avhrr/rgb/".$name);
echo '<div class="container" style="display:flex">';
echo '<table border="5">';
echo '<tr><td>';
echo 'Daily_RGB : '.date('d-m-Y h:m:i A',$file_stats["mtime"]).PHP_EOL; //Equivalent of filemtime()
echo "<br>";
echo "<a href='/data1/products/images/avhrr/rgb/$file' target='_blank'></a>";
echo "<img src='/data1/products/images/avhrr/rgb/$file' height='450' width='450'/>";
echo '</td></tr>';
echo '</div>';
echo '</table>';
$count++;
}
if ($count == 2) break;
}
?>

Related

undefined offset: 1, when trying to access a jpg file within a folder

I got this error and I dont know why. I tried to change the code but nothing happened.
Its on the line:
if ($system[1] == $filters[$f]){$files[] = $file;}
I tried to change the value to 0, and it show nothing.
Here is the code:
<?php
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] =
$file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
$folder = "photobooth/photobooth/Michelle_Illona_Alexander/animated/"; //folder tempat gambar disimpan
$handle = opendir($folder);
$i = 1;
while(false !== ($file = readdir($handle) )){
if($file != '.' && $file != '..'){
$file2=str_replace("_mp4.jpg","",$file);
$file3=substr($file,0);
$filenames= directory(".","jpg");
foreach ($filenames as $value)
{
echo '<li>'.
'<a href="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file2.'.mp4">
<img src="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file.'" width="300" title="" type="jpg"></a>'.
'<br/></li>';
if(($i % 4) == 0){
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
}
$i++;
if($i==0)
break;
}
}
}
?>

echo 15 random lines of html files

I have to do a php assignment for college.
How would I echo 15 random lines of a html file if there is a movie script text and a image with the same in the same folder?
What I have tried so far:
$folder = 'Filmnoir/';
$hitchcock ='Hitchcock/';
$shakespear ='Shakespear/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$file2 = glob ($hitchcock.$filetype);
$file3 = glob ($shakespear.$filetype);
$count = count($files);
$count1 = count($file2);
$count2 = count($file3);
$scripttype ='*.html';
$Scripts = glob($ScriptFilmNoir.$scripttype);
$Scripts=file_get_contents("DoubleIndemnity.html");
if($_POST['radio1']=="0"){
if(($i %1)==0){
for ($i = 0; $i < $count; $i++)
{
echo '<div class="image2">';
echo '<img src="'.$files[$i].'" />';
echo '</div>';
if (condition) {
include 'DoubleIndemnity.html'; }
echo '</td></tr>';
}
echo '<div class="Passwordtext">';
echo 'Type in password to view full Script';
echo '</div>';
echo "<label><div class=\"password\"><input type='password' name='code' value='code'/></div></label>";
echo "<form method='POST' action='ca1_result.php'>";
echo "<div class=\"SubmitIt\"><input type='submit' name='submitScript' value='Submit Password'/></div>";
echo '</form>';
}
}
if($_POST['radio1']=="1"){
echo '<div class="Sorrytext">';
echo 'We apologize. No script available for this movie.';
echo '</div>';
}
if($_POST['radio1']=="2"){
echo '<div class="Sorrytext">';
echo 'We apologize. No script available for this movie.';
echo '</div>';
}
I want to achieve that when there is an image (e.g DoubleIndemnity.png) and movie script (e.g DoubleIndemnity.html) in the same folder that I get 15 random lines of the movie script text plus the image. Could I use the glob function and when yes how would I achieve that?
Can I ask another thing? When I submit the password how to I get then the full movie script?
I tried:
foreach($files as $file2) {
if($_POST['submitPassword']){
if($file2 === '.' OR $file2 === '..' OR $file2 === 'thumbs.db' OR !is_dir($folder.'/'.$file2)) {continue;}
if(file_exists($folder.'/'.$file2.'/doubleindemnity.gif') AND file_exists($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm')) {
echo '<div class="Container">';
echo "<div class='image2'><img src='$folder/$file/doubleindemnity.gif'>";
$lines4 = file($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
$count = count($lines4);
for($a = 0;$a < $count;$a++) {
echo substr($lines4[$a],strlen($folder),strpos($lines4[$a], '.')-strlen($folder));
}
echo "</div>";
}
echo "</div>";
}
}
?>
With that code I just get a couple of lines from the html file. I don't want that. I want the full text. And how can I use the string replace function to get rid of the code and just receive the text from the paragraphs?
Cheers:)
This piece of code will loop trough folders in a pre-defined folder (eg: /movies). It will look if whatever is inside is a folder, and if that folder has 2 files: image.png and lines.html. If it has those two files, it'll first place the image, then it'll read the HTML file, and writing down 15 random lines from that file.
<?php
$folder = "movies";
$files = scandir($folder);
foreach($files as $file) {
if($file === '.' OR $file === '..' OR $file === 'thumbs.db' OR !is_dir($folder.'/'.$file)) {continue;}
if(file_exists($folder.'/'.$file.'/image.png') AND file_exists($folder.'/'.$file.'/lines.html')) {
echo "<div class='image2'><img src='$folder/$file/image.png'>";
$lines = file($folder.'/'.$file.'/lines.html');
for($x = 1;$x<=15;$x++) {
echo $lines[rand(0, count($lines)-1)]."<br>";
}
echo "</div>";
}
}
Loops through files in $directory and then outputs 15 random lines into a html variable and if the image exists it adds it to the image html variable. If they are both added it returns the result. (untested but should work)
<?php
//I dont know where you have got some of these variables from
$directory = "Filmnoir";
$ScriptFilmNoir = "DoubleIndemnity";
$scripttype ='html';
$supportedImages = Array("png", "jpg", "jpeg");
$html = "";
$imagehtml = "";
$allFiles = scandir($folder);
$imageFound = 0;
$htmlFound = 0;
foreach($files as $file) {
if($file === '.' || $file === '..' || $file === 'thumbs.db' || is_dir($file)) {
continue;
}
$nameWithoutExtension = substr($file, 0 , (strrpos($file, ".")));
$fileExtension = $id = substr($file, strrpos($file, '.') + 1);
if($nameWithoutExtension == $ScriptFilmNoir){
if($fileExtension == $scripttype){
$htmlFound = 1;
$lines = file($directory . "/" . $ScriptFilmNoir . "." . $scripttype);//file in to an array
$range = range(1, count($lines));
$range = array_flip($range);
$range = array_rand($range, 15);
foreach($range as $line){
$html .= $lines[$value] . PHP_EOL;
}
}elseif(in_array ( $fileExtension , $supportedImages)){
$imageFound = 1;
$imagehtml = "<img src='{$directory}/{$ScriptFilmNoir}.{$fileExtension}' /><br />";
}
}
}
if($imageFound == 1 && $htmlFound == 1){
echo $imagehtml . $html;
}
I think what owen is looking for is a random extract of 15 lines rather than 15 random lines.
Using #ThijmenDF's example, just change
for($x = 1;$x<=15;$x++) {
echo $lines[rand(0, count($lines)-1)]."<br>";
}
to
$random = rand(0, count($lines)-1);
for($x = 1;$x<=15;$x++) {
echo $lines[$random + $x]."<br/>";
}

Search for a folder and and get the content of the files inside

I'm trying to search for a folder and retrieve the files inside of the folder (get content) I'm able to search for the folder using the follow code but I can't pass from there I can't see the content an retrieve the files inside. The files inside will be txt files and I would like to be able to open and see then.
How can achieve what i want? Thank you.
<?php
$dirname = "C:\windows";//Directory to search in. *Must have a trailing slash*
$findme = $_POST["search"];
$dir = opendir($dirname);
while(false != ($file = readdir($dir))){//Loop for every item in the directory.
if(($file != ".") and ($file != "..") and ($file != ".DS_Store") and ($file !=
"search.php"))//Exclude these files from the search
{
$pos = stripos($file, $findme);
if ($pos !== false){
$thereisafile = true;//Tell the script something was found.
echo'' . $file . '<br>';
}else{
}
}
}
if (!isset($thereisafile)){
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
?>
New code
<?php
$dirname = "C:\\Windows\\";//Directory to search in. *Must have a trailing slash*
$findme = 'maxlink'; //$_POST["search"];
$files = scandir($dirname);
foreach ($files AS $file)
{
if ($file == '.' or $file == '..' or $file == '.DS_Store' or $file == 'search.php') continue;
if (stripos($file, $findme) !== false)
{
$found = true;
echo 'FOUND FILE ' . $file . '<hr>';
echo 'OPENING IT:<br>';
echo file_get_contents($dirname . $file);
echo '<hr>';
}
else
{
echo 'not found: ' . $file . '<br>';
}
}
if (!isset($found))
{
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
The following code uses a recursive function for searching the directory. I hope it’ll solve your problem.
function scandir_r($dir){
$files = array_diff(scandir($dir), array(".", ".."));
$arr = array();
foreach($files as $file){
$arr[] = $dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
$arr = array_merge($arr, scandir_r($dir.DIRECTORY_SEPARATOR.$file));
}
}
return($arr);
}
$dirname = "C:\windows";
$findme = "/".preg_quote($_POST["search"], "/")."/";
$files = preg_grep($findme, scandir_r($dirname));
if(sizeof($files)){
foreach($files as $file){
$_file = $dirname.DIRECTORY_SEPARATOR.$file;
echo "$file<br/>";
}
}
else{
echo "Nothing was found.";
echo "<img src=\"yourimagehere.jpg\"/>";
}

array output not reading correctly

My issue is that one part displays file directory names and the other parts are supposed to be file counts for certain folders in those directories. right now it is listing the directory names but it is only doing a file count on the first directory and not on any of the others. i do realize that currently the code is for the counts under the In Que table header but if i get this figured out it makes it easier to use for the other categories.
but yes the issue is that i need it to grab file counts from all locations not just the first file path it finds.
This is the code:
<?php
if ($handle = opendir('Users/')) {
$blacklist = array('.', '..', 'somedir', 'somefile.php');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
$i = 0;
$dir = 'Users/';
$files1 = scandir($dir);
$x = 1;
while ($x <= 4 and $i <= 4){
$x++;
$dir1 = 'Users/'.$files1[$x].'/uploaded/';
if ($handle1 = opendir($dir1)) {
while (($file1[$x] = readdir($handle1)) !== false){
if (!in_array($file1[$x], array('.', '..')) && !is_dir($dir1.$file1[$x]))
$i++;
}
}
$file."\n" == $option; ?>
<table border='1px' style="border:thin;" cols="3">
<th>Employee</th>
<th>In Que</th>
<th>Incomplete</th>
<th>Processed</th>
<?php
echo "<tr>";
echo "<td>";
echo "$file\n";
echo "</td>";
echo "<td>".$i."</td>";
echo "<td>" ."</td>";
echo "<td>" ."</td>";
echo "<tr>";
echo "<br>";
?>
</table>
<?php
}
// integer starts at 0 before counting
}
}
}
?>
You are incrementing $x as soon as you enter loop so you will miss $files1[1] to start with...
If I am not misinterpreting this you are can get that achieved by couple loops.
$count = 0;
$folders = #scandir('Users');
foreach($folders as $item){
if (preg_match("/.*\.php$/",$item)) continue;
if (is_dir("Users/$item")){
$target_folders = #scandir("Users/$item/uploaded/");
foreach($target_folders as $target_item){
if ((!preg_match("/^[.]/",$target_item)) || (!is_dir("Users/$item/uploaded/$target_item"))) $count++;
}
}
}

Pull Images from directory - PHP

I am trying to pull images simply from my directory /img and load them dynamically into the website into the following fashion.
<img src="plates/photo1.jpg">
That's it. It seems so simple but all of the code I have found basically doesn't work.
What I have that I am trying to make work is this:
<?php
$a=array();
if ($handle = opendir('plates')) {
while (false !== ($file = readdir($handle))) {
if(preg_match("/\.png$/", $file))
$a[]=$file;
else if(preg_match("/\.jpg$/", $file))
$a[]=$file;
else if(preg_match("/\.jpeg$/", $file))
$a[]=$file;
}
closedir($handle);
}
foreach($a as $i){
echo "<img src='".$i."' />";
}
?>
This can be done very easily using glob().
$files = glob("plates/*.{png,jpg,jpeg}", GLOB_BRACE);
foreach ($files as $file)
print "<img src=\"plates/$file\" />";
You want your source to show up as plates/photo1.jpg, but when you do echo "<img src='".$i."' />"; you are only writing the file name. Try changing it to this:
<?php
$a = array();
$dir = 'plates';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (preg_match("/\.png$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
}
closedir($handle);
}
foreach ($a as $i) {
echo "<img src='" . $dir . '/' . $i . "' />";
}
?>
You should use Glob instead of opendir/closedir. It's much simpler.
I'm not exactly sure what you're trying to do, but you this might get you on the right track
<?php
foreach (glob("/plates/*") as $filename) {
$path_parts = pathinfo($filename);
if($path_parts['extension'] == "png") {
// do something
} elseif($path_parts['extension'] == "jpg") {
// do something else
}
}
?>

Categories