I needed a code to upload images to the web application and I did it using the following example:
the code works fine on localhost on apache. Now I want to convert the image file to the same base64 while uploading it to the server. I couldn't find how to modify the code for this. Can anyone help? Thanks.
This is upload.php:
<?php
/* get the name of the uploaded file */
$filename = $_FILES['file']['name'];
/* choose where to save the uploaded file */
$location = "upload/".$filename;
/* save the upploaded file to the local filesystem */
if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
echo 'Success';
} else {
echo'Failure';
}
?>
and this is uploader.html
<!DOCTYPE html>
<html>
<head>
<title> Ajax JS F Up Example </title>
</head>
<body>
<!-- HTML5 Input Form Elements -->
<input id="fileupload" type="file" name="fileupload" />
<button id="upload-button" onclick="uploadFile()"> Upload </button>
<!-- Ajax JS File Up Logic-->
<script>
async function uploadFile() {
let formData = new FormData();
formData.append("file", fileupload.files[0]);
await fetch('/upload.php', {
method: "POST",
body: formData
});
alert('File uploaded.');
}
</script>
</body>