I'm trying to write some PHP that runs through a folder grabbing each sub directory name and assigning it to a variable. Then, open a URL with that variable.
For example, D:Folder contains a number of sub folders named 1-??.
The PHP would first open www.url.com/run_batch.php?q=1 and sleep for 30 seconds, then www.url.com/run_batch.php?q=2, etc... for each sub directory in the main directory.
I'm currently in the process of trying to write this. I don't have much code yet, but thought one of you geniuses could help me speed up this process.
UPDATED
Ok, here is what I have so far, it runs without any errors, but it appears to be running all of them at once without sleeping? Not sure, the page just stays busy.
<?php
if ($handle = opendir('D:\HTTP\pic\')) {
$blacklist = array('.', '..', 'bu');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "<iframe width='800' height='600' src='http://www.url.com/run_batch.php?q=" . "$file" . "'></iframe>";
sleep(100);
}
}
closedir($handle);
}
?>
When you make a sleep in PHP code, the HTML is not sent to the browser, that is why it looks busy.
You have to call flush() on each pass.
<?php
if ($handle = opendir('D:\HTTP\pic\')) {
$blacklist = array('.', '..', 'bu');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "<iframe width='800' height='600' src='http://www.url.com/run_batch.php?q=" . "$file" . "'></iframe>";
//Send content to browser
flush();
sleep(100);
}
}
closedir($handle);
}
?>
I suggest you start with pseudo code; create comments for the steps. From there, look at PHP.net for help with specific tasks. When you have something workable but buggy, paste your code.
Pseudocode:
// get directory list
// loop through directories
// ...
// redirect to next page
Related
So. I'm trying to make a simple PHP program that will read the contents of a directory. I've been working off W3Schools. And it's been working well, except for one small problem.
When this script runs, it posts two additional filen that don't exist - even if the directory is completely empty .
<?php
$dir = "./userphotos/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
print <<< HERE
<p>Filename: $file</p>
HERE;
}
closedir($dh);
}
}
?>
Any thoughts?
On Unix machines, each directory contains 2 hidden files.
. and .. these are references to the current and parent directories.
You should look into DirectoryInterator class
$dir = "./userphotos/";
foreach (new DirectoryIterator($dir) as $fileInfo) {
if($fileInfo->isDot() === false) {
echo $fileInfo->getFilename() . "<br>\n";
}
}
This example ignores the "dot" files
Also, you can look into RecursiveDirectoryIterator to do this recursively.
So I'm going through reading and writing to files in PHP via PHP Docs and there's an example I didn't quite understand:
http://php.net/manual/en/function.readdir.php
if toward the end it shows an example like this:
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
?>
In what case would . or .. ever be read?
The readdir API call iterates over all of the directories. So assuming you loop over the current directory (denoted by ".") then you get into an endless loop. Also, iterating over the parent directory (denoted by "..") is avoided to restrict the list to the current directory and beneath.
Hope that helps.
If you want to read directories using PHP, I would recommend you use the scandir function. Below is a demonstration of scandir
$path = __DIR__.'/images';
$contents = scandir($path);
foreach($contents as $current){
if($current === '.' || $current === '..') continue ;
if(is_dir("$path/$current")){
echo 'I am a directory';
} elseif($path[0] == '.'){
echo "I am a file with a name starting with dot";
} else {
echo 'I am a file';
}
}
Because in a UNIX filesystem, . and .. are like signposts, as far as I know. Certainly to this PHP function, anyway.
Keep them in there, you'll get some weird results (like endless loops, etc.) otherwise!
In *nix . is the present working directory and .. is the directory parent. However any file or directory preceded by a '.' is considered hidden so I prefer something like the following:
...
if ($entry[0] !== '.') {
echo "$entry\n";
}
...
This ensures that you don't parse "up" the directory tree, that you don't endlessly loop the present directory, and that any hidden files/folders are ignored.
I use php files in my website and in those file I include my html to show whatever i want the user to view. However i've ran into a problem. I need to show a list of available downloads from a specific folder from my website. The uploading downloadables from my website works. The reading files from directory works, this is done using php code. Now the thing is how can I show this in my html, how to I show this to my user in a fashion that i like most, like for example how Dropbox shows their file listing something like that.
The thing is to pass those files found within the PHP file and pass them to the html to be able to work with them however I want.
I hope i am clear, in case please just tell me so I can elaborate more.
Thanks.
Some code as requested, this is how I am supposedly extracting my files from my website's directory...
Ahh something like this, i get the idea, but here is my problem...
my code looks like this...
$directory_mine;
if ($directory_mine = opendir('/path/to/files')) {
//This is for testing.
echo "Directory: ". $directory_mine . "\n";
echo "Entries:\n";
while (false !== ($entry = readdir($directory_mine))) {
//should be writing each file name into the html here. at least thats my thinking.
}
closedir($directory_mine);
}
include("overall_header.html");
include("mobiledownloadview.html");
include("overall_footer.html");
See here is the problem, how can i add the data extracted by my php file to the mobiledownloadview.html???
I believe this is one way to do it, but if this is terrible please tell me. is there a better way to acomplish my goal?
From the manual on readdir:
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
/* This is the WRONG way to loop over the directory. */
while ($entry = readdir($handle)) {
echo "$entry\n";
}
closedir($handle);
}
?>
If you want to style it, wrap it in an (un-) ordered list and use css for that.
while ($entry = readdir($directory_mine)) !== false) {
echo "Entry: $entry ; filetype: " . filetype($directory_mine . $entry) . "\n";
}
If you're using PHP 5.3+, the PHP SPL class FilesystemIterator could be useful in this case, it easily allows iterating over a certain path and retrieving files as objects, including metadata.
http://www.php.net/manual/en/class.filesystemiterator.php
Example:
<?php
$it = new FilesystemIterator($directory_mine);
echo '<ul class="file_list">'
foreach ($it as $fileinfo) {
echo '<li>' . $fileinfo->getFilename() . '</li>' . PHP_EOL;
}
echo '</ul>'
You can add CSS to the page to style the unordered list how you want it to appear.
Every 72 hours I upload a new PHP file to my server. (well actually it is an xml file transformed on the server with php) Is there a method to create a link on an html page that links to the "new" PHP doc automatically everytime a new file is uploaded?
I don't want to manually change the link every 72 hours. I would ultimately like to have an html page with a list of links to every new doc that is uploaded. I found this for images but I need someting like this but for PHP files and links.
http://net.tutsplus.com/articles/news/scanning-folders-with-php/
Any help would be very appreciated.
I found a solution that add links to the xml files. Now I just need to figure out how to add a link to reference the xslt sheet for each new xml file that is upload AUTOMATICALLY. I am not sure how to do this but any help would be very helpful. Thanks for everyones help.
<?php
$count = 0;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {$count++;
print("".$file."<br />\n");
}
}
echo '<br /><br />Return';
closedir($handle);
}
?>
To read in a directory of files and then sort them by upload time you can just use:
$files = glob("files/*.xml");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
print "link: " . current($files); // make that an actual <a href=
You can do that pretty easily with PHP function readdir:
http://php.net/manual/en/function.readdir.php
Simply loop through the files in the directory where you upload files and have php output a link for each.
ie:
<?php
if ($handle = opendir('/path/to/upload_dir')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo '' . $file . '<br />';
}
}
closedir($handle);
}
?>
You'll need to edit the http:// URL on the href to point to the correct download URL for your server, as well as the server path for opendir.
Hope that helps.
list by filetype
<?php
if ($handle = opendir('/path/to/dir')) {
while (false !== ($file = readdir($handle))) {
if (strpos($file, '.php',1)||strpos($file, '.xml',1) ) {
echo "<p>$file</p>";
}
}
closedir($handle);
}
I have created this php script which displays the contents of a designated directory and allows users to download each file. Here is the code:
<?php
if ($handle = opendir('test')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href='test/$file'>$file\n</a><br/>";
}
}
closedir($handle);
}
?>
This script also displays folders, but when I click a folder, it does display the contents of the folder, but in the default Apache autoindex view.
What I would like the script to do when a folder is clicked, is display the contents, but in the same fashion as the original script does (as this is more editable with css and the like).
Would you know how to achieve this?
Don't create a link to the directory itself, but to a php page which displays the contents.
Change your php code to somthing like:
if(isset($_REQUEST['dir'])) {
$current_dir = $_REQUEST['dir'];
} else {
$current_dir = 'test';
}
if ($handle = opendir($current_dir)) {
while (false !== ($file_or_dir = readdir($handle))) {
if(in_array($file_or_dir, array('.', '..'))) continue;
$path = $current_dir.'/'.$file_or_dir;
if(is_file($path)) {
echo ''.$file_or_dir."\n<br/>";
} else {
echo ''.$file_or_dir."\n<br/>";
}
}
closedir($handle);
}
PS write you html code with double quotes.
You need your HREF to point back to your PHP script, and not the directory. You will then need to update your PHP script to now which directory it needs to read.