Php access folder from file - php

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';

Related

How to read only the folders

I have this code that reads all the content of a folder and converts it to an array, but I want to only the code reads folders and no files.
For example in language file are these files and folders:
../
en_EN/
fr_FR/
happy.rar
And the code is:
$folder = '../language/';
$return = scandir($folder, 1);
$return = array_diff($return, array('.', '..','error_log','_notes'));
$return = str_replace(".php", "", $return);
I can make exceptions in the 3rd line, but I want to create an exception for all the files.
Is there a way to make that?
Thank you
You can use glob() to select only folders simply as per below with GLOB_ONLYDIR flag.
and use basename() to get only folder name.
$a = glob("../language/*", GLOB_ONLYDIR ); // $a has only folders
foreach($a as $file){
echo(basename($file));
}

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: Find directory by matching a pattern

I have a small problem: I want to show images from directory, what I successfully programmed, but just from directory which has a static name. How can I add here some filter, or something like that, when I send in variable osc number 31322357 it will automatically find directory 31322357 - Automatic Board and show images? Thanks.
Here is my script:
<?
//path to directory to scan
$osc=$_GET['osc'];
$directory="./$osc/";
//get all image files with a .jpg extension.
$images = glob("$directory{*.jpg,*.JPG,*.png}", GLOB_BRACE);
//print each file name
foreach($images as $image)
{
echo "<img src='$image' style='width:100px;height:auto'>";
}
?>
<?php
...
$dirs = glob("./$osc*",GLOB_ONLYDIR);
$images = array();
if(count($dirs))
foreach($dirs as $dir){
$images = array_merge($images,glob("$dir/{*.jpg,*.JPG,*.png}", GLOB_BRACE));
}
?>
Use a wildcard * in your directory name:
$directory = "./$osc*/";
$images = glob("$directory*.{jpg,JPG,png}", GLOB_BRACE);
Note that this will match 31322357 - Automatic Board and 31322357 - Other stuff, etc. and list files from all matching directories.

Changing Base Path In PHP

I need to change the folder that "relative include paths" are based on.
I might currently be "in" this folder:
C:\ABC\XYZ\123\ZZZ
And in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\ABC\XYZ\Source
And realpath('.') would = 'C:\ABC\XYZ\123\ZZZ';
If however, realpath('.') were "C:\Some\Other\Folder"
Then in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\Some\Source
How do I change what folder is represented by '.' in realpath()?
Like this:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
// Some PHP code here...
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
How can I change the folder represented by '.', as seen by realpath()?
The function chdir() does this.
For example:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
chdir('C:/Some/Other/Folder');
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
Use the chdir() function.
Change your current working directory with chdir()
http://us.php.net/chdir

PHP: Last file in directory

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);

Categories