PHP: Last file in directory - php

How do I get the name of the last file (alphabetically) in a directory with php? Thanks.

Using the Directories extension you can do it simply with
$all_files = scandir("/my/path",1);
$last_files = $all_files[0];

The scandir command returns an array with the list of files in a directory. The second parameter specifies the sort order (defaults to ascending, 1 for descending).
<?php
$dir = '/tmp';
$files = scandir($dir, 1);
$last_file = $files[0];
print($last_file);
?>

Code here looks like it would help - would just need to use end($array) to collect the last value in the generated array.

$files = scandir('path/to/dir');
sort($files, SORT_LOCALE_STRING);
array_pop($files);

Related

Php access folder from file

I want to access a folder from a .php file using scandir();!
This is my folder tree:
- public_html:
- lua:
- fld1:
- myfile.php
- works(folder)
The "fld1" directory contains the php file and the "works" folder(the requested one)
The php file and the folder are in the same location!
I tried to access it like this:
<?php
$dir = "works/";
// Sort in ascending order - this is default
$a = scandir($dir);
// Sort in descending order
$b = scandir($dir,1);
print_r($a);
?>
But nothing is printed on the page!
I also tried this:
$dir = "/works/";
or the full path:
$dir = "/public_html/lua/fld1/works/";
Give the path correctly
Try this:
$dir = '/works';
or
$dir = './works';

Wrong result of sizeof

I have several files in a folder and i want to count them.
$folder = "images";
$allPics = scandir($folder);
$result = sizeof($allPics);
echo $result;
The result is 350 but it should be 348. I don't get it why it is showing me the result +2?
Am i missing something?!
http://php.net/manual/en/function.scandir.php
When looking at the documentation you can see the function return both '.' and '..', that's why you're having 2 more than you should have.
You can use this:
array_diff(scandir($folder), array('..', '.'));
To get rid of the dots you don't wanna have.
You are using the unix system and it have, 2 pointers in each directory, the pointer for the parent dirrectory that usualy is notted with .. and the pointer to the current directory that is notted as .

jQuery / PHP - How to get random file in folder

I have my php code as follows:
<?php include("/myfolder/my-file-01.html"); ?>
and in the folder myfolder I have 2 files: my-file-01.html and my-file-02.html
Now, with jQuery or php, how can I randomly include my-file-01.html or my-file-02.html in one refresh my website (F5).
Any Idea?
Thanks
As an alternative, you could also load them inside an array thru scandir, point it into the files path, then use an array_rand:
$path_to_files = 'path/to/myfolder/';
$files = array_diff(scandir($path_to_files), array('.', '..'));
$file = $files[array_rand($files)];
require "$path_to_files/$file";
However, if you have other files other than my-file prefix, it'll get mixed up, so to prevent that from happening, you could use a glob solution instead. This will only search file/s that has that my-file prefix. Example:
$files = glob('myfolder/my-file-*.html');
$file = $files[array_rand($files)];
require $file;
You generate a random number which is 1 or 2 with the rand() function.
<?php
//Create random number 1 or 2:
$random = rand(1,2);
//Add zero before 1 or 2
$random = "0".$random;
//Include random file:
include("/myfolder/my-file-".$random.".html");

php get list of files as array, output 1 random file name with extension.

So what i am trying to do is get a list of text files from a directory.
take that list and randomly choose 1 file.
then take that file and print out the contents. now i did this a few years back. but can't find my old script. i tried what i have below just to print out a file name.. but event that is not working?
$path = '/seg1';
$files = scandir($path);
$seg = array ( $files );
$rand_keys = array_rand($seg, 1);
print $rand_keys;
Would love some new eyes on this as well as any input.
/*** Search All files in Dir. with .txt extension ***/
foreach (glob('./seg1/*.txt') as $filename)
{
$myFiles[] = $filename;
/*** Array of file names **/
}
/*** Total count of files ***/
$max=sizeof($myFiles);
/*** Select a Random index for Array with Max limit ***/
$fileNo=rand(0, $max);
/*** Path of the Random file to Access ***/
$file=$myFiles[$fileNo];
/*** Get the content from Text file ****/
$data = file_get_contents($file, true);
As your variable naming suggests, you randomly select one key out of the array; try getting the value by
$filename = $seg[$rand_keys];
$path = '/seg1';
$files = scandir($path));
if($files)
echo file_get_contents($files[mt_rand(2,count($files))]);
Recommend you use glob so you only get files you want, this excludes system directories like . and ..
foreach (glob("seg1/*.txt") as $filename) {
$seg[] = $filename;
}
Or an even simpler solution:
$rand_keys = array_rand(glob("seg1/*.txt"), 1);

newbie: php how to read in file names in order of filename

I want to "include" the last 2 files in my php file based on their names.
So the names are:
01-blahbla.php
02-sdfsdff.php
03-aaaaaaa.php
04-bbbbbbb.php
05-wwwwwwi.php
and I only want to include 05 and 04 since their name starts with the biggest number.
How do I go on about doing this?
Assuming there is only the numbered files in the folder, you could use
$files = glob('/path/to/files/*.php'); // get all php files in path
natsort($files); // sort in natural ascending order
$highest = array_pop($files); // get last file, e.g. highest number
$second = array_pop($files); // again for second highest number
Put the values in an array, reverse sort it using rsort() and then take the first two:
$values = array('01-blahbla.php', '02-sdfsdff.php', '03-aaaaaaa.php', '04-bbbbbbb.php', '05-wwwwwwi.php');
rsort($values);
$file1 = $values[0];
$file2 = $values[1];
require_once $file1;
require_once $file2;
The PHP manual at php.net has some great info for the various sort methods.
Update:
As Psytronic noted, rsort will not work for numbers, but you can create a custom function that easily does the same thing:
function rnatsort(&$values) {
natsort($values);
return array_reverse($values, true);
}
$files = rnatsort($values);
List the directory contents into an array.
Sort that array using built-in PHP sorting functions.
Do a require_once() on the first two elements of the array.

Categories