Move_upload_file does not work

0

I'm developing a code to open an xml, if the file does not exist, it will create a new, beauty, the system is creating, but at the time of moving the file to the folder it will be stored, move_upload_file does not work. The name of the folder where the file should be is xml_msg . (The path is being saved in the database, the problem is in moving the file). (Note: If you can help me, I'll be grateful, it's kind of urgent for TCC.)

codes:

function gerarNomeXML() {
    $novonome = "xml_msg/" . md5(uniqid(time())) . ".xml";
    return $novonome;
}

function lerXML($dom, $conexao, $cpfCli, $cpfTec, $tipo) {
    if ($tipo === 2) {
        $sql = "select caminho_xml as xml from tbl_mensagem where cpf_cliente_fk = $cpfCli and cpf_tecnico_fk = $cpfTec";
    } else {
        $sql = "select caminho_xml as xml from tbl_mensagem where cpf_cliente_fk = $cpfTec and cpf_tecnico_fk = $cpfCli";
    }
    $return = mysqli_query($conexao, $sql);
    $row = mysqli_fetch_array($return, MYSQLI_ASSOC);
    $nomeArquivo = $row["xml"];
    if (!file_exists($nomeArquivo)) {
        $novonome = gerarNomeXML();
        guardarXML($conexao, $novonome, $cpfCli, $cpfTec, $tipo);
        // criando nó principal
        $root = $dom->createElement("mensagens");
        // retirar os espaços em branco
        $dom->preserveWhiteSpace = false;
        // gerar código ??
        $dom->formatOutput = true;
        $_SESSION['nomeXML'] = $novonome;
        return $root;
    } else {
        // carrega o arquivo
        $dom->load($nomeArquivo);
        $_SESSION['nomeXML'] = $nomeArquivo;
        // recupera nó principal
        $root = $dom->documentElement;
        return $root;
    }
}

function guardarXML($conexao, $caminho, $cpfCli, $cpfTec, $tipo) {
    if ($tipo == 2) {
        $sql = "update tbl_mensagem set caminho_xml = '$caminho' where cpf_cliente_fk = $cpfTec and cpf_tecnico_fk = $cpfCli";
    } else {
        $sql = "update tbl_mensagem set caminho_xml = '$caminho' where cpf_cliente_fk = $cpfCli and cpf_tecnico_fk = $cpfTec";
    }
    move_uploaded_file($_FILES[$caminho]['error'], $caminho);

    $resultado = mysqli_query($conexao, $sql);
    return $resultado;
}
    
asked by anonymous 20.06.2018 / 00:38

1 answer

0

Your $caminho already has the full name of the file and the directory it should be, but if it does not exist it is not there, you want to put it there.

So, try to use $_FILES["file"]["tmp_name"] this will be the file you uploaded.

Instead:

move_uploaded_file($_FILES[$caminho]['error'], $caminho);

Try this:

$dir = "xml_msg";
$nome = $dir.DIRECTORY_SEPARATOR.md5(uniqid(time())) . ".xml";
move_uploaded_file($_FILES["file"]["tmp_name"], $nome);

$_FILES["file"]["tmp_name"] will have the file you uploaded and $nome the path you want it to be saved.

I believe this to be the case, also make sure that your script is writable, as well as your target directory.

Here is a sample code:

<?php
ini_set('display_errors',1);
ini_set('display_startup_erros',1);
error_reporting(E_ALL);
?>

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <button type="submit">Enviar</button>
</form>
<?php
$file = "";
if ($_SERVER["REQUEST_METHOD"]==="POST"){
    $file  = isset($_FILES["file"])?$_FILES["file"]:"";
}
$dir ="xml_msg";

if(!is_dir($dir)){
   mkdir($dir);
    echo "Pasra criada com sucesso";
}

if (!empty($file)) {
move_uploaded_file($file["tmp_name"],$dir.DIRECTORY_SEPARATOR.$file["name"]);
}
    
20.06.2018 / 01:08