Listing everything at once is a bad idea, unless you just want to run something in these files and folders, but do not list everything.
Actually what you want is a "file manager":
I want to make you aware of one thing, this code can fail, regardless of who write or rewrite, if it is to be used in production I recommend looking for tools already considered, then take these codes as a case study, or test the codes very well below.
In GNU / Linux based servers there are folder permissions and access levels, so maybe not every file is accessible, of course I assume the files will be generated by the php script itself, so you will not have this problem with the "owner of the file "
For case studies
First create a global.php file, because we will use more than one file and in this case it is better to keep the settings in one and use include
, create a file called global.php
:
<?php
define('DATA_FOLDER', '/home/user/arquivos'); //Troque pela pasta que deseja editar e navegar
If you are listing a folder (which I recommend) at a time you can do so (called navegar.php
):
<?php
include 'global.php';
$ap = DATA_FOLDER;
$dir = '/';
if (empty($_GET['pasta']) === false) {
$dir = '/' . trim($_GET['pasta'], '/');
}
if (isset($_GET['deletar']) && file_exists($ap . $dir)) {
echo '<a href="deletar.php?arquivo=', $ap . $dir,'">Quer realmente deletar o arquivo "', $ap , $dir, '"?</a><hr>';
}
if (is_dir($ap . $dir)) {
$dh = opendir($ap . $dir);
if ($dh) {
echo '<ul>';
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$acessivel = false;
$atual = $ap . $dir . '/' . $file;
if (is_dir($atual)) {
echo '<li> <a href="navegar.php?pasta=', $dir, '/', $file, '">', $file,'</a>';
$acessivel = true;
} else if (is_file($atual)) {
echo '<li>', $file,' <a href="editar.php?arquivo=', $dir, '/', $file, '">[Editar]</a>';
$acessivel = true;
}
if ($acessivel) {
echo ' <a href="deletar.php?arquivo=', $dir, '/', $file, '&deletar=1">[Deletar]</a> </li>';
}
}
echo '</ul>';
closedir($dh);
}
}
?>
<hr>
<a href="navegar.php">Voltar a raiz</a>
When accessing navegar.php?pasta=
it will point the new directory.
editar.php
reads and writes the file, note that I used LOCK_EX
to prevent more than one person from deleting while recording.
<?php
include 'global.php';
if (empty($_GET['arquivo'])) {
echo 'Arquivo não definido';
exit;
}
$ap = DATA_FOLDER;
$arquivo = $ap . '/' . $_GET['arquivo'];
if (false === is_file($arquivo)) {
echo 'Arquivo não inacessivel ou invalido:', $arquivo;
exit;
}
echo 'Você está editado: ', $arquivo, '<br>';
if (isset($_POST['conteudo'])) {
file_put_contents($arquivo, $_POST['conteudo'], LOCK_EX);
}
?>
<form action="editar.php?arquivo=<?php echo $_GET['arquivo']; ?>" method="POST">
<textarea name="conteudo" cols="150" rows="50"><?php
echo htmlspecialchars(file_get_contents($arquivo));
?></textarea><br>
<button type="submit">Salvar</button>
</form>
<hr>
<a href="navegar.php?pasta=<?php echo dirname($_GET['arquivo']); ?>">Voltar para navegação</a>
And finally delete.php:
<?php
if (empty($_GET['arquivo'])) {
echo 'Arquivo não definido';
exit;
}
include 'global.php';
$ap = DATA_FOLDER;
$arquivo = $ap . '/' . $_GET['arquivo'];
if (is_dir($arquivo)) {
//Deleta pasta recursivamente, pois não é possivel deletar pastas que não estão vazias
function recursive_rmdir($path) {
if (is_dir($path) && ($dh = opendir($path))) {
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$atual = $path . '/' . $file;
if (is_dir($atual)) {
recursive_rmdir($atual);
} else if (is_file($atual)) {
unlink($atual);
} else {
echo $atual, ' é inacessivel<br>';
}
}
closedir($dh);
rmdir($path);
} else if (is_file($path)) {
unlink($path);
}
}
recursive_rmdir($arquivo);
} else if (is_file($arquivo)) {
if (unlink($arquivo)) {
echo 'Arquivo "', $arquivo,'" deletado com sucesso';
} else {
echo 'Falha ao deletar o arquivo "', $arquivo,'"';
}
} else {
echo 'Arquivo "', $arquivo,'" inacessivel';
}
?>
<hr>
<a href="navegar.php?pasta=<?php echo dirname($_GET['arquivo']); ?>">Voltar para navegação</a>
Functions used and what you should study:
- Open directory: link
- Checks if file: link
- Veirifca if it's folder: link
- Delete files: link
- Delete empty folders: link
- Write content to a file (it's binary-safe): link
- Read the contents of a file (it's binary-safe): link
For production
If it is the case of a production environment, I recommend using well-tested applications, such as:
I have not tested, I do not know if they work so well, but it has repository and bug tracking
-
link (GPLv2 license, full of functions, has plugins, thumbnails, ajax based)
-
link (paid but seems to be well advanced and full of extra functions such as thumbnails and ajax-based)
- link
- link
- link
- link