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;
}
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; }
– Leandro
I put an example code, test. I hope it helps
– boolean
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..
– Leandro
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)...– boolean
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.
– Leandro