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
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.
I m new to PHP so please bear me
There is a dir name filename which contains diff sub-dir
in these sub-dir there are some .md files
I want to get the content of all of these files and show it like
Filename:""
Content: ""
I tried to use this but no luck
<?php
$directory = "./dirname";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if ($type == 'file') {
$contents = file_get_contents($filename);
$items = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
foreach ($items as $item) {
echo "<tr><td>$item</td></tr>\n";
}
echo '</table>';
}
}
closedir($dir);
?>
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++;
}
}
}
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.