1
I am trying to upload a PDF file via Ajax and Php. But I can’t.
HTML
<script>
var client = new XMLHttpRequest();
function upload(){
var file = document.getElementById("uploadfile");
var formData = new FormData();
formData.append("upload", file.files[0]);
formData.append("MAX_FILE_SIZE", '30000');
formData.append("userfile", 'pdf');
client.open("post", "upload.php", true);
client.send(formData);
}
client.onreadystatechange = function(){
if (client.readyState == 4 && client.status == 200){
$("#resposta").html(client.responseText);
}
}
</script>
<input type="file" id="uploadfile" name="uploadfile" />
<input type="button" value="upload" onclick="upload()" />
PHP
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir.basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Arquivo válido e enviado com sucesso.\n";
} else {
echo "Possível ataque de upload de arquivo!\n";
}
echo "<br><br><br>";
echo 'Aqui está mais informações de debug:';
print_r($_FILES);
print "</pre>";
?>
PHP returns no error, but the file is not uploaded.
Great, thank you very much ! Improved a lot
– Pedro Augusto