How to readdir in ../ on lighttpd? Permissions? - php

I'm trying to read all folder names and create buttons out of it via .php. It somehow doesn't work how I want it to work.
Foldernames to read are in /www/templates/
.php file is in /www/php/
tried to:
put read.php in /www/templates/, telling it to opendir() and readdir() of "./" and include it to /www/php/ -> shown me results (folders) of /www/php/ (where it works perfectly and shows the folders of "./" if it's placed in there aswell).
Tried using the complete path ( /opt/xyz/www/templates/ ) -> no result at all
CHMOD of the folders are 766 - owner is the same as the PHP script.
Script placed in /www/php/:
<?php
$d = opendir('../templates/');
while(false !== ($f = readdir($d))) {
if (is_dir($f) && $f != "." && $f != "..") {
echo "<form action=\"./change.php\" method=\"get\">
<input type=\"submit\" name=\"template\" value=\"" . $f . "\"><br>";
}
}
?>
Script tried in /www/templates/:
<?php
$d = opendir('./');
while(false !== ($f = readdir($d))) {
[insert-code-from-above-here] }
?>
included the script in /www/templates/ to the "index.php" with:
<?php include('../templates/read.php'); ?>
Webserver: lighttpd

Related

How to set the path in opendir() in an included file?

In my application root folder I have a folder named 'articles' in which there are some files. And in my root folder I have the header file and some other files. The same header file is used in the files inside the articles directory too.
In my header file, I have a dropdown menu which lists the files inside the articles directory. And I have used the following code.
<?php
$dir = "./articles";
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != "images" && $entry != "index.php") {
$foo = $entry;
$foo = str_replace("_", " ", $foo);
$foo = str_replace(".php", "", $foo);
$foo = ucwords($foo);
?>
<a class="dropdown-content-a" href="<?php echo "$entry" ?>"><?php echo $foo ?></a>
<?php
}
}
closedir($handle);
}
?>
This works fine in the files in the root folder but it does not work in the files in the 'articles' folder(But the header file is still in the root folder and relative to the header file, the path './articles' is correct).
How to overcome this by using the same header file?
use (..) to cd the previous directory
include_once(dirname(__FILE__).'/../filename.php');
Maybe you can try a condition-wise solution. Check the path and decide to use ".." or not.

delete file called ".html"

I accidentally created a file with no name http://website.com/myFolder/.html,
now, in the control panel of my webhost, this file is not listed, I cannot see or delete it...
but I can see it using this "myList.php" file: (http://website.com/myFolder/myList.php):
<?php
echo "<ol>";
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo '<li>'.$entry.'</li>';
}
}
closedir($handle);
}
echo "</ol>";
?>
This "myList.php" file outputs all the files present in the directory: http://website.com/folder/
also the file with no name http://website.com/myFolder/.html
How can I delete this file?
I tried to create another .php file called http://website.com/myFolder/myDelete.php,
and use the php function unlink():
<?php
$path = "../myFolder/.html";
if(file_exists($path)){
if (is_file($path)){
//unlink($path);
if (!unlink($file)){
echo ("Error deleting".$path);
}else{
echo ("Deleted".$path);
}
}
}
?>
But it doesn't work.
$path = "../myFolder/.html";
if(file_exists($path)){
if (is_file($path)){
//unlink($path);
if (!unlink($file)){
^^^^^----undefined variable
Why all of that when you could just have
unlink('.html');
? Your unwanted file is in the same directory as your myDelete.php script, so the rest of all that is pointless.
Files and directories that begin with . are considered "hidden" on *nix systems. You can see them with ls -la but not with just ls.
Try changing the $file variable to just be the name of the file ".html". Make sure to use the $file variable for the delete - this is not defined in your example.
$file = ".html";
if ( file_exists( $file ) ){
if ( ! unlink( $file ) ){
echo "Error deleting '$file'" );
} else{
echo "Deleted '$file'";
}
} else {
echo "File '$file' does not exist!";
}
The one comment suggested you use FTP, you should have FTP access to your server then you can simply delete through FTP.

Renaming, move to a folder, and create folder

I am taking over a website already build because the original programmer is not available anymore.
I need to rename files in a directory, then move the files to their own folder matching the file name, but if the folder does not exist I need to create the folder.
To rename the files I have this php code that it was using the original programmer of the website (Was tested and is working as expected, Let me clarify that the files are something like STV12543.htm and need to be 12543_Todays-Date.htm)
<?php
function dirList ($directory, $prefijo )
{
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
$comienzo = substr($file, 0, 3);
if ($file != '.' && $file != '..' && $comienzo== $prefijo)
$results[] = $file;
}
closedir($handler);
return $results;
}
$directory = "ws/archivos-cli/";
$prefijo = "STV";
$resultado = dirList($directory, $prefijo);
foreach($resultado as $file){
$ultima_modificacion = filemtime($directory.$file);
$ultima_modificacion = date("Y-m-d", $ultima_modificacion);
$name = split('STV',$file);
$name = split('.htm',$name[1]);
$newname = $name[0].'_'.$ultima_modificacion.'.htm';
//if (!file_exists($directory.$newname))
rename($directory.$file, $directory.$newname);
}
?>
I guess I can call that scrip with a cron, but for moving files, I have a sh script
#!/bin/sh
source_dir='/ws/archivos-cli/'
target_dir='/ws/archivos-cli/subfolders'
name_separator='_'
(
cd ${source_dir} || {
echo "${source_dir} no existe!" ; exit 1
}
for i in `ls` ; do
client_name="`echo ${i} | cut -f1 -d${name_separator}`"
echo "-> Moving file [${i}] to [${target_dir}/${client_name}/] folder"
mv -vf ${i} ${target_dir}/${client_name}/ || break
echo
done
)
So, how can I combine them (the 2 scripts) and add the option to create the folder if not exists, based on the file name without the date?
Thank you in advance.

Why does opendir not show folders with a single integer as the name

I have a script that opens a directory, checks if the folders matches an array, and then opens them.
In the directory there is a list of folders like "apache2-50", but when the script opens that folder, it only displays the .DS_Store file. Here is the output:
This-is-not-a-MacBook:backend code archangel$ php -f frolic.php "/Users/archangel/Desktop/Httpbench Files/results"
Test Found apache2 in directory /Users/archangel/Desktop/Httpbench Files/results/apache2-50
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/.DS_Store
But here is the directory listing:
This-is-not-a-MacBook:apache2-50 archangel$ ls
0 1 2
Now what I am trying to figure out why my php script is not showing those folders. When I change the folder "0" to "3" it works:
This-is-not-a-MacBook:apache2-50 archangel$ ls
1 2 3
This-is-not-a-MacBook:backend code archangel$ php -f frolic.php "/Users/archangel/Desktop/Httpbench Files/results"
Test Found apache2 in directory /Users/archangel/Desktop/Httpbench Files/results/apache2-50
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/.DS_Store
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/1
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/2
--/Users/archangel/Desktop/Httpbench Files/results/apache2-50/3
Here is the code that I am running:
#!/bin/php
//...
$dir = opendir($argv[1]);
//Opened the directory;
while($file = readdir($dir)){
//Loops through all the files/directories in our directory;
if($file!="." && $file != ".."){
$f = explode("-", $file);
if(in_array($f[0], $servers) and in_array($f[1], $tests)) {
echo "Test Found $f[0] in directory $argv[1]/$f[0]-$f[1]\n";
$sdir = opendir("$argv[1]/$f[0]-$f[1]");
while($sfile = readdir($sdir)){
if($sfile!="." && $sfile != ".."){
echo "--$argv[1]/$f[0]-$f[1]/$sfile\n";
}
}
}
}
}
Could this be something wong with my script, or a bug in php(PHP 5.3.3)?
Thanks
This is a (very nasty) side effect of the string "0" evaluating to false in PHP. When that happens, your while loop
while($file = readdir($dir))
will break.
This should work, because it breaks only when readdir() actually returns false:
while(($file = readdir($dir)) !== false)
(obviously, change both loops accordingly, not just the outer one.)
Why are you using opendir at all? I think glob would be a little easier to use:
$files = glob("$argv[1]/*-*/*");
foreach($files as $file) {
$parts = explode("/", $file);
// get the directory part
$f = explode("-", $parts[count($parts) - 2]);
if(in_array($f[0], $servers) and in_array($f[1], $tests)) {
echo "Test Found $f[0] in directory $argv[1]/$f[0]-$f[1]\n";
echo "--$argv[1]/$f[0]-$f[1]/$sfile\n";
}
}
Replace
while($sfile = readdir($sdir)){
with
while(($sfile = readdir($sdir)) !== 0){
Otherwise when filename is 0, $sfile is "0" which is translated to false. By using !== or === you are forcing a type check between the variables so that "0" does not equal 0.

php directory explorer script help

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.

Categories