I have a sub directory called "logs" where I store files for download. I'm trying to create a dynamic download page.
There are 5 raids that are referenced (dragonsoul, firelands, tier11, tier14 and ulduar) and the files in the log folder and named by the format ex. dragonsoul07072013.csv (where the end of the name is a date).
The download page:
I have a form with a select list for each of the raids that submits to itself.
<?php
$raidref=$_POST['raid'];
function getfilename($filedate)
{
return $filedate[1];
}
$path = realpath('logs');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file)
{
if (isset($raidref) and $raidref!="")
{
$filedate=explode($raidref,$file);
$filedatestr=getfilename($filedate);
$filename=$raidref.$filedatestr;
if ($filedatestr=="")
{unset($filename);}
if (isset($filename))
{ echo"$filename<br /><br /><input type=\"button\" value=\"Download\" onClick=\"download('../logs/".$filename."')\"><br /><br />";}
}
}
This code works as is but I want to be able to sort the files found so that the most recent file is on top.
The way the code works now is you select a raid from the drop down, submit and it will display the name of the file and a download button and it will repeat these for each file found that includes the name of the raid in the file.
Seeing as the file name includes dates in it, it'd be easier for people to get to the right download if the most recent link is at the top.
How should I go about printing my results sorted descending?
How it currently displays:
dragonsoul07072013.csv
Download
dragonsoul07142013.csv
Download
dragonsoul07212013.csv
Download
How I'd like it to display:
dragonsoul07212013.csv
Download
dragonsoul07142013.csv
Download
dragonsoul07072013.csv
Download
Also as a note, please explain your answers. I'm learning php from trial and error and research as I need to do things so your explanations will help a lot so I can figure this out on my own in the future (I actually hate to ask for help but I just don't have a clue of how to even approach this).
Seeing as I'm new to the website, I can't answer my own question for another 8 hours but I just wanted to let you guys know your answers helped. This is my revised code, which seems to be doing the trick!
<?php
$raidref=$_POST['raid'];
function getfilename($filedate) {return $filedate[1];}
$files=array();
$path = realpath('logs');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file)
{
if (isset($raidref) and $raidref!="")
{
$filedate=explode($raidref,$file);
$filedatestr=getfilename($filedate);
$filename=$raidref.$filedatestr;
if ($filedatestr=="") {unset($filename);}
if (isset($filename)) {$files[]=$filename;}
}
}
arsort($files);
foreach ($files as $files)
{
echo"$files<br /><br /><input type=\"button\" value=\"Download\" onClick=\"download('../logs/".$files."')\"><br /><br />";
}
?>
Put the files into an array.
Use the sort() function.
Loop through array.
Related
I came up with this questioin. My background is from the Node.js. I am not usually quite used to be in PHP. That's why I'm aksing this question to solve the current issues.
The issues is that's to says I have certain Files and Folders that contains with a specific letters and number at the beginning. As you can see given by down below with a scrrenshot.
I just learn and writing some php code that I grabbed it from the internet resources. Take a look at what's my code:
I want this to detele this all folders which contains letters "exp_" and all the files names start with this numbers "xx-xx-xx" etc.
I created delete.php. When I'd called this file via the browser, I want to achieve to delete all the files and folder which for the No. 1 case.
All these folders and files are generated in everdays. That's why I do want to clean all those data.
<?php
$path = "test";
if(!unlink($path)){
echo "File has not deleted yet.";
} else {
echo "Successfully deleted!";
}
?>
Is there any how any solution to solve this issues? I will appreciate all in advanced who are giving me idea and suggestions from you guys.
You can do something like this:
$files = new DirectoryIterator(__DIR__);
foreach ($files as $file) {
if ( ($file->isDir() and strpos($file->getFilename(),'exp_')!==false) || ($file->isFile() and $file->getFilename() == date('d-m-Y') ) ) {
unlink($file->getPathname());
}
}
Thus, all folders with names like exp_ and files with today's date will be deleted.
sorry if this has been asked already, couldn't find anything on it.
I need a code that will force how certain filetypes open.
For example, I have an Apache directory listing that displays a bunch of .mp4 files. I have a custom template set up with it, and I have an iframe on the page.
What I want is so that only .mp4 files open in the iframe, but so that all other extensions open normally.
I tried using the simple: base target="iframe_content"
but that will make EVERY link open in the iframe.
Specifying: target="_parent"
on all my navigation urls is not an option, because I need people to be able to navigate through folders within the listing as well, and open other filetypes in the listing normally.
I'm thinking I need some sort of If/Else statement, but I can't figure out how to do it.
Sorry if this is a fairly obvious answer, I'm somewhat newb-ish at PHP.
Thanks
You just need to iterate through your files, and check, is the extension is mp4 or not. If yes, then open it in the iframe, if no, then open it normally:
$dir = '.'; //The dir what you want to list
$dirContent = scandir($dir);
foreach ($dirContent as $entry) {
if (!in_array($entry, array('.', '..')) && !is_dir($entry)) {
$pathInfo = pathinfo($entry);
if ($pathInfo['extension'] == 'mp4') {
//Open in iframe where the id of iframe is: myIframe
?>
<?php echo $entry; ?><br />
<?php
} else {
//Open normally
?>
<?php echo $entry; ?><br />
<?php
}
}
}
I have been using a snipit I found online to display the files and folders in a directory with my PHP script. This part works fine, but I would like to be able to click the folders and get a similar html page up that displays it's contents, and beeing able to open the text files in the browser(this works fine if the text file is in the start directory).
At the moment, when I click a folder, an old school sort of page opens up with no html file in it. Is the only way to do this to create a new PHP script in each folder and link to it? I tried using the DirectoryIterator class, but it gave me an error. I dont have the snipit for DirectoryIterator anymore, but it was something like "Can't find class DirectoryIterator".
Here's the code I'm using now(working):
$arrayImports = array ();
if ($handle = opendir ($importLogPath)
{
while (false !== ($entry = readdir ()))
{
chop ($entry);
if ($entry != "." $entry != "..")
{
$arrayImports [] = "<p><a href=$importLogPath$entry target=_blank>$entry</a></p>";
}
}
closedir($handle);
}
arsort ($arrayImports);
foreach ($arrayImports as $value)
{
print "<li>$value</li>";
}
Thanks!
Are you looking for something like this?
Encode Explorer
I used it for an old project... it's very cool. You can also add ajax to browse folders without refreshing the pages.
Bye
I'm not a developer, but I'm the default developer at work now. : ) Over the last few weeks I've found a lot of my answers here and at other sites, but this latest problem has me confused beyond belief. I KNOW it's a simple answer, but I'm not asking Google the right questions.
First... I have to use text files, as I don't have access to a database (things are locked down TIGHT where I work).
Anyway, I need to look into a directory for text files stored there, open each file and display a small amount of text, while making sure the text I display is sorted by the file name.
I'm CLOSE, I know it... I finally managed to figure out sorting, and I know how to read into a directory and display the contents of the files, but I'm having a heck of a time merging those two concepts together.
Can anyone provide a bit of help? With the script as it is now, I echo the sorted file names with no problem. My line of code that I thought would read the contents of a file and then display it is only echoing the line breaks, but not the contents of the files. This is the code I've got so far - it's just test code so I can get the functionality working.
<?php
$dirFiles = array();
if ($handle = opendir('./event-titles')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$dirFiles[] = $file;
}
}
closedir($handle);
}
sort($dirFiles);
foreach($dirFiles as $file)
{
$fileContents = file_get_contents($file);//////// This is what's not working
echo $file."<br>".$fileContents."<br/><br/>";
}
?>
Help? : )
Dave
$files = scandir('./event-titles') will return an array of filenames in filename-sorted order. You can then do
foreach($files as $file)
{
$fileContents = file_get_contents('./event-titles/'.$file);
echo $file."<br/>".$fileContents."<br/><br/>";
}
Note that I use the directory name in the file_get_contents call, as the filename by itself will cause file_get_contents to look in the current directory, not the directory you were specifying in scandir.
This is a tricky one and I'm not sure where to start, so any help will be grateful.
I have a parent folder called 'source' (c:/dev/source) which contains several child folders.
I need a PHP script that will display the child folders with checkboxes next to each, and a text field for a new folder name, allowing users to tick the checkboxes of the ones they want to copy to a 'destination of c:/dev/destination/the_folder_name_they_typed_in
When they click submit, the selected child folders will be copied from c:/dev/source to c:/dev/destination/the_folder_name_they_typed_in
This is all running on a local internal development server. The child folders will always be in c:/dev/source/
Somme advice:
Use a whitelist for allowed characters in destination folder. Only commit the operation if it matches:
^[a-z0-9_-]+$
You can use array indices for directory names. This way you can iterate thru the ckeckboxes with foreach ($_POST["dirs"]) { ... }
<input type="checkbox" name="dirs[directory_name]"/>
<input type="checkbox" name="dirs[other_dir_name]"/>
<input type="checkbox" name="dirs[third_directory_name]"/>
Always checkthe directory names against a whitelist like above. (If you allow characters like . or / or many other it can be a security risk).
Here's a not well known little bit of code called DirectoryIterator. It's not fully documented on the PHP site, but heres the jist of it:
Create a list of files and folders with checkboxes next to them, slap them all in an array.
$Directory = new RecursiveDirectoryIterator('c:/dev/source');
$Iterator = new RecursiveIteratorIterator($Directory);
?><form method="post"><?
foreach($Iterator as $r){
if($r->isDot()) continue;
echo "<input type=\"checkbox\" name=\"copy[]\" value=\"".($r->getSubPathName())."\"> ".$r->getSubPathName() . " <br>";
}
?></form><?
Now add this part to the top of the file
<?php
if($_POST){
if(is_array($_POST['copy'])) foreach($_POST['copy'] as $c){
#copy($c, str_replace('c:/dev/source','c:/dev/dest', $c));
echo "copied: $c to ". str_replace('c:/dev/source','c:/dev/dest', $c) . "<br>";
}
}
I'm not fully sure what result you get from $r->getSubPathName() can you let me know if it outputs an array? if so it might be that you replace that with $r->getSubPath() and then add the "c:/dev/source" to the variable $c when you copy it?
Further Reading:
here