How to create directory and upload file

Asked

Viewed 1,582 times

0

Well I am with the following doubt, it is possible to create a directory with mkdir and in the same PHP code insert uploads files in this created directory?

For example I have a form where I receive files by upload, and I would like to create a folder and throw these files in it at the same time, it is possible to do this all in the same code in a way that uses only 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)) {
  • 1

    yes it is possible, post your code

  • 1

    Post the code, please.

1 answer

1


Yes it is possible,

This link has an example file upload https://www.w3schools.com/php/php_file_upload.asp

To create the folder you use the mkdir command in the link below has more details http://php.net/manual/en/function.mkdir.php

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’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.