Remove first two entrys from subfolder reading php script - php

i got a function which gets all subfolders from a specific directory and display subfolders in html output with php.It is working so far but now i have two entrys from the folder above which is diplayed as . and the second entry as .. how i have to modify my script?
<?php
$dir = "pictures/whatsapp/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
// Where to filter or delete the two entrys from folder . and .. ???
echo '<i class="ion-social-whatsapp-outline"></i>'.ucwords(str_replace("-"," ", $file)).'<i class="ion-record"></i>';
}
closedir($dh);
}
}
?>

simply add to loop following line to skip om . and ..
if ($file == "." or $file == "..") continue;

Related

how to search a folder for images that contain the keyword in their name in php

im making a photo gallery and i want to make a search bar which displays all the images that have the searched keyword in their name. i have stored my images in a folder (NOT DATABASE, IN A FOLDER).
i have made the search bar but i cant get the PHP part working. do i need ajax or jquery ?
this is my current code which returns nothing when i press search. not even the test " eco 'hello' " part.
?php
$dir = "/uploads";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == $_POST['searching']){
// replace this line with eco <img> line
echo(''. $file .''."\n");
echo "hello" ;
}
}
closedir($dh);
}
}
?>
i want to get a list of the images with keyword (searching) in the name.
It could be as simple as replacing if($file == $_POST['searching']){ with if (false !== strpos($file, $_POST['searching'])){.
You could use scandir() to get an array of files in the directory and then loop through it:
$dir_array=scandir($dir);
foreach($dir_array as $file){
if($file == $_POST['searching']){
// open file etc. here
echo(''. $file .''."\n");
}
}

PHP - Reading the Contents of a Directory

So. I'm trying to make a simple PHP program that will read the contents of a directory. I've been working off W3Schools. And it's been working well, except for one small problem.
When this script runs, it posts two additional filen that don't exist - even if the directory is completely empty .
<?php
$dir = "./userphotos/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
print <<< HERE
<p>Filename: $file</p>
HERE;
}
closedir($dh);
}
}
?>
Any thoughts?
On Unix machines, each directory contains 2 hidden files.
. and .. these are references to the current and parent directories.
You should look into DirectoryInterator class
$dir = "./userphotos/";
foreach (new DirectoryIterator($dir) as $fileInfo) {
if($fileInfo->isDot() === false) {
echo $fileInfo->getFilename() . "<br>\n";
}
}
This example ignores the "dot" files
Also, you can look into RecursiveDirectoryIterator to do this recursively.

Dynamic link created based on new file upload?

Every 72 hours I upload a new PHP file to my server. (well actually it is an xml file transformed on the server with php) Is there a method to create a link on an html page that links to the "new" PHP doc automatically everytime a new file is uploaded?
I don't want to manually change the link every 72 hours. I would ultimately like to have an html page with a list of links to every new doc that is uploaded. I found this for images but I need someting like this but for PHP files and links.
http://net.tutsplus.com/articles/news/scanning-folders-with-php/
Any help would be very appreciated.
I found a solution that add links to the xml files. Now I just need to figure out how to add a link to reference the xslt sheet for each new xml file that is upload AUTOMATICALLY. I am not sure how to do this but any help would be very helpful. Thanks for everyones help.
<?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);
}
?>
To read in a directory of files and then sort them by upload time you can just use:
$files = glob("files/*.xml");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
print "link: " . current($files); // make that an actual <a href=
You can do that pretty easily with PHP function readdir:
http://php.net/manual/en/function.readdir.php
Simply loop through the files in the directory where you upload files and have php output a link for each.
ie:
<?php
if ($handle = opendir('/path/to/upload_dir')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo '' . $file . '<br />';
}
}
closedir($handle);
}
?>
You'll need to edit the http:// URL on the href to point to the correct download URL for your server, as well as the server path for opendir.
Hope that helps.
list by filetype
<?php
if ($handle = opendir('/path/to/dir')) {
while (false !== ($file = readdir($handle))) {
if (strpos($file, '.php',1)||strpos($file, '.xml',1) ) {
echo "<p>$file</p>";
}
}
closedir($handle);
}

php directory explorer script help

I have created this php script which displays the contents of a designated directory and allows users to download each file. Here is the code:
<?php
if ($handle = opendir('test')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href='test/$file'>$file\n</a><br/>";
}
}
closedir($handle);
}
?>
This script also displays folders, but when I click a folder, it does display the contents of the folder, but in the default Apache autoindex view.
What I would like the script to do when a folder is clicked, is display the contents, but in the same fashion as the original script does (as this is more editable with css and the like).
Would you know how to achieve this?
Don't create a link to the directory itself, but to a php page which displays the contents.
Change your php code to somthing like:
if(isset($_REQUEST['dir'])) {
$current_dir = $_REQUEST['dir'];
} else {
$current_dir = 'test';
}
if ($handle = opendir($current_dir)) {
while (false !== ($file_or_dir = readdir($handle))) {
if(in_array($file_or_dir, array('.', '..'))) continue;
$path = $current_dir.'/'.$file_or_dir;
if(is_file($path)) {
echo ''.$file_or_dir."\n<br/>";
} else {
echo ''.$file_or_dir."\n<br/>";
}
}
closedir($handle);
}
PS write you html code with double quotes.
You need your HREF to point back to your PHP script, and not the directory. You will then need to update your PHP script to now which directory it needs to read.

List all files in a directory ,extra information is coming

here my code-
if ($handle = opendir('banner/')) {
while (false !== ($file = readdir($handle))) {
echo "$file";
}
closedir($handle);
}
wher I run this code unnecessary dots(.) are coming.
output image-3.jpgimage-4.jpgimage-1.jpgimage-2.jpgimage-5.jpg... why 3 dots are coming at the last??
Because . is the current directory and .. is the parent directory.
They are always exists.
If you need to exclude them - just add
if ($file != '.' && $file != '..')
right before echo
It's because there are items in your directory which you don't see... one of them is . and represents the current directory, and the other is .. and represents the directory above the current one. You need to filter these out of any readdir results.

Categories