Good day, basically I need to take a picture and re-name it with the name that the user provides in an input and upload it to the server.
With this code, the images are already uploaded to the server, it would only be necessary to name them with the key provided by the user in the input, because if I try to do it in the manner below it marks me the following error:
Notice: Undefined index: clave in C:\wamp64\www\Idis\php\upload.php on line 2
HTML Code:
<form id="formUpl">
<input id="fachada" name="clave"/>
<span>Imagenes</span>
<input type="file" name="archivo[]" multiple>
<button type="submit">Subir</button>
</form>
JS Code
$("#formUpl").submit(function(e) {
e.preventDefault();
var formulario = new FormData($(this)[0]);
$.ajax({
method: "post",
url: "php/upload.php",
data:formulario,
cache: false,
processData: false,
contentType: false,
success: function(respAX) {
if (respAX == 0) {
alert("ERROR");
} else {
console.log(respAX);
}
}
});
});
And the Php code with which I want to name the photo and upload it to the server.
<?php
$clave = $_POST['clave'];
$total = count($_FILES['archivo']['name']);
for($i=0; $i<$total; $i++) {
$tmpFilePath = $_FILES['archivo']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newname = $clave.'.jpg';
print_r($clave);
if(move_uploaded_file($tmpFilePath,"uploads/".$newname)) {
echo 1;
}else{
echo 0;
}
}
}
?>