PHP Upload files to a directory created with mkdir

1

I have an HTML form where I get uploaded files, in the case of an image and another with several files in PDF, I need to organize them by placing everything in a folder, for that I used mkdir, I was able to create a folder with the name that I get a variable, ie the name of the folder being created is a variable that takes content via POST from a form input, how do I play the files coming from the upload within that folder being created in the same code?

What's happening at the moment with my current code is: the files are placed in the same place where the folder is created but I can not put them inside it.

PHP code:

$vregistro = utf8_decode($_POST['f_registro']); //Guarda um nome digitado pelo usuário que será o nome da pasta criada

mkdir("C:\local\Arquivos\Documentos&Logos/$vregistro", 0777, true);  //Cria a pasta no servidor com o nome guardado na variável



$uploaddir = 'C:\local\Arquivos\Documentos&Logos/';           //Arquivo fica no mesmo local da pasta criada porém fora dela
$uploadfile = $uploaddir . basename($_FILES['fanexo']['name']);              

echo '<pre>';                                                                

if (move_uploaded_file($_FILES['fanexo']['tmp_name'], $uploadfile)) {                   //Upload de imagem que precisa ser guardado na pasta criada acima
    echo "Arquivo válido e enviado com sucesso.\n";

}else{ 

    echo "Upload ERROR <--\n";

}

echo 'Debug: ';
print_r($_FILES);

print "</pre>";




$total = count($_FILES['pdfanexo']['name']);

for($i=0; $i<$total; $i++) {                                      //Upload de vários PDFS que precisa ser guardados na mesma pasta que a imagem no caso a pasta criada no inicio
    $tmpFilePath = $_FILES['pdfanexo']['tmp_name'][$i];

    if ($tmpFilePath != ""){
        $newFilePath = "C:\local\Arquivos\Documentos&Logos/" . $_FILES['pdfanexo']['name'][$i];

        if(move_uploaded_file($tmpFilePath, $newFilePath)) {



        }
    }
}
    
asked by anonymous 04.09.2017 / 14:17

1 answer

1

Good looking over rewrote part of code.

<?php
$vregistro = utf8_decode($_POST['f_registro']); //Guarda um nome digitado pelo usuário que será o nome da pasta criada
$diretorioBase = 'C:\local\Arquivos\Documentos&Logos\';
$fullPath=rtrim($diretorioBase.$vregistro,'\/').'/';
if (!@mkdir($fullPath, 0777, true) && !is_dir($diretorioBase.$vregistro)) {

    $uploadfile = $fullPath.basename($_FILES['fanexo']['name']);

    if (move_uploaded_file(
        $_FILES['fanexo']['tmp_name'],
        $uploadfile
    )) {                   //Upload de imagem que precisa ser guardado na pasta criada acima
        echo "Arquivo válido e enviado com sucesso.\n";
    } else {
        echo "Upload ERROR <--\n";
    }
    echo 'Debug: ';
    print_r($_FILES);
    print "</pre>";
    $total = count($_FILES['pdfanexo']['name']);
    for ($i = 0; $i < $total; $i++) {                                      //Upload de vários PDFS que precisa ser guardados na mesma pasta que a imagem no caso a pasta criada no inicio
        $tmpFilePath = $_FILES['pdfanexo']['tmp_name'][$i];
        if ($tmpFilePath != "") {
            $newFilePath = $fullPath.$tmpFilePath;
            if (move_uploaded_file($tmpFilePath, $newFilePath)) {


            }
        }
    }
}

repair the use of $fullPath=rtrim($diretorioBase.$vregistro,'\/').'\'; so I guarantee you'll end with \ in the end. the main error in your code and at the time of generating $newFilePath , at this point you are not generating the directory name, so we have:

DE:

$newFilePath = "C:\local\Arquivos\Documentos&Logos/" . $_FILES['pdfanexo']['name'][$i];

TO:

$newFilePath = "C:\local\Arquivos\Documentos&Logos/$vregistro/" . $_FILES['pdfanexo']['name'][$i];

Notice that my code is different, with this modification your should already become functional, just notice that ending the PATH with a / the use of \ also results in the separation of directory in windows, in case you want a more readable code and that works on both windows and unix like, use the constant DIRECTORY_SEPARATOR instead of any / or \ .

    
06.09.2017 / 17:48