Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am moving images from xcode to an online platform and i need to generate a JSON file from a directory structure (images only) that lists the folders, and the images in those folders, with complete URL (online) AND date they where added.
I am a COMPLETE noob with PHP web stuff, so am really lost at the moment.
I found the following code, but that does not do anything so far, and does not travels into directory's.
<?php
/*
JSON list of all images files in current directory
*/
$dir = ".";
$dh = opendir($dir);
$image_files = array();
while (($file = readdir($dh)) !== false) {
$match = preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file);
if ($match) {
$image_files []= $file;
}
}
echo json_encode($image_files);
closedir($dh);
?>
The following code works for me. Found it here.
I've only modified the echo, added the regex and the time. I hope this answers your question.
<?php
function getDirContents($path) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = array();
foreach ($rii as $file) {
if (!$file->isDir() && preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file)) {
$files[] = [
'path' => $file->getPathname(),
'c_time' => $file->getCTime()
];
}
}
return $files;
}
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'files' => getDirContents('.')
]);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 18 days ago.
Improve this question
I have 100 html files, and i want to add a line of code (at the end) to all of them.
How is this possible ?
Something like a script which appends that line into all html files
I tried searching but did not find anything.
You can use :
<?php
$folder = '/path/to/html/files';
$line_to_add = '<p>your line/p>';
foreach (glob("$folder/*.html") as $file) {
$contents = file_get_contents($file);
file_put_contents($file, $line_to_add . PHP_EOL . $contents);
}
Your question is quite unclear, because you didn't specify where you are going to append this lane? Here is a starting point:
<?php
$path = '/path/to/html/files';
$code_to_add = '<p>This line was added by a script.</p>';
$files = scandir($path); // get an array of all the files in the directory specified by $path.
foreach ($files as $file) { // Iterate over files
if (substr($file, -5) == '.html') { // hecking if each file has a ".html" extension.
$file_path = $path . '/' . $file;
$file_contents = file_get_contents($file_path); // read the contents
file_put_contents($file_path, $file_contents . $code_to_add); // put new content, you can modify it.
}
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've my .ini file as follows
languages.ini
--------------------
fr = "French"
en = "English"
Now, I'd like to read the file and keep the keys and values in the form of an array using PHP. I'm very new to PHP. I hope any one help me in achieving this. This is my following code.
public function GetLanguages()
{
$langdir = ISC_BASE_PATH.'/language';
$skip = Array (
'.',
'..',
'CVS',
'.svn',
);
$langs1 = array();
$dh = opendir($langdir);
while (($file = readdir($dh)) !== false) {
if (!is_file($langdir.'/'.$file.'/languages.ini')) {
continue;
}
$langs1[] = $file;
}
echo "FileLL:".$file;
foreach ($langs1 as $key) {
echo "Store languagesss::".$key."<br>";
}
return $langs1;
}
Thank you in advance.
You are looking for PHP's ini parse function:
parse_ini_file
Just call $arrayResult = parse_ini_file($path_to_file)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to find the oldest file in a directory, oldest file in all (including, directories, subdirectories of a particular folder)
Folder 1
- dir 1
- dir 1.1
- file 1
- dir 2
- dir 2.1
- file 2
if file 2 is oldest , I need to able to get oldest file (file 2) by passing main directory name (Folder 1)
my solutions is
function get_oldest_file($dir) {
$filemdate = array();
print $dir.PHP_EOL;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$files[] = $file;
print $file.PHP_EOL;
}
foreach ($files as $eachfile) {
if (is_file($dir.eachfile)) {
$file_date[$eachfile] = filemtime($dir.$eachfile);
print $filemdate[$eachfile].PHP_EOL;
}
}
}
closedir($handle);
asort($filemdate, SORT_NUMERIC);
reset($filmdate);
$oldest = key($filemdate);
print "Oldest is : ".$oldest;
return $oldest;
}
echo get_oldest_file("/path/---")
Thanks !
What about?
function workerFunction($currentDir, $oldestFile)
foreach (glob($currentDir.'/*') as $file) {
if (is_dir($file)) {
if ((basename($file)!='.') && (basename($file)!='..')) {
$oldestFile = workerFunction($file, $oldestFile);
}
} else {
$mtime = filemtime($file);
if ($mtime <= $oldestFile['mtime']) {
$oldestFile['mtime'] = $mtime;
$oldestFile['path'] = $file;
}
}
}
return $oldestFile;
}
function searchForOldestFile($dir) {
$oldestFile['mtime'] = time();
$oldestFile['path'] = null;
$oldestFile = workerFunction($dir, $oldestFile);
return $olderstFile['path'];
}
I don't have the PHP environment for debugging, but at least with some little fixes it could work as you need.
Let me explain, so you would be able to use it easily:
searchForOldestFile function is the interface that your script should call; workerFunction does the "magic" (if it work :)), keeping in $oldestFile a reference to the current oldestFile.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a search feature on my website. I would like it to search through a certain folder for files on my server and display results from there. I'd rather not use databases.
Is there a way to do this?
<?php
$dir = "/your_folder_here/";
// 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['SEARCHBOX_INPUT']){
echo(''. $file .''."\n");
}
}
closedir($dh);
}
}
?>
From php.net mostly. Obviously change your file path. Also change the $_POST command in the middle to whatever the name of your search box input is. This will only find exact matches of your search. Some changes could be made to find close matches.
$dir = "/etc/php5/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if($file == 'songs1.php')
//ur code here ...
}
closedir($dh);
}
}
I got this from PHP.net. I hope this helps you.
This is how I did it, although it displays all the files which are in that directory and it does not give a brief description of each file. I dont know if you can help modify it.
<?php
print "<h2>Showing results for $search</h2>";
$dirName="MYBOOKS";
$dp=opendir($dirName);
chdir($dirName);
while ($currentFile !== false) {
$currentFile = readDir($dp);
$theFiles[] = $currentFile;
}
$BookFiles= preg_grep("/pdf$|gif$|png$|jpg$|jed$/", $theFiles);
$output="";
foreach ($BookFiles as $currentFile) {
$output .= <<< Here
<ul>
<li><a href=MYBOOKS/$currentFile>$currentFile</a></li>
</ul>
Here;
}
$fp=fopen("BookIndex.htm","w");
fputs ($fp,$output);
fclose($fp);
readfile ("BookIndex.htm");
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am creating a downloadable zip file. In that zip, I want to add php, html, and images and videos.
All of them are in other folders.
I can read and write PHP and HTML files from other files/folders.
But I don't know how to read and write (or move from one folder to anther folder) images and videos.
Please try this code
function move_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != ".." && $file != "Thumbs.db"/*pesky windows, images..*/)
{
$sourcefile = $dir.$file;
$destinationfile = 'Your New Location';
if (!copy($sourcefile, $destinationfile)) {
echo "failed to copy $file...\n";
}
}
}
closedir($handle);
}
}
}
move_files("folder/");
I you want to move files, see PHP manual: copy, unlink