D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
html
/
crsys_ant
/
Filename :
uploadunico.php
back
Copy
<?php // ==================== PROCESSAR UPLOAD ==================== if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_FILES['imagem']) && $_FILES['imagem']['error'] === 0) { $pasta = "C:/wamp64/www/crsys/adm/orcamento/"; if (!is_dir($pasta)) { mkdir($pasta, 0777, true); } $nomeTemp = $_FILES['imagem']['tmp_name']; $nomeArquivo = basename($_FILES['imagem']['name']); // Gera nome único $nomeArquivo = basename($_FILES['imagem']['name']); $nomeArquivo = str_replace(" ", "_", $nomeArquivo); // troca espaços por _ // acrescenta data e hora ao nome $timestamp = date('d_m_Y_H_i_s'); // dia_mes_ano_hora_min_segundos $nomeFinal = pathinfo($nomeArquivo, PATHINFO_FILENAME) . "_{$timestamp}." . pathinfo($nomeArquivo, PATHINFO_EXTENSION); var_dump($nomeFinal); $caminhoCompleto = $pasta . $nomeFinal; if (move_uploaded_file($nomeTemp, $caminhoCompleto)) { $mensagem = "Arquivo enviado com sucesso!"; } else { $mensagem = "Erro ao enviar o arquivo!"; } } else { $mensagem = "Nenhum arquivo enviado!"; } } ?> <!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <title>Upload de Imagem</title> <style> /* ===== CONTAINER PRINCIPAL (400x400) ===== */ .box-400 { width: 400px; height: 400px; border: 2px solid #777; padding: 10px; display: flex; flex-direction: column; gap: 10px; justify-content: flex-start; align-items: center; box-sizing: border-box; background: #f8f8f8; } /* ===== FORMULÁRIO ===== */ .custom-file-label { display: inline-block; padding: 6px 12px; border: 1px solid #777; background-color: #eee; cursor: pointer; user-select: none; } input[type="file"] { display: none; } .container-img { width: 300px; height: 300px; border: 1px solid #ccc; overflow: hidden; display: flex; justify-content: center; align-items: center; background: #fff; } .container-img img { width: 100%; height: 100%; object-fit: cover; } </style> </head> <body> <?php if (isset($mensagem)): ?> <p><strong><?= $mensagem ?></strong></p> <?php endif; ?> <div class="box-400"> <form action="" method="POST" enctype="multipart/form-data"> <label for="input-img" class="custom-file-label">Selecione</label> <input type="file" id="input-img" name="imagem" accept="image/*" required> <button type="submit">Enviar</button> </form> <div class="container-img"> <img id="preview-img" src="" alt="Pré-visualização"> </div> </div> <script> document.getElementById("input-img").addEventListener("change", function () { const file = this.files[0]; const preview = document.getElementById("preview-img"); const label = document.querySelector(".custom-file-label"); if (file) { const reader = new FileReader(); reader.onload = function (e) { preview.src = e.target.result; }; reader.readAsDataURL(file); label.textContent = file.name; } else { preview.src = ""; label.textContent = "Selecione"; } }); </script> </body> </html>