Listing everything at once is a bad idea, unless you just want to run something in these files and folders, but don’t list everything.
Actually what you want is a "file manager" (file manager):
I want to keep you aware of something, this code can fail, regardless of who write or rewrite, if you are going to use in production I recommend looking for tools already considered, so take these codes as a case study, or test the following codes very well.
On GNU/Linux-based servers there are folder permissions and access levels, so maybe not every file is accessible, of course I assume that the files will be generated by the php script itself, so you won’t have this problem with the "file owner"
For case of 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 going to list 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 to access navegar.php?pasta=
it will point to the new directory.
The editar.php
reads and writes the file, note that I used LOCK_EX
to avoid more than one person 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 which you should study:
For production
If it is the case of production environment recommend using applications already conceptualized as and properly tested, such as:
I haven’t tested, I don’t know if they work as well, but it has repository and bug control
Our brother.. Vlw by the tips and idea... I will study the script and functions... Brigadão.. Hehe.. only one question... on it it is possible to edit and save the changes?
– Pedro Quezado
@Pedroquezado I fixed the delete.php !
– Guilherme Nascimento
very good @Guilhermenascimento
– durtto