PHP Upload a level in a path to delete an image ../

0

I have a form where you can upload and delete images that are stored in a folder that is at a higher level. This is the route:

  • Imagenes_folder / imagen.jpg
  • Form_folder / form.html

From form.html I want to delete an image but can not find the path in php. $_POST['archivo'] contains the name of the image, in this example it would be "imagen.jpg"

<?php
$mainRoot = "..//" . "Imagenes_folder//";
if (isset($_POST['archivo'])) {  
    $archivo = $_POST['archivo'];
    $ruta = $mainRoot . basename($archivo);
    if (file_exists($ruta)) {
        unlink($ruta);
    }
}
?>

I do not recognize the route. If I change form.html so that it is on the same level as Imagenes_folder if it works for me:

  • Imagenes_folder / imagen.jpg
  • form.html

        $mainRoot = "Imagenes_folder//";
        if (isset($_POST['archivo'])) {  
            $archivo = $_POST['archivo'];
          $ruta = $mainRoot . basename($archivo);
             if (file_exists($ruta)) {
                unlink($ruta);
            }
        }
    

"..//" This is not the way to go up a level? I have tried with "../" and "../../"

    
asked by Maria Jo 03.11.2017 в 14:29
source

2 answers

0

Thank you very much for your answers. Finally, what worked for me to go up one level is "./" (only one point)

I found the solution here link

    
answered by 07.11.2017 / 13:40
source
-1

One way to do what you want would be with dirname which:

  

Returns the path of a parent directory

     

Given a string that contains the path to a file or directory, this   function will return the path of the parent directory that is levels   levels of the current directory.

levels

  

The number of parent directories to upload.

     

Must be an integer greater than 0.

If you use PHP 7 this code would give you the path of a directory back to the directory where the script runs: dirname(__FILE__, 2)

Changing the number two to 3 would back another directory to the root and so on.

For PHP 5 or below, it would be the same, only that it does not support numbers and you would have to express the directory backspace using dirname and parentheses for each backspace.

<?php

#PHP 7  
#Un directorio hacia atrás  
$ruta=dirname(__FILE__, 2);
echo $ruta; 
echo "<hr />";


#Dos directorios hacia atrás    
$ruta=dirname(__FILE__, 3);
echo $ruta; 
echo "<hr />";


#PHP 5

#Un directorio hacia atrás  
$ruta_php5=dirname(dirname(__FILE__));
echo $ruta_php5;
echo "<hr />";


#Dos directorios hacia atrás    
$ruta_php5=dirname(
                    dirname(
                                dirname(__FILE__)
                            )
            );

echo $ruta_php5;
?>

Result:

Assuming the script is in a directory within test :

Note that you do not add the / at the end, so you would have to concatenate it to the name of the file so that the route does not fail: /archivo.txt .

PHP 7

/home/miusuario/public_html/test
/home/miusuario/public_html

PHP 5

/home/miusuario/public_html/test
/home/miusuario/public_html
    
answered by 03.11.2017 в 15:04