Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Jackie Jen
Jackie Jen
2,723 Points

<input type="file" name="attachment"> warning Undefined index: attachment

<form action="1st_page.php"  method="post" >
<input type="file" name="attachment" id="attachment" >
<input class="button" type="submit" name="submit" value="Submit" />

below is 1st_page.php

$_SESSION['attachment'] = trim ($_POST['attachment']);

At 1st the input type =file i would like to allow user to attach file or picture whenever they like. But when user did not upload the file which mean the $_POST['attachment'] doest not have any data/string therefore i have warning on 1st-page.php Undefined index: attachment.

How can i get rid of this warning?

2 Answers

do it like this <?php if(isset($_POST['attachment'])) { $_SESSION['attachment'] = trim ($_POST['attachment']); } ?>

Try the above and see if it works.

Jackie Jen
Jackie Jen
2,723 Points

why i didn't think of that... anyway thanks a lot andreas