Search code examples
phpgetunlink

php $_GET dont take my document name with space so I cant unlink?


I have a problem I tried to retrieve a file with this code:

<?php


$path= "./uploadedfiles/";
$dir= dir($path);

while ($file = $dir->read()) {

        echo $file . "<a href=deletefile.php?file=$file>Delete</a><br>";         
}
$dir->close();
?>

and my deletefile.php

<?php
$file = $_GET['file'];

echo $file;

$path =  'C:/wamp/www/project/uploadedfiles/'.$files;

if(unlink($path)){

    echo "File deleted";
}else{
    echo "Erro no uploaded";

}


?>

The problem is that with the line $file = $_GET['file'];, if my files name is document name.pptx (space included) the $_GET just takes document, so my file never gets deleted, can someone help me? Help really appreciated


Solution

  • Try using urlencode() and urldecode(), this should encode the space in the filename when passing it over the URL

    So

    echo $file . "<a href=deletefile.php?file=" . urlencode($file) . ">Delete</a><br>";
    

    and

    $file = urldecode($_GET['file']);