0

I have been trying to find a thread that could help, and have tried everything I could find to fix this.

I have a page that uploads a file, can be any type, and it works. I decided to use that same functionality on another page, but the $_FILES array is always empty.

Form:

 <form method="post" class="mainForm" enctype="multipart/form-data">
     <fieldset>
        <div class="widget first">
           <div class="rowElem">
               <label for="file">Upload Profile Picture</label>
               <div class="formRight">
                   <input type="file" id="profilepicture" name="profilepicture" />
                   <button formaction="profile_pic.php"  class="greyishBtn">Upload</button>
               </div>
           </div>
      </fieldset>
  </form>

PHP:

$name_first = "John";
$name_last = "Doe";
$folder_name = $name_last . "-" . $name_first . "-ID-" . $id . "/";

$dirname = "profile/" . $folder_name;
if(!is_dir($dirname)){
    mkdir($dirname);
}

$dirname = $dirname .  $_FILES['file']['name'];

if(move_uploaded_file($_FILES['file']['tmp_name'], $dirname)){
    header("location:profile.php");
}
else{
    echo $dirname; 
} 

?>

The echo $dirname just shows the folder with no file name.

11
  • There are two ways to submit the form, one is using <form action="**"> the other is having it in the button. Through most of my site, I'm using the button tag instead of the <input type="submit"> and have styled the button tags a certain way. Commented Aug 1, 2013 at 1:19
  • This <button formaction is invalid. At the least put a space between form and action. Commented Aug 1, 2013 at 1:24
  • <form action="**">.... really? Well I learned something new today, in my 15 years with working with forms, wow. Pardon the sarcasm, but you're wrong there Jack. There are many ways of submitting a form, and what you said, is NOT one of them, I can guarantee you that. Do what you want, hope it works. How, I have no idea. Good luck. Commented Aug 1, 2013 at 1:26
  • It is not invalid, and there is not space between it. validator.w3c.org did not show any errors. Thank you. Commented Aug 1, 2013 at 1:30
  • I'm glad you have such extensive experience Fred, but regardless of what you say, the form submits using that. Commented Aug 1, 2013 at 1:35

2 Answers 2

4

Well, your input file is called name='profilepicture'

Try:

$dirname = $dirname .  $_FILES['profilepicture']['name'];

and

if(move_uploaded_file($_FILES['profilepicture']['tmp_name'], $dirname)){
 header("location:profile.php"); }
Sign up to request clarification or add additional context in comments.

Comments

2

Try this code:

$name_first = "John";
$name_last = "Doe";
$folder_name = $name_last . "-" . $name_first . "-ID-" . $id . "/";

$dirname = "profile/" . $folder_name;
if(!is_dir($dirname)){
    mkdir($dirname);
}

$dirname = $dirname .  $_FILES['profilepicture']['name'];

if(move_uploaded_file($_FILES['profilepicture']['tmp_name'], $dirname)){
    header("location:profile.php");
}
else{
    echo $dirname; 
} 

3 Comments

What's different with this?
Explanation to the OP for changes/modifications? Would help for the OP to know and why the code didn't work in the first place (wink)
I already knew why he said that as soon as I saw it. Didn't think about that, but it made sense as soon as I saw it.

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.