Last active
May 2, 2024 10:54
-
-
Save rmgimenez/f96ab44738fc36027affe6a8356c768b to your computer and use it in GitHub Desktop.
Script PHP que faz o upload de um arquivo e retorna um json com a url do arquivo ou a mensagem de erro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pastaUpload = '/c:/wamp64-3.3.0-20230516/www/aps/uploads/'; | |
$endereco = 'http://localhost/aps/uploads/'; | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
// Check if file was uploaded without errors | |
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) { | |
$uploadDir = $pastaUpload; // Directory to save uploaded files | |
$dataTimestamp = date('YmdHis'); // Current date and time | |
$fileName = $dataTimestamp . '_' . $_FILES['file']['name']; // New file name | |
$filePath = $uploadDir . $fileName; | |
// Move the uploaded file to the desired directory | |
if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) { | |
$fileUrl = $endereco . $fileName; // URL of the uploaded file | |
// Create a JSON response | |
$response = array( | |
'success' => true, | |
'message' => 'Upload feito com sucesso!', | |
'url' => $fileUrl | |
); | |
// Convert the response to JSON format | |
$jsonResponse = json_encode($response); | |
// Output the JSON response | |
echo $jsonResponse; | |
} else { | |
echo json_encode(array( | |
'success' => false, | |
'message' => 'Falha ao mover o arquivo enviado.' | |
)); | |
} | |
} else { | |
echo json_encode(array( | |
'success' => false, | |
'message' => 'Erro ao fazer upload do arquivo.' | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment