I'm triying to fetch the name of a file uploaded and set it as the value of a hidden input on the same form. I've tried this and it uploads the file but dont send the file name.
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"../img/plates/".$file_name);
header("Location: backend.php");
}else{
print_r($errors);
}
}
After that i try to inset the $file_name in to the same form as the file input. Something like this
<input name="image" type="file" placeholder="Select photo">
<input type="hidden" value="<?php echo $file_name ?>" name="photo" />
<button type="submit" name="add">ADD NEW ITEM</button>
$file_nameempty after that. Can you please explain in more details what you're trying to do here? What problem are you trying to solve etc?$_FILESarray?$_FILES-array when you post the form? My suggestion about sessions was to repopulate the form after the post. The JS solution would populate the name before the post. Totally different things. But since you haven't explain why or when you need this, I can't say which is better. Two different use cases.