0

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>
6
  • 1
    You're redirecting the user after a successful upload, which would make $file_name empty after that. Can you please explain in more details what you're trying to do here? What problem are you trying to solve etc? Commented Sep 15, 2018 at 5:04
  • @MagnusEriksson I see what you're saying now. Let me read about it and how to solve it. This form insert the data into a json file, i want to save the name of the uploaded file to a field named "photo". All the rest of the script works fine. Thanks for your comment and sorry for the noob question. Commented Sep 15, 2018 at 5:09
  • 1
    No worries. We've all been there and asking questions is the right way of learning (and it's what this site is for :-)). Why would you need that, though? You already have the name in the $_FILES array? Commented Sep 15, 2018 at 5:14
  • @MagnusEriksson something like $_SESSION['NameOfFile'] = $_POST[$file_name]; /// and then inserting it like <?=$_SESSION['NameOfFile']?> ? Is this solution better than using javascript as Danial suggest? Commented Sep 15, 2018 at 5:20
  • I don't think I understand what you're actually trying to do here. Why would you need to save the file name in a separate input field when you already get the name in the $_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. Commented Sep 15, 2018 at 5:26

1 Answer 1

2

You cant do it just by php, because php update your page after request and response if you like to get the file name and file in a request you can use java script like this :

$("#imagefile").change(function(){
  $("#imagename").val(this.files[0].name);
}); 

and change your code like this :

<input id="imagefile" name="image" type="file" placeholder="Select photo">
<input id="imagename" type="hidden" value="<?php echo $file_name ?>" name="photo" />
<button type="submit" name="add">ADD NEW ITEM</button>

you can read more at this link :

How to display file name for custom styled input file using jquery?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the code dude! But most important, thanks for the explanation. Now i undersand why it wasn't working before.
This seems pretty unnecessary. You already have the name in the $_FILES['image']['name'] when you post the form?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.