Define a directory for input type file

0

I have a web application that generates a txt file, which is hosted in a default folder on the server, and sends it through PHPmailer automatically. For reasons of having a second option, I added the functionality of sending it manually using a form, also by PHPMailer. The question arises because when the browser is opened to choose the txt, all the folders of the site are displayed. Although in the input type file filter the file type, I would like to know if there is a way to hide or prevent access to the other folders.

    
asked by Fernando Rizo Patrón 14.04.2017 в 01:22
source

1 answer

1

If the file is on the server and a user must select a file to attach then what you should do is list the files of a certain folder.

You can use the following script to achieve it.

$directorio = opendir("."); //ruta actual
while ($archivo = readdir($directorio)) //obtenemos un archivo y luego otro sucesivamente
{
    if (is_dir($archivo))//verificamos si es o no un directorio
    {
        echo "[".$archivo . "]<br />"; //de ser un directorio lo envolvemos entre corchetes
    }
    else
    {
        if (strpos($archivo,".txt") !== false)
        {
        echo $archivo . "<br />";
        }
    }
}

Then it is up to you with what shape the results are printed. If in a SELECT, in a messy list, etc.

I limit that only files with .txt format are printed, but you can modify it or directly remove it. If you do not want the folders to print, you must use the code as well.

$directorio = opendir("."); //ruta actual
while ($archivo = readdir($directorio)) //obtenemos un archivo y luego otro sucesivamente
{
    if (!is_dir($archivo))//verificamos si es o no un directorio
    {
        if (strpos($archivo,".txt") !== false)
        {
        echo $archivo . "<br />";
        }
    }
}
    
answered by 14.04.2017 / 03:28
source