Move_upload_file does not work

Asked

Viewed 157 times

0

I am developing a code to open an xml, if the file does not exist, a new one will be created, beauty, the system is creating, but when it comes to moving the file to the folder that it will be stored, the move_upload_file does not work. Name of the folder where the file should be xml_msg. (The path is being saved in the bank, the problem is in moving the file). (Note: If you can help me, I will be grateful, it is kind of urgent pro 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;
}

1 answer

0

Your $caminho is already with the full name of the file and the directory it should be, only 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 sent.

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 sent and $nome the way you want him to be saved.

I believe that’s it, also make sure that your script has write permission as well as your target directory.

Here is an example 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"]);
}
  • I updated the gerarNome, but gave the error: Undefined index: file. Generate name att: Function gerarNomeXML() { $dir = "xml_msg"; $novonome = $dir . DIRECTORY_SEPARATOR . md5(uniqid(time())) . ". xml"; move_uploaded_file($_FILES["file"]["tmp_name"], $novonome); Return $novonome; }

  • I put an example code, test. I hope it helps

  • I will try to adapt my code with the one you passed, but a question, is it not moving the file because I am creating one instead of upar? Type, in your code the file is being uploaded by the user, in mine the file should be created if it does not exist instead of being upshipped..

  • The function move_uploaded_file does what it suggests: moves a file that has already been uploaded to the server. So your first argument should be a valid and existing file. In your code, this first argument has the name of the file you want to create, but it doesn’t exist yet. In short: what you have to do is take the file you sent and at the time of creating the file (with move_uploaded_file)...

  • Thanks for all the help, but I finally realized something, in lerXML, if xml does not exist, after creating it is not saved, so I gave a $dom->save($novonome), then it already saves inside the folder.

Browser other questions tagged

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