PHP Upload files to a directory created with mkdir

Asked

Viewed 948 times

1

I have an HTML form where I receive files by upload, in case one of image and the other with several PDF files, I need to organize them putting everything in a folder, for that I used mkdir, I managed to create a folder with the name I receive from 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 inside that folder being created in the same code?

What is happening at the moment with my current code is: the files are placed in the same place where the folder is created but I cannot place 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)) {



        }
    }
}
  • 1

    just take the name of the folder you created and put it in the path $uploaddir = 'C:\local\Arquivos\Documentos&Logos/'.$vregistro;

  • Doing this it continues the same way, just puts the name that is stored in $vregistro in the upload files.

1 answer

1


Good looking over rewrote part of the 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 that it ends with a at the end. the main error in your code and the time to generate the $newFilePath, at this point you are not generating the directory name, so we have:

OF:

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

FOR:

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

Note that my code is different, with this modification yours should already become functional, just note that I finish the PATH with a / the use of also results in directory separation in windows, if you want a more readable code that works on both windows and Unix like, use the constant DIRECTORY_SEPARATOR in place of any / or \ .

  • So, I’ve been trying to do this but when I make the change the folder keeps being created but the files are not uploaded, no error appears only simply does not upload.

  • Isn’t there some log, something to see some point to explore? Without this kind of information we can’t help you

  • I made a change here and I managed to make it work thanks very much for the help is working all right now.

  • Say what happened, it might help others by going through the same.

  • Well basically what was missing was to indicate in the directories that "$vregistro" was a variable, what I did was to make the mkdir like this: " '.... Archive Documents&logos/'. $vregistro " and when uploading into this folder: "'.... Archive Documents&logos/' . $vregistro. '/' ".

  • 1

    Got it, another way for you to act "..\..\Arquivos\Documentos&Logos/{$vregistro}/ ", haha is more readable in my opinion

  • 1

    If you realize that’s what I do at this point $diretorioBase = 'C:\local\Arquivos\Documentos&Logos\\'; $fullPath=rtrim($diretorioBase.$vregistro,'\\/').'/';

  • It is necessary to give a better study in PHP I’m starting yet, so I let go of some things just by being expressed otherwise. =/

Show 3 more comments

Browser other questions tagged

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