PHP readdir(): sort folder first and then files - php

How to sort the folders first, then the files with the readdir?
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$file . "\n";
}
closedir($dh);
}
need
..
dir/
dir/
file
file
file
The folders can contain more than 50K files and sub-folders, I don't want to store the results in an array (scandir, etc.) for performance reasons.
Thanks

Related

Take files from directory and insert content into SQL

I have 800+ text files in directory called documents. All have txt extension. from 100 to 500kb each file with text and html tags. Now i want import all files to my database. It's possible to do this with php? Also want to delete file.
I know only how to get filenames from dir but how to take content and insert into database?
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: .".$file."<br />";
}
closedir($dh);
}
}
You should be able to load the contents of the file into a string and then write the string to sql
This will give you the contents of the file:
$fileContents= file_get_contents($file);

Match string to a filename from folder using php

Hi I wonder if it is possible to match a string to a file from folder using php.
For example I have a folder called uploads and inside, I have different files like image1.png, image2.jpg, doc1.doc, and doc2.pdf.
Assuming I have this code on my php file:
<?php
$string = "image2";
// I need some function to display the image2 on my webpage.
// If string "image2" is found in the uploads folder
// then it should display the image
?>
Thanks!
I think this one should do what you want
$dir = "uploads";//the path to your folder
if(file_exists($dir)){
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file)) {
if ($file == "image2"){
// your code
}
}
}
}
}

how can i return the names of files in a directory using php

lets say I have a folder on a webhost that is called sebis_files and this folder contains some files, maybe pictures, docs...
I want to return the contents of this folder on a separate page, something like:
$row = get dir host/sebis_files*//everything
for ( $row !== 0){ //for every valid file
echo $row . "<br/>"; //return name of file
}
You can use opendir and readdir. Here's a breakdown:
We use __DIR__ to make the path relative to the directory of the current script, just to be safe:
$dir = __DIR__ . '/sebis_files';
Next we open the directory to read it's entries.
We call readdir, which will return a 'resource' object, or false if $dir is not a readable directory:
if ($dh = opendir($dir))
{
The directory is successfully opened.
We now call readdir on that directory. We use the return value of opendir, the mysterious 'resource' object, that will let PHP know what directory we are reading.
Every time we call readdir it will give us the next entry in the directory. When there are no more entries, readdir will return false:
while ( ($entry = readdir($dh)) !== false)
{
We have read a directory $entry: the name of a file or sub-directory inside $dir. So, it's not a full pathname. Let's print it's name, along with whether it is a directory or a file. We will use is_file and is_dir, but we will need to pass the full pathname (hence "$dir/$entry"):
if ( is_dir( "$dir/$entry" ) )
echo "Directory: $entry<br/>";
else if ( is_file( "$dir/entry" ) )
echo "File: $entry<br/>";
}
we are done with the directory, let's close it to free the resource:
closedir($dh);
}
But what if $dir cannot be opened for reading? Let's print a warning:
else
echo "<div class='warning'>cannot open directory!</div>";
you need is to see this
<?php
$dir = "/tmp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
print_r($files);
rsort($files);
print_r($files);
?>
You can do it using the glob function :
$dir = "/your/dir/";
if(file_exists($dir))
{
foreach (glob("$dir*") as $file)
{
if(is_file($file))
{
echo basename($file) . "<br />";
}
}
}

Read one file name "Only" and skip to next directory - PHP

I have the following code which reads all filnames from each directory, but I want it to read one filename "only" and skip to the next directory.
<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
How can I read only one filename, then skip to the next directory to read "only" first filename and so on. Please let me know if you require any more information.
if it doesn't matter which file you read then just put a break; in your while loop or don't even go in a loop just take the $file = readdir($dh);
PS: In linux OS be aware of . and .. Also look up scabdir() function
You need to maintain counter here to do the same.
Do like this:
$dir = "/images/";
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
break;
}
closedir($dh);
}
}
Let me know for more help!

Making download links out of all of the files in a particular folder

Right now there is an upload system on the site I am working on where users can upload some documents to a particular file. Later on I will need to make these documents downloadable. Is there an easy way to iterate through all the files in a particular directory and create download links for the files?
Something like:
foreach($file){
echo 'somefilename';
}
Many thanks in advance.
if($dh = opendir('path/to/directory')) {
while(($file = readdir($dh)) !== false) {
if($file == "." || $file == "..") { continue; }
echo '' . $file . '';
}
closedir($dh);
}
You should see opendir.
Example from that page adapted to question:
$dir = "/etc/php5/";
$path = "/webpath";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "$file";
}
closedir($dh);
}
}

Categories