Upload file to a folder inside the root folder with PHP

1

Based on the PHP documentation code on fileupload I created the following code:

<?php
$uploaddir = './novo/side';
$uploadfile = basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Arquivo válido e enviado com sucesso.\n";
  } else {
 echo "Possível ataque de upload de arquivo!\n";
}

   echo 'Aqui está mais informações de debug:';
   print_r($_FILES);

   print "</pre>";

?>

So that's fine, when I use the HTML form it takes the file and brings it to the project root, the problem is that I want it to go to a folder that is inside the root called side .

$uploaddir = './novo/side';

The result I have is that the file is only in the novo folder.

In tests I freed the inherited share so that it would not have access to the folders.

I'm using Windows with WampServer.

    
asked by anonymous 12.09.2017 / 21:43

1 answer

2

In function move_uploaded_file you are moving it to:

$uploadfile = basename($_FILES['userfile']['name']);

Then the correct one, in your case would be:

$pasta = $uploaddir."/".$uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'], $pasta)
    
12.09.2017 / 22:11