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++;
}
}
}
Related
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;
}
?>
Hi i have this problem that my code can't read my numbers correctly. the file im trying to read is a .csv file seperated by commas. any help is appreciated.
i havent been able to find anything that does exacly as i needed yet, it works if i replace the , with . but i need it to be , for my project.
<?php
// inkludere vores footer hvis logget ind
if ($user->is_loggedin() == true) {
// File selector
$path = "./assets/csv";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// table start
echo'<h1 class="text-center">CSV Table</h1>';
echo'<h6 class="text-center">'.$latest_filename.'</h6>';
echo '<table id="example" class=" table table-striped table-bordered" style="width:100%">';
echo'<tbody>';
$f = fopen("$path/$latest_filename", "r");
while (($line = fgetcsv($f)) !== false) {
$row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array)
$cells = explode(";",$row);
echo '<tr>';
foreach ($cells as $cell) {
echo '<td>' .htmlspecialchars($cell, ENT_COMPAT). '</td>';
}
echo '</tr>';
}
fclose($f);
echo'</tbody>';
echo '</table>';
}
the table Looks like in excel
https://gyazo.com/49e8b16d369b1fa61496601952ca953b
and here is how it looks on my site
https://gyazo.com/b271c3a221587b2e0c6b7ee4cebd6bdb
From what i have read, the reason i get these errors is because i need UTF-8, to be able to read my language
I finally found the answer myself... after a long time of testing none stop, i found this answer
// File selector
$path = "./assets/csv";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// table start
echo'<h1 class="text-center">CSV Table</h1>';
echo'<h6 class="text-center">'.$latest_filename.'</h6>';
echo '<table id="example" class=" table table-striped table-bordered" style="width:100%">';
echo'<tbody>';
$f = fopen("$path/$latest_filename", "r");
while (($line = fgetcsv($f, 1000, ";")) !== false) {
$row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array)
// $cells = explode(";",$row);
echo '<tr>';
foreach ($line as $cell) {
echo '<td>' .htmlspecialchars($cell, ENT_COMPAT). '</td>';
}
echo '</tr>';
}
fclose($f);
echo'</tbody>';
echo '</table>';
}
hope someone could use this, what i have noticed with this solution is that, i think the while (($line = fgetcsv($f, 1000, ";")) !== false) { . ive tested it and it could get over 10000 lines. Anyways what i found out saves someone some time. I for sure could have used it.
Hi i have this csv file reader. I have this problem tho, it makes blank cells when it has some special characters like, "ø" "," "-". there might be more. Here is my code:
<?php
// inkludere vores footer hvis logget ind
if ($user->is_loggedin() == true) {
// File selector
$path = "./assets/csv";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// table start
echo'<h1 class="text-center">CSV Table</h1>';
echo'<h6 class="text-center">'.$latest_filename.'</h6>';
echo '<table id="example" class=" table table-striped table-bordered" style="width:100%">';
echo'<tbody>';
$f = fopen("$path/$latest_filename", "r");
while (($line = fgetcsv($f)) !== false) {
$row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array)
$cells = explode(";",$row);
echo '<tr>';
foreach ($cells as $cell) {
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '</tr>';
}
fclose($f);
echo'</tbody>';
echo '</table>';
}
This is how it is shown when i try show the file.
I will use the | to indicate cells and write "Blank" when it shows a blank cell
|Name|Datum|Property|Criterion|Type|Nominal|Actual|Tol-|Tol+|Dev|
|1)Height|Blank|Blank|Blank|inp|Blank|Blank|Blank|Blank|Blank|
How it should look:
|Name|Datum|Property|Criterion|Type|Nominal|Actual|Tol-|Tol+|Dev|
|1)Height|Blank|Ø|Blank|inp|123,3|123,3|-1|1|-0,24|
Hope this makes sense to you
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/>";
}
I have several files in a directory.I want to display all those filenames with the extension .txt and .jpeg
<?php
if ($handle = opendir("/home/work/collections/utils/")) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..') {
continue;
}
$actual_file=pathinfo("/etrade/home/collections/utils");
if (($actual_file["extension"]== "txt") ||
($actual_file["extension"]== "jpg") ||
($actual_file["extension"]== "pdf")) {
//Require changes here.Dont know how to iterate and get the list of files
echo "<td>"."\n"." $actual_file['basename']."</a></td>";
}
}
closedir($handle);
}
Please help me on how to iterate and get the list of files .For instance I want all files with jpg extension in a seperate column and pdf files in a seperate column(since I am going to display in a table)
See if this does what you want (EDITED):
<?php
$ignoreFiles = array('.','..'); // Items to ignore in the directory
$allowedExtensions = array('txt','jpg','pdf'); // File extensions to display
$files = array();
$max = 0;
if ($handle = opendir("/home/work/collections/utils/")) {
while (false !== ($file = readdir($handle))) {
if (in_array($file, $ignoreFiles)) {
continue; // Skip items to ignore
}
// A simple(ish) way of getting a files extension
$extension = strtolower(array_pop($exploded = explode('.',$file)));
if (in_array($extension, $allowedExtensions)) { // Check if file extension is in allow list
$files[$extension][] = $file; // Create an array of each file type
if (count($files[$extension]) > $max) $max = count($files[$extension]); // Store the maximum column length
}
}
closedir($handle);
}
// Start the table
echo "<table>\n";
// Column headers
echo " <tr>\n";
foreach ($files as $extension => $data) {
echo " <th>$extension</th>\n";
}
echo " </tr>\n";
// Table data
for ($i = 0; $i < $max; $i++) {
echo " <tr>\n";
foreach ($files as $data) {
if (isset($data[$i])) {
echo " <td>$data[$i]</td>\n";
} else {
echo " <td />\n";
}
}
echo " </tr>\n";
}
// End the table
echo "</table>";
If you just want to display two lists of files (it's not clear what part you're having trouble with from your question) can't you just store the filenames in an array?
You don't seem to be getting the file details - you're getting the pathinfo for /etrade/home/collections/utils, but you never add the file name to it.
<?php
if ($handle = opendir("/home/work/collections/utils/")) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..') {
continue;
}
$actual_file=pathinfo($file);
switch ($actual_file['extension'])
{
case ('jpg'):
$jpegfiles[] = $actual_file;
break;
case ('pdf'):
$pdffiles[] = $actual_file;
break;
}
}
closedir($handle);
}
echo "JPG files:"
foreach($jpegfiles as $file)
{
echo $file['basename'];
}
echo "PDF Files:"
foreach($pdffiles as $file)
{
echo $file['basename'];
}
?>
Obviously you can be cleverer with the arrays, and have use multi-dimensional arrays and do away with the switch if you want.