I have a webpage with several images. Each image can be replaced by an other image on the same place. Next to each image there is a form to upload the new image.
Important is that the new filename must be replaced with a fixed name eg.: cat.jpg becomes image1.jpg, dog.jpg becomes image5.jpg, depending on the form used.
I use this html code for a first image:
<div class="w3-padding-8 w3-card-8 w3-center w3-third w3-container ">
<div class="w3-padding-8 w3-card-8 w3-center w3-container"><img src="images/cat.jpg" class="w3-padding-8 style="width:100%"></div>
<div class="w3-center w3-container">
<form method="POST" action="upload4.php" enctype="multipart/form-data">
<input type="file" multiple name="file[]" data-maxfilesize="5000000">
<input class="button-primary" type="submit" value="Submit 1">
</form>
</div>
</div>
In the upload4.php I have this code to receive the new file. Now it assigns the name images/image1.jpg to it but I need a variable with the new filename in it.
<?php
$sentfile = 'images/image1.jpg';
if (is_array($_FILES['file']['name'])) {
foreach($_FILES['file']['name'] as $k=>$filename) {
$uploadfile = $sentfile;
if (move_uploaded_file($_FILES['file']['tmp_name'][$k], $uploadfile)) {
// ok
} else {
$error_message.= "Error while uploading file ".$filename."<br />";
}
}
} elseif(isset($_FILES['file']['name'])) {
$uploadfile = $sentfile;
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
// ok
} else {
$error_message = "Error while uploading file ".$_FILES['file']['name']."<br />";
}
}
?>
How do I assign images/image1.jpg to $sentfile?
I think I need AJAX? But how?
I know Javascript is client-side and php is serverside but how to communicate...
actionattribute values? Or what else?