Is it possible to loop through a folder using file_get_html for each file?
I'm currently using the following:
$html = file_get_html('http://example.local/folder/file1.html');
Is it possible to set the path to a folder, then it loops through the folders contents?
$html = file_get_html('http://example.local/folder/');
The files within the folder could be named anything (there's no set naming convention!) but they will always be html files.
I'm using simple_html_dom.php to get the HTML.
This would be the general idea:
$source = '/some/local/folder/';
foreach (new DirectoryIterator($source) as $fileInfo) {
if($fileInfo->isDot()) continue;
$html = file_get_contents($source.$fileInfo->getFilename());
//do stuff with $html
}
Related
I'm trying to make an image gallery that scans a main directory and creates a separate album for each subdirectory.
My structure is similar to this:
-Gallery
--Subdir 1
---Image 1
---Image 2
--Subdir 2
---Image 1
---Image 2
The idea is that each album is going to be made of a div with a class of web-gallery. Then there will be a header for the album title made from the subdirectories name. After that a list is generated of each image. This is going to be a one page gallery. If possible I would like to have a variable that sets how many albums are listed that way if I have 30 subdirectories my page doesn't get too crowded.
So far I've written this but it doesn't work. I'm not getting any errors or logs though it just doesn't work.
$dirs = glob('img/gallery_temp/*', GLOB_ONLYDIR);
foreach($dirs as $val) {
echo '<div class="web-gallery">';
echo "<h3><span>ยป</span> ".basename($val). "</h3>";
echo '<ul class="web-gallery-list">';
$files = glob($val.'*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
echo "<li><a href='".$file."'><img src='" . $file . "' alt='description'></a></li> \r\n";
}
echo "</ul>";
echo "</div>";
}
Simply add a / before *.{jpg,png,gif} like this:
$files = glob($val.'/*.{jpg,png,gif}', GLOB_BRACE);
This is because $val doesn't have a final / for the directory.
You might consider using "readdir" instead of glob. Glob is to find pathnames matching a pattern, see here: http://php.net/manual/en/function.glob.php and is known to be a bit problematic.
Readdir, if your directory is entirely images might be easier to use: http://php.net/manual/en/function.readdir.php
Couple this with is_dir() http://php.net/manual/en/function.is-dir.php to resolve your directories vs files. Here is a snippet
<?php
if ($handle = opendir('/galleries')) {
while (false !== ($entry = readdir($handle))) {
// this is a subdirectory
if (is_dir($entry)) {
}
// this is a file
else {
echo $entry;
}
}
closedir($handle);
}
?>
If you make it a recursive function you could actually have it traverse a number of subdirectories creating galleries within galleries.
I also found this fantastic little snippet that is very elegant on another stack question: Get Images In Directory and Subdirectory With Glob
$rdi = new RecursiveDirectoryIterator("uploads/prevImgs/");
$it = new RecursiveIteratorIterator($rdi);
foreach($it as $oneThing)
if (is_file($oneThing))
echo '<img src="'.$oneThing.'" /><br />';
Using SPL Library (PHP >= 5)
Better solution in your case
is to use SPL library (the most cross-platform)
$directory = new RecursiveDirectoryIterator("./img/gallery_temp", FilesystemIterator::SKIP_DOTS);
// Flatten the recursive iterator, folders come before their files
$it = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
foreach($it as $fileinfo)
{
if($fileinfo->isDir())
{
// prevPath used to separate each directory listing and closing the bracket UL list
$prevPath = $it->getSubPath().DIRECTORY_SEPARATOR.$fileinfo->getFilename();
echo sprintf
(
"<div class='web-gallery'>
<h3><span>></span> %s</h3>
<ul>".PHP_EOL,
$fileinfo->getFilename()
);
}
if($fileinfo->isFile())
{
echo sprintf("<li><a href=''><img src='%s/%s' alt='description'></a></li>".PHP_EOL, $it->getSubPath(), $fileinfo->getFilename());
if($prevPath != $it->getSubPath())
echo("</ul>");
}
}
Note:
For more informations : SPL Documentation
DIRECTORY_SEPARATOR is a cross-platform constant, will use the
correct directory separator of the OS where are executed the code
FilesystemIterator::SKIP_DOTS, avoid to fetch the '.' and '..' dir
link level.
you can limit the depth of scanning with $it->setMaxDepth(5);
I have a html file listing links to artists' pages. What I want to do is to use a php script to list them instead of listing them manually. I also would like to have a thumbnail image above each corresponding link, but I want to get the links first before I add the images. I'm using the following script but it's not working:
<?php
$directory = "C:/wamp/myprojects/UMVA/web/includes/artists";
$phpfiles = glob($directory . "*.html");
foreach($phpfiles as $phpfile)
{
echo ''.$phpfile.'';
}
?>
The folder containing the html files is artists. It doesn't work using the full pathname and it doesn't work using just 'artists' or '/artists' as the pathname. The 'artists' folder is in the same directory 'web' as the php file with the script. What am I missing here?
this should actually do the trick:
$htmlFiles = glob("$directory/*.{html,htm}", GLOB_BRACE);
source
Not sure where is the error, but you can also use SPL Iterators, like GlobIterator, in a more reusable way. GlobIterator returns SplFileInfo objects that provides many useful informations about your file.
Here are the doc pages:
http://fr2.php.net/manual/en/class.globiterator.php
http://fr2.php.net/manual/en/class.splfileinfo.php
Here is an example:
$it = new GlobIterator('C:/wamp/myprojects/UMVA/web/artists/*.jpg');
foreach ($it as $file) {
// I added htmlspecialchars too, never output unsafe data without escape them
echo '' . htmlspecialchars($file->getFilename()) . '';
}
if your directory is always 'C:/wamp/myprojects/UMVA/web/artists', I think you can try scandir( $dirname ) instead of glob().
Here is a simple script that will look for html files in the current directory create and a hyperlink based on the title tag.
<?php
// Get all HTML files in the directory
$html_files = glob("*.{html,htm}", GLOB_BRACE);
$url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Print out each file as a link
foreach($html_files as $file) {
$contents = file_get_contents($file);
$start = strpos($contents, '<title>');
if ($start !== false) {
$end = strpos($contents, '</title>', $start);
$line = substr($contents, $start + 7 , $end - $start - 7);
echo "<center>$line</center><br>\n";
}
}
?>
Save this file as index.php place the html files in the folder and browse to the URL.
I'm trying to create a script that will read all of the other files in the directory and list them by grabbing the title meta tag for each one using the code below, but it's not working. If I'm reading the documentation correctly, get_meta_tags expects a URL by default, and if you want to point to a local file, you need to set the use_include_path parameter. But I don't think I'm doing that correctly.
$dir = '.';
$files = scandir($dir);
set_include_path($dir);
foreach ($files as &$value) {
$tags = get_meta_tags($value, true);
echo $tags['title'] . "<br/>";
}
According to the documentation it takes a URL or a filename-string. The only thing the second parameter is good for is if the files you want to parse are not on the current path, but are on the include path instead. That is not the case here, as you are already iterating over a path to get the filenames. You should do:
$dir = '.';
$files = scandir($dir);
foreach ($files as &$value) {
$tags = get_meta_tags($value);
echo $tags['title'] . "<br/>";
}
i have an application that is used to edit .txt files. the application is made up of 3 parts
Displays contents of a folder with the files to be edited(each file is a link when clicked it opens on edit mode).
writing in to a file.
saving to file.
part 2 and 3 I have completed using fopen and fwrite functions that wasn't too hard. the part that i need help is part one currently I open the file by inputing its location and file name like so in the php file where i have the display function and save function:
$relPath = 'file_to_edit.txt';
$fileHandle = fopen($relPath, 'r') or die("Failed to open file $relPath ! ");
but what i want is for the file to open in edit mode when clicked instead of typing in the files name every time.
$directory = 'folder_name';
if ($handle = opendir($directory. '/')){
echo 'Lookong inside \''.$directory.'\'<br><br>';
while ($file = readdir($handle)) {
if($file !='.' && $file!='..'){
echo '<a href="'.$directory.'/'.$file.'">'.$file.'<a><br>';
}
}
}
this is the code that ti use to display the list of files that are in a specified folder.
Can anyone give me some pointers how I can achieve this ? any help will be greatly appreciated.
To get content of file use file_get_contents();
To put content of file use file_put_contents(); with FILE_APPEND flag for editing.
To recieve list of files in directory you can use DirectoryIterator
Example:
foreach (new DirectoryIterator('PATH/') as $fileInfo) {
if($fileInfo->isDot()) continue;
echo $fileInfo->getFilename() . "<br>\n";
}
If you don't want to put filenames you can put read files once put in db assign ids to them and use links with id param. The other solution is to store files in session array and assign keys for them. When you want to get a file you just need to provide key instead of whole filename and path.
Example with $_SESSION
$file_arr = array();
foreach (new DirectoryIterator('PATH/') as $fileInfo) {
if($fileInfo->isDot()) continue;
$file_arr[] = array("path" => $fileInfo->getPathname(), 'name' => $fileInfo->getFilename());
}
$_SESSION['files'] = $file_arr;
then in view you can use
foreach($_SESSION['files'] as $k=>$file)
{
echo "<a href='edit.php?f=".$k."'>'.$file['name'].'</a>";
}
and edit.php
$file = (int)$_GET['f'];
if(array_key_exits($file, $_SESSION['files'])
{
$fileInfo = $_SESSION[$file'];
//in file info you have now $fileInfo['path'] $fileInfo['name']
}
I have the following code snippet. I'm trying to list all the files in a directory and make them available for users to download. This script works fine with directories that don't have sub-directories, but if I wanted to get the files in a sub-directory, it doesn't work. It only lists the directory name. I'm not sure why the is_dir is failing on me... I'm a bit baffled on that. I'm sure that there is a better way to list all the files recursively, so I'm open to any suggestions!
function getLinks ($folderName, $folderID) {
$fileArray = array();
foreach (new DirectoryIterator(<some base directory> . $folderName) as $file) {
//if its not "." or ".." continue
if (!$file->isDot()) {
if (is_dir($file)) {
$tempArray = getLinks($file . "/", $folderID);
array_merge($fileArray, $tempArray);
} else {
$fileName = $file->getFilename();
$url = getDownloadLink($folderID, $fileName);
$fileArray[] = $url;
}
}
}
Instead of using DirectoryIterator, you can use RecursiveDirectoryIterator, which provides functionality for iterating over a file structure recursively. Example from documentation:
$path = realpath('/etc');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
echo "$name\n";
}
This prints a list of all files and
directories under $path (including
$path ifself). If you want to omit
directories, remove the
RecursiveIteratorIterator::SELF_FIRST
part.
You should use RecursiveDirectoryIterator, but you might also want to consider using the Finder component from Symfony2. It allows for easy on the fly filtering (by size, date, ..), including dirs or files, excluding dirs or dot-files, etc. Look at the docblocks inside the Finder.php file for instructions.