1

I created a simple form, to create a post, that has three inputs:

  • One for the title
  • Description
  • Image

So, when I submit my form (using post) I call a php file, that "echoes" the value from each input.

It works just fine, but when I try to call the php function $_FILES['my_input_name']['tmp_name'], on my file input, I get an error saying:

Undefined index: my_input_name

My form looks like this (shorter version):

<form action="processForm.php" method="post">
  <input type="text" name="title" class="input" required>
  <textarea id="description" name="description"required></textarea>
  <input type="file" name="fileMedia">
</form>

My php file looks like this

$method = $_SERVER[ 'REQUEST_METHOD' ];

if ( $method=='POST') {
    $_args = $_POST;
    $_INPUT_METHOD = INPUT_POST;
}
elseif ( $method=='GET' ) {
    $_args = $_GET;
    $_INPUT_METHOD = INPUT_GET;
}
else {
    exit(-1);
}

$title = $_args['title'];
$description = $_args['description'];
$mediaName = $_args['fileMedia'];
$mediatmpPath = $_FILES["fileMedia"]["tmp_name"];

echo $title."<br>";
echo $description."<br>";
echo $mediaName."<br>";
echo $mediatmpPath ."<br>";

I have no idea of what I'm doing wrong, so any helped would be really apreciated!

P.s: My form's is really reduced. In the original one I have row, cols, divs, etc, and some other inputs, which I did not find relevant for this question

8
  • 1
    Have a read through stackoverflow.com/a/3587158/1213708 for some ideas. Commented Jul 3, 2021 at 15:07
  • You need to add enctype <FORM enctype="multipart/form-data" ..... > Commented Jul 3, 2021 at 15:07
  • @KenLee I did what you said and it did solve one of my problems, that I did not mencion (realpath() works now), however my main issue remains :( Commented Jul 3, 2021 at 15:37
  • What is your main issue ? Commented Jul 3, 2021 at 15:41
  • 1
    it should be $_FILES["fileMedia"]["tmp_name"] (since in your form the input file has a name of "fileMedia") -- Your code is: <input type="file" name="fileMedia"> Commented Jul 3, 2021 at 15:58

2 Answers 2

1

You just need to add multipart = "form/data" in form tag

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

Comments

0

You need to add this below line in <form> tag

<form action="processForm.php" method="post" enctype='multipart/form-data'>
   <input type="text" name="title" class="input" required>
   <textarea id="description" name="description"required></textarea>
   <input type="file" name="fileMedia">
   <input type="submit" name="save" value="save">
</form>

And below post data code:

<?php $method = $_SERVER[ 'REQUEST_METHOD' ];

    if ( $method=='POST') {
      $_args = $_POST;
      $_INPUT_METHOD = INPUT_POST;
    }
    elseif ( $method=='GET' ) {
      $_args = $_GET;
      $_INPUT_METHOD = INPUT_GET;
    }
    else {
     exit(-1);
    }

    $title = $_args['title'];
    $description = $_args['description'];
    $mediaName = $_FILES["fileMedia"]["name"];
    $mediatmpPath = $_FILES["fileMedia"]["tmp_name"];

    echo $title."<br>";
    echo $description."<br>";
    echo $mediaName."<br>";
    echo $mediatmpPath ."<br>";
   ?>

I think this help you.

Comments

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.