PHP Scandir Cloud Folder - php

I Would like to know how i could do a simple scandir of a folder in a cloud storage, or if it's even possible.
<?php
$dir = "C:/wamp64/www/test";
$a = scandir($dir);
$files = array_diff($a, array('.', '..'));
file_put_contents("C:/wamp64/www/test.txt", "");
foreach ($files as $key) {
file_put_contents("C:/wamp64/www/test.txt","$key \r\n", FILE_APPEND);
}
?>
I have this code currently working fine, but my cloud path is L:\2017 and putting those values will just say No such file or directory
Any help appreciated, and sorry for the inconvenience!

You should do something like this:
<?php
$path = '\\2017\somepath\';
$user = "username";
$pass = "password";
$drive_letter = "L";
system("net use ".$drive_letter.": \"".$path."\" ".$pass." /user:".$user." /persistent:no>nul 2>&1");
$location = $drive_letter.":/somepath/your_directory";
if ($handle = opendir($location)) {
while (false !== ($entry = readdir($handle))) {
echo "$entry";
}
closedir($handle);
}
?>

Related

PHP: read contents from a file and echo the name of the folder that file belongs to

There are a directory full of random folders and inside all those folders is name.txt
Example: abcd/name.txt, 1234/name.txt, xyz/name.txt
<?php $users_input = "Jonathan"; ?>
I want to check if $users_input is equal to the contents of any random folder's name.txt and echo THAT folder's name.
Like (if $users_input is equal to contents of 1234/name.text) {echo folder's name which is 1234}
You can use a RecursiveDirectoryIterator to achieve this, as follows:
$users_input = 'Jonathan';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/target/path'));
foreach ($iterator as $file)
{
if ($file->isDir()) {
continue;
}
$path = $file->getPathname();
$contents = file_get_contents($path);
if (strcmp($contents,$users_input)) {
echo $path.'<br\>';
}
}
You may want to include an additional check on the file name if the folders can contain other files with a different name and or extension.
Here's how the solution will look like based on Barmar's suggestion:
<?php
$users_input = "Jonathan";
$file_arr = glob("*/name.txt");
foreach ($file_arr as $path) {
$content = file_get_contents($path);
if (trim($content) === $users_input) {
echo "dirname: " . dirname($path) . "\n";
}
}
Thanks everyone.
Concluded with
$users_input = "Jonathan";
$directory = glob("*");
foreach ($directory as $path) {
if (is_dir($path)) {
$myfile = fopen($path."/name.txt", "r") or die("Unable to open file!");
if (fread($myfile,filesize($path."/name.txt")) == $users_input) {echo $users_input . " is in " . $path;}
fclose($myfile);
}
}

Php Show Files after listing directory (Part 2)

This is a part two to my previous question that got answered Php Browsing Multiple Directories
Now that I got the script reading the directories perfectly, Is it possible to load the results of the file listings into a new page like races.php to "pretty it up"
Here is how it's listing the files Example of Files being listed from server
I would really like to have that be displayed inside of the site instead of jumping out into a apache server listing so its more user friendly.
EDIT
I think what I'm trying to do in theory that is once the script scans the dir it puts it into an array called $files I then foreach loop it for all of the folders but where I get stuck now is how do I pass that $file into a new page ? and make it show the contents inside the folder :)
Thanks again and sorry super newbie here trying to learn!
Script File to generate list and click to files inside of each:
<?php
$files = array();
$dir = opendir('races/ob/');
// $dir = opendir('races/ob/');
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != "..") and ($file != "index.php")) {
$files[] = $file; // put in array.
}
}
natsort($files); // sort.
// print.
foreach($files as $file) {
echo("<span class='txt-spacing'><a href='races/ob/$file'>$file</a> <br />\n</>");
}
?>
The simple function can do all for you
scandir(<path>);
It will give you list of all files in the directory.
I figured out my problem with some help from a friend. I hope others can use this to help out the community.
1) First thing is to list A directory of files on the index.php
2) Once the user clicks the generated folder, go into races.php and display the the results of the files listed inside the folder clicked.
Here is how it's done by passing a parameter in the URL
index.php
<?php
$files = array();
$dir = opendir('races/ob/');
// $dir = opendir('races/ob/');
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != "..") and ($file != "index.php")) {
$files[] = $file; // put in array.
}
}
natsort($files); // sort.
// print.
foreach($files as $file) {
$url = "races/ob/$file";
$path = urlencode($url);
echo("<span class='txt-spacing'>
<a href='races.php?race=$path'>$file</a> <br />\n</>");
}
?>
races.php
<?php
$path = $_GET['race'];
// right here, you need the path prefix
$path = '/public_html' . urldecode($path); //SERVER PATH
// above here you need it
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
$fileData = array();
foreach($objects as $name => $object){
$fileinfo = pathinfo($name);
if (!is_dir($name) && isset($fileinfo['extension'])) {
$file = $fileinfo['basename'];
$fileData[] = $file;
}
}
?>
<?php foreach($fileData as $file): ?>
<a target = '_blank' href="http://crpu.ca<?php echo urldecode($_GET['race']) . '/' . $file; ?>"><?php echo "$file<br>"; ?></a>
<?php endforeach; ?>

How to grab last modified file with certain keyword on page

I have a snippet of code here that grabs the last modified file from a directory. However, I want it to exclude any file that has the word "placeholder" written on it. How do I modify this so that it does exclude those files, especially since the file that has "placeholder" written on its page is usually the last modified file?
Here is the code that I have:
<?php
$dir = "path goes here";
$pattern = '\.(php)$'; // check only file with these ext.
$newstamp = 0;
$newname = "";
if ($handle = opendir($dir)) {
while (false !== ($fname = readdir($handle))) {
// Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fname)) continue;
// Eliminate other pages not in pattern
if (! ereg($pattern,$fname)) continue;
$timedat = filemtime("$dir/$fname");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fname;
}
}
}
closedir ($handle);
$name = basename($newname,".php");
echo "<b><span class='latestch'>Latest Chapter:</span></b> $name";
?>
To skip over files which contain the string "placeholder":
if(strpos(file_get_contents($fname), 'placeholder') !== false) continue;
You could save a lot of code:
array_multisort(
array_map('filemtime',
$files = preg_grep('/placeholder/',
glob("$dir/*.php"), PREG_GREP_INVERT)),
SORT_ASC, $files);
$name = basename(end($files), '.php');

PHP- Count files on a server that connected with FTP

How to count number of files on a remote server that connected with FTP?
This is my code but it doesn't work
<?php
#$ftp = ftp_connect("host");
#ftp_login($ftp, "usr", "pwd");
ftp_chdir($ftp,'uploads/');
echo count(glob(ftp_pwd($ftp) . '*'));
?>
Thanks!
Try to use count() and ftp_nlist() functions combination:
$ftp = ftp_connect("host");
ftp_login($ftp, "usr", "pwd");
echo count(ftp_nlist($ftp, 'uploads/'));
ftp_close($ftp);
use ftp_rawlist :
$files = ftp_rawlist($ftp, '/');
echo count($files).' files ..';
instead of
echo count(glob(ftp_pwd($ftp) . '*'));
Try something like this
<?php
#$ftp = ftp_connect("host");
#ftp_login($ftp, "usr", "pwd");
//ftp_chdir($ftp,'uploads/');
//echo count(glob(ftp_pwd($ftp) . '*'));
if ($handle = opendir(ftp_chdir($ftp,'uploads/'))) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
echo "Total number of files:$i";

Display all file with prefix id

I have user folder /folder_name and there are files with name prefix
Example this pattern id_radom.example
5__1490952185fed525d92.24525311.jpg
15__4658521860030a66d0.90328377.jpg
15__6654521861778060e1.31100475.jpg
15__6654521861778060e1.31100475.jpg
I want to display all of these image(id=15) using php
I am trying:
$path = "uploads/registered_files/".$_SESSION['user_data']['username'];
$a = glob("/".$path."/".$article->article_id."__",GLOB_BRACE);
print_r($a);
But I got empty array()
Solution may look like this:
if (false === ($handle = opendir($path))) {
//catch error here
}
$images = array();
while (false !== ($file = readdir($handle))) {
preg_match('/^15__.*/', $file)) and $images[] = $file;
}
closedir($handle);
foreach ($images as $image) {
echo '<img src="',$path,DIRECTORY_SEPARATOR,$image,'"/>';
}
/".$path."/".$article->article_id."__" is pointing to the root of the filesystem, not to the root of your website. This might be the problem.
Try removing the first / or prefix it with the absolute path to your website's root.

Categories