How to create directory and upload file

0

Well, I have the following question: can I create a directory with mkdir and in the same PHP code insert files coming from uploads in this created directory?

For example I have a form where I get uploaded files, and I would like to create a folder and play those files in it at the same time, can I do it all in the same code so that I only use one action?

PHP code:

$vregistro = utf8_decode($_POST['f_registro']); //Recebe valor de um input

mkdir("C:\test\Arquivos\Documentos/$vregistro", 0777, true); 
//Cria uma pasta com o nome designado na variável

$uploaddir = 'C:\test\Arquivos\Documentos/';       
$uploadfile = $uploaddir . basename($_FILES['fanexo']['name']);              

echo                                                                 

if (move_uploaded_file($_FILES['fanexo']['tmp_name'], $uploadfile)) {
    
asked by anonymous 31.08.2017 / 17:36

1 answer

1

Yes it is possible,

In this link you have an example file upload link

To create the folder you use the command mkdir in the link below more details link

You can check if the directory already exists before creating, and then save the file

$uploaddir = dirname(__FILE__).'/nome_diretorio';     
if (!is_dir($uploaddir)) {
   mkdir($uploaddir);
}
if (move_uploaded_file($_FILES["fanexo"]["tmp_name"], $uploaddir)) {
   echo "UPLOAD ok.";
} else {
   echo "Erro.";
}

Using dirname (__ FILE __) the path is relative to the current file directory.

I hope I have helped

    
31.08.2017 / 18:16