php list indents or doesnt link - php

I am using this code to list all the files in a Dir:
<?php
if ($handle = opendir('CD300/')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".."){
$thelist .= '<a href="/CD300/'.$file.'" target="_blank"
style="color: #0f33cc">'.$file.'</a></br>';
}
}
closedir($handle);
}
echo "
<div id='output'>
List of help files:</div>
<div id='List'>
<?=$thelist
</div>"
?>
If I set the a href to have < li> tags then I get the list however the first result is not indented and the rest are. As I currently have it set, all of the files are displayed correctly however the first link is non clickable.
Am I missing something?

Remove the <?= markup, you are already printing the output from within PHP, so what happens is that <?= is literally appended in the first line just before you output the file list.

Related

Random image of day (PHP)

I'm trying to add a simple random image of the day script. It doesn't point to the right file location though. Here is the code:
<?php
$i=0;
$path="images";
$ext = "jpg";
$extra= "alt=\"Random Image\" float=\"left\"";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if (substr($file,strlen($file)-3,3)==$ext)
{ $imgs[$i++]=$file;
}
}
closedir($handle);
$today=getdate();
srand($today['mday']+$today['month']+$today['year']);
$r=rand(0,$i-1);
echo("<img src=images/\"$imgs[$r]\" $extra>");
}
?>
The image source when I right click and check properties is adding %22 before and after the file. EX: mobile/images/%22image.jpg%22
Therefore, the image isn't showing because of it I presume.
That %22 is actually that superfluous " inside your url source, remove those:
echo "<img src=\"images/{$imgs[$r]}\" alt=\"Photo\" />";
^ opening ^ closing

How to echo the source code of multiple files using file_get_contents PHP?

<textarea placeholder="Source code of file" class="source">
<?php echo ($thesource) ?>
</textarea>
<?php
$blacklist = array("one.jps", "two.txt", "four.html");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
$thesource = file_get_contents($entry);
echo "<div class='post'
<p>$entry</p>
</div>
";
}
}
closedir($handle);
}
?>
Output:
<textarea placeholder="Source code of file" class="source">
<!DOCTYPE html>
<html>
etc
etc
</body>
</html>
</textarea>
<p>ok.html</p>
<p>source.php</p>
<p>sub.html</p>
<p>stylesheet.css</p>
As confusing this may look, let me try to explain what I want to achieve.
Lets say that there are some files in a directory (4 in this case). The PHP code I am using will echo out all those 4 files onto the page that the PHP code is on - it excludes everything in the blacklist. So it will look a little something like this:
<p>ok.html</p>
<p>source.php</p>
<p>sub.html</p>
<p>stylesheet.css</p>
There is also a textarea:
<textarea placeholder="Source code of file" class="source">
<?php echo ($thesource) ?>
</textarea>
This textarea will have a value of $thesource. Whatever is in the $thesource variable will appear in the textarea.
To define the $thesource variable, this is the PHP code I am using:
$thesource = file_get_contents($entry);
Note that $entry is all the files that are echoed out onto the page (as explained above - 4 files in this case)
I am trying to make it so that, whenever a user clicks on one of the files:
<p>ok.html</p>
<p>source.php</p>
<p>sub.html</p>
<p>stylesheet.css</p>
When the user clicks on those listed above, it will display the source code of the clicked file in the textarea.
The current source code I am using, only echo's out the source code of the current file containing the PHP code.
How would I achieve this? Thanks - and if you are still unsure of what I am trying to achieve, then please ask!
This should work for you:
First i get all files in a directory with glob() which aren't in the blacklist. After that i print a list which you can click on it.
If you click on it the file get's included and displayed in the textarea.
<?php
$blacklist = array("one.jps", "two.txt", "four.html");
$files = array_diff(glob("*.*"), $blacklist);
foreach($files as $file)
echo "<div class='post'><a href='" . $_SERVER['PHP_SELF'] . "?file=" . $file . "'><p>" . $file . "</p></a></div>";
if(!empty($_GET["file"]) && !in_array($_GET["file"], $blacklist) && file_exists($_GET["file"]))
$thesource = htmlentities(file_get_contents($_GET["file"]));
?>
<textarea rows="40" cols="100" placeholder="Source code of file" class="source"><?php if(!empty($thesource))echo $thesource; ?></textarea>

PHP - Scan dir for folders and txt

I am dynamically building an accordion menu. Accordion will get the header information from folder names and contents from .txt files associated to folder names. They are relatives in terms of directory.
<div class="accordion">
<?php if($_GET['cat']!='') {
$handleCat = 'tv/'.$_GET['cat'];
$category = scandir($handleCat);
$i = 1;
foreach ($category as &$value) {if ((!in_array($value,array(".","..","...")))){
echo '<div class="header">'.$value.'</div><div class="content" id="ac'.$i.'">'.file_get_contents($value.".txt", false).'</div>';
$i+=1;}}}
?>
</div>
In my code there are two problems. First one is logic problem. I couldn't made up scan foldernames and file names seperately. Forexample program1.txt also becomes a headername. Second problem is method problem. I found file_get_contents() method but this doesn't extracts .txt file contents.
You can distinguish files from folders using the function is_dir().
As of file_get_contents, it reads the file contents but does not echo it. Use :
echo '<div class="header">'.$value.'</div>'.$value.'<div class="content" id="ac'.$i.'">';
echo file_get_contents($value.".txt", false);
echo'</div>';
Use the following to list files in a directory. Where I commented code you can do whatever you want with that particular file. You can use is_dir() to distinguish from files and directories and then proceed accordingly.
<?php
if ($dir = opendir('.')) {
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
echo "$file\n";
//code
}
}
closedir($handle);
}
?>
Read the contents of a file using the following code.
$contents = file_get_contents($file);

Using PHP to display a folders contents, but show a message when folder is empty

I'm creating a intranet for my workplace and have used a bit of php I found online to scan the contents of the folder it's in and display them as links. It does this fine, but when it's inside an empty folder I would like it to display a message such as "There are no records matching those criteria.".
Is there a way to add something to the php to specify if there are no folders listed print this?
I have next to no knowledge of php, but html and css are no problem.
Here's the php I'm using in the page:
<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "A.php")
{
array_push($files, $file);
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
print "<div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
?>
If you need anymore code such as the full page html or css just let me know.
Thanks in advance for any help.
EDIT:
After trying Josh's solution it pretty much nailed it, but I'm now getting "No files found" printing 3 times. Here's the code I'm using now:
<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
if( count($files) == 0 )
{
echo '<p>No files found</p>';
}
else
{
if ($file != "." and $file != ".." and $file != "A.php")
{
array_push($files, $file);
}
}
}
closedir($dir);
sort($files);
foreach ($files as $file)
print " <div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
?>
Just do an:
if( count($files) == 0 )
{
echo '<p>No files found</p>';
}
else
{
// you have files
}
You can use the count function to check if there are any files in your files array like this:
if(count($files) > 0) // check if there are any files in the files array
foreach ($files as $file) // print the files if condition is true
print " <a href='$file'>$file</a> <br />";
else
echo "ERROR!";
EDIT:
You can also use the scandir function. However, this function will return two extra entries for the current directory and directory up one level. You need to remove these entries from the files array. Your code will look like this:
<?php
$dir = "."; // the directory you want to check
$exclude = array(".", ".."); // you don't want these entries in your files array
$files = scandir($dir);
$files = array_diff($files, $exclude); // delete the entries in exclude array from your files array
if(!empty($files)) // check if the files array is not empty
{
foreach ($files as $file) // print every file in the files array
print " <div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
}
else
{
echo "There are no files in directory"; // print error message if there are noe files
}
?>
Try This:
<?php
$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "A.php")
{
array_push($files, $file);
}
}
closedir($dir);
if(count($files) == 0){
die("There are no records matching those criteria.");
}else{
sort($files);
foreach ($files as $file)
print " <div class='fileicon'>
<a href='$file'>
<img src='../../../images/TR-Icon.png'>
<p class='filetext'>$file</p>
</a>
</div>";
}
?>
You can use a simple conditional using count on your file array.
// ...
closedir($dir);
if (count($files) > 0) {
// sort files and iterate through file array, printing html
} else {
echo "There are no records matching those criteria.";
}
// ...
I think you could let it print something else if($files.length == 0) and only print it normally if($files.length > 0) or something.
I have no knowledge of php, but I know java, html, css, and javascript.
I'm sure php has things like array.length (or something else to get the length of an array) in it
I hope this was helpful.
EDIT: I've seen others already answered thigs that are like 10x better than mine.
Also, php seems pretty cool, I might learn it

Displaying output of file from a dropdown menu selection

First off, I am totally new to PHP, but so far i love the possibilities i have seen with it.
Ok, heres my problem. I have a script that will automatically scan a folder and show me the file names of that folder in a drop-down menu.
<?php
$dirname = "logs";
$dir = opendir($dirname);
echo '<select name="file2">';
echo '<option value="">Logfiles</option>';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo "<option value=".$file.">$file</option>";
}
}
echo '</select>';
?>
So far so good, that part works just fine.
I then have a piece of code that would show me the contents of a chosen file.
<?php
$content = file("filenamehere");
$data = implode("<br>",$content);
echo $data;
?>
This part on its own also works fine.
I now want to combine these 2 scripts. This is where i get totally lost. I have tried all various combinations of which variable to put as a "filenamehere", but I only get as far as having my choice from the drop-down echoing the chosen filename in the drop-down button. I have not been able to actually get the 2nd part of the code to display the file contents of the file I have chosen. I tried things like ("$file") for the $content variable but nothing happens.
Of course any other single script solution would also be great. I just need to be able to scan a folder, have its files listed in a drop-down, and once i choose a file i want it to be displayed on the page.
Any help would be deeply appreciated since im a total beginner in PHP.
Cheers.
Am assuming you are doing everything in a single page. I have wrapped a form tag around the select dropdown and added a submit button
echo '<form name="displayfile" action="" method="POST">';
echo '<select name="file2">';
echo '<option value="">Logfiles</option>';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo "<option value=".$file.">$file</option>";
}
}
echo '</select>';
echo '<input type="submit" value="display file" />';
echo '</form>';
The second part
<?php
//file2 is the name of the dropdown
$selectedfile = #$_POST['file2'];
$content = file($selectedfile);
$data = implode("<br>",$content);
echo $data;
?>
Hope this helps

Categories