Ejemplo n.º 1
0
<?php

require_once "config.php";
list($type, $data) = explode(';', $_POST['foto']);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$conexao = new Conexao();
$dao = new DAOGenerico();
$dados = $dao->findByPk($conexao->getConexao(), "pacientes", (int) $_GET["id"]);
$diretorio = DIR_UPLOADS . SEPARADOR_DIRETORIO . "pacientes" . SEPARADOR_DIRETORIO . $dados["id"];
if (existeArquivo($diretorio . SEPARADOR_DIRETORIO . $dados["foto"])) {
    excluiArquivo($diretorio . SEPARADOR_DIRETORIO . $dados["foto"]);
}
if (!existeDiretorio($diretorio)) {
    criaDiretorio($diretorio);
}
$nomeFoto = date('dmYHis') . '.png';
$file = file_put_contents($diretorio . '/' . $nomeFoto, $data);
if (is_integer($file)) {
    $dados["foto"] = $nomeFoto;
    $dao->salva($conexao->getConexao(), "pacientes", $dados);
    $conexao->getConexao()->commit();
    $conexao->getConexao()->disconnect();
    echo $diretorio . '/' . $nomeFoto;
}
Ejemplo n.º 2
0
<?php

// You need to add server side validation and better error handling here
$data = array();
if (isset($_GET['files'])) {
    require_once "../../config.php";
    $error = false;
    $files = array();
    $uploaddir = DIR_UPLOADS . SEPARADOR_DIRETORIO . 'atendimentos' . SEPARADOR_DIRETORIO . $_GET['id'] . SEPARADOR_DIRETORIO;
    if (!existeDiretorio($uploaddir)) {
        if (!criaDiretorio($uploaddir)) {
            $data = array('error' => 'Erro ao tentar criar diretório');
        }
    } else {
        foreach ($_FILES as $file) {
            if (move_uploaded_file($file['tmp_name'], $uploaddir . basename($file['name']))) {
                $files[] = $uploaddir . $file['name'];
            } else {
                $error = true;
            }
        }
        $data = $error ? array('error' => 'There was an error uploading your files') : array('files' => $files);
    }
} else {
    $data = array('success' => 'Form was submitted', 'formData' => $_POST);
}
echo json_encode($data);