0

I needed a code to upload images to the web application and I did it using the following example:

source

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>
kadir
  • 1
  • Why do you think you need to base64 encode your image? – Tangentially Perpendicular Jul 02 '21 at 22:23
  • Does this answer your question? [How can I convert an image into Base64 string using JavaScript?](https://stackoverflow.com/questions/6150289/how-can-i-convert-an-image-into-base64-string-using-javascript) – Tangentially Perpendicular Jul 02 '21 at 22:24
  • Because it is an internship/homework-like project and task givers want base64 encoding, even restful architechture and server-side computer vision goals at the next steps... Thanks for the sample but no, i cant implement it to my code comparing with your posted example... – kadir Jul 03 '21 at 11:38

0 Answers0