How to change the style of a few echoed pieces of text? - php

<?php
$blacklist = array("no.html", "one.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
closedir($handle);
}
?>
What happens here is that every file in the directory (excluding those in the blacklist) get echoed out like so:
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
However, is there any way to style certain elements? For example, every 3 echo's, make the text bigger and the color red.
E.g.
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html

Try this. I think it will help you
$i = 1;
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if($i%3 == 0){
echo "<a href='$entry'><strong><li id='file'>$entry</li></strong></a>";
}else{
echo "<a href='$entry'><li id='file'>$entry</li></a>";
}
}
$i++;
}

Something like this with the counter initialized will do the job.
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if ($i%3==0)
{
//the echo with the style
}
else
{
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
$i++
}

Related

unlink a file in a lower directory

I am trying to find a solution to remove files from a directory listing. I found an example that works bery well for files within the same directory. However I assume that I would need to change directories but I am not understanding how to accomplish this.
Here is the example that works for files in the current directory
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br><b>$entry - Delete<br></b>";
}
}
closedir($handle);
}
?>
Ok got it.
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('../videos/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br><b>$entry - Delete<br></b>";
}
}
closedir($handle);
}
?>
I just added the path after href=\?delete= and before $entry

how can i add child node to the parents nodes in php?

I am reading a rootpath and listing all the folder. I have to add some child node to every folder programatically ?
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li><a href=/hi.php?dev=$entry&action=viewcam>$entry</a>";
}
}
closedir($handle);
?>
I am getting like
- folder1
- folder2
but I want like
- folder1
Appple
orange
- folder2
Appple
orange
Any suggestion ?
There is no need to specifically "create a node" on the server side for this. All you have to do is output the html markup you want to have:
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
echo "<ul>\n";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li>$entry</li>\n";
echo "<ul>\n";
foreach (['Apple', 'orange'] as $fruit) {
echo "<li>".$fruit."</li>\n";
}
echo "</ul>\n";
}
}
echo "</ul>\n";
}
closedir($handle);
?>
(I fixed a few minor issues with your code on-the-fly...)
Obviously this is just an example to show the basic approach.

PHP: Where to put rsort?

$count = 0;
if ($handle = opendir('./arkiv/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$count++;
print("".basename($file, ".php")."<br />\n");
}
}
echo '<br /><br />Tilbake';
closedir($handle);
}
This is the code I'm using, and I'm trying to figure out where to but 'rsort', but this is very new to me, can anyone help me?
Possible use can be like
$count = 0;
$fileArray = array();
if ($handle = opendir('./arkiv/'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$count++;
$fileArray[] = $file;
}
}
rsort($fileArray);
foreach($fileArray as $file)
{
print("".basename($file, ".php")."<br />\n");
}
echo '<br /><br />Tilbake';
closedir($handle);
}
Give it a try ....
GOOD LUCK !!!

Strip the extension from a file

I have a code that is working as i want it to apart from 1 problem.
You can see the result here http://test.whatanswered.com/health/what-can-a-first-aider-do.php on the right below "Related Articles" showing dead links.
The following is the HTML that should be displayed.
<p>Name of the page</p>
I would like to strip the dashes and php as above "Name of the page" but it also strips the dashes and php from the url.
The code is:
<?php
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[$file] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' ');
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file => $health) {
$thelist .= '<p>'.$health.'</p>';
}
}
?>
<?=$thelist?>
I've refactored your code a little - the $fileTab array now just stores filenames and converting this to a title happens at the point of display:
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$thelist .= '<p>'.$title.'</p>';
}
}

How to exclude certain file types with PHP readdir?

I am using a php scan directory script that will scan the contents of a directory and then populate the page with links to the directory contents.
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("".$file."<br />\n");
}
}
echo '<br /><br />Return';
closedir($handle);
}
?
I am wondering how I can exclude certain files or file types like test.xml, durka.xslt or .html from showing up on the populated page. I have some code but not sure how to integrate it. Any help would be very appreciated...
?php
if ($handle = opendir(‘.’)) {
while (false !== ($file = readdir($handle)))
{
if ($file != “.” && $file != “..”
&& $file != “NOT-TO-BE-IN-1.php”
&& $file != “NOT-TO-BE-IN-2.html”
&& $file != “NOT-TO-BE-IN-3.jpg”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”
&& $file != “”
)
<?php
// These files will be ignored
$excludedFiles = array (
'excludeMe.file',
'excludeMeAs.well'
);
// These file extensions will be ignored
$excludedExtensions = array (
'html',
'htm',
'php'
);
// Make sure we ignore . and ..
$excludedFiles = array_merge($excludedFiles,array('.','..'));
// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
// Loop through directory
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
$extn = explode('.',$file);
$extn = array_pop($extn);
// Only echo links for files that don't match our rules
if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)) {
$count++;
print("".$file."<br />\n");
}
}
echo '<br /><br />Return';
closedir($handle);
}
?>
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".."
&& $file != "NOT-TO-BE-IN-1.php"
&& $file != "NOT-TO-BE-IN-2.html"
&& $file != "NOT-TO-BE-IN-3.jpg"
&& substr($file,-strlen(".html")) != ".html" //if you don't want to include .html files, for instance
&& substr($file,-strlen(".js")) != ".js" //if you don't want to include .js files, for instance
&& $file != ""
) {$count++;
print("".$file."<br />\n");
}
}
echo '<br /><br />Return';
closedir($handle);
}
?>
Other way:
$excludeExtensions = array(
'php',
'html',
'jpg'
);
if ($file != "." && $file != ".." && !in_array(pathinfo($file, PATHINFO_EXTENSION), $excludeExtensions))
EDIT: again I was too late:)
you can also just use glob:
foreach (glob("*.{php|html|jpg}",GLOB_BRACE) as $file) {
echo file_get_contents($file);
}
see http://php.net/manual/en/function.glob.php for more info
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".."
&& $file != "NOT-TO-BE-IN-1.php"
&& $file != "NOT-TO-BE-IN-2.html"
&& $file != "NOT-TO-BE-IN-3.jpg"
&& $file != ""
&& $file != ""
&& $file != ""
&& $file != ""
&& $file != ""
)
echo file_get_contents($file);
will show contents of all pages which haven't name
NOT-TO-BE-IN-1.php
NOT-TO-BE-IN-2.html
NOT-TO-BE-IN-3.jpg
you can restrict certain filepath with
if (!preg_match('/(php|html|jpg)/', $file))
Try something like:
$excluded_files = array('test.xslt');
$excluded_ext = array('html');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." &&
!in_array($file, $excluded_files) &&
!in_array(pathinfo($file, PATHINFO_EXTENSION), $excluded_ext))
{
// do stuff
}
}

Categories