form file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kshitizgp
Forum Newbie
Posts: 10
Joined: Sat Dec 03, 2011 12:56 am

form file

Post by kshitizgp »

:?: the form code is

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="name">Name:</label><br />
<input type="text" name="name" value="" /><br />
<label form="email">Email:</label><br />
<input type="text" name="email" value="" /><br />
<label form="homework">Class notes:</label><br />

<input type="file" name="homework" value="Choose the file u want to upload" /><br />
<input type="submit" name="submit" value="Submit Homework" />
</form>


///
upload_file.php

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?




am getting this error

Notice: Undefined index: file in C:\wamp\www\my_phpinfo\upload_file.php on line


??
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: form file

Post by Bind »

in your php script, what you ahve as ["file"] needs to be the name value in your file upload form for the file, in this case ["homework"], or visa versa.
kshitizgp
Forum Newbie
Posts: 10
Joined: Sat Dec 03, 2011 12:56 am

Re: form file

Post by kshitizgp »

can some1 give me an example ..... where i can create a user and passwords and access that in sesssion ....please am reading session examples but cant get a hang of it ....a simple example will do fine ? form + access using session ;)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: form file

Post by social_experiment »

Set your session variables:

Code: Select all

<?php
 // to use sessions you need to call this at the top of
 // your scripts
 session_start();

 // set a session variable
 $_SESSION['username'] = $_POST['name'];
?>
And on other pages will be:

Code: Select all

<?php
 session_start();

 // will contain whichever value was in $_POST['name']
 echo $_SESSION['username'];
?>
Passwords shouldn't be stored in sessions; unless they are vital to your script, but off hand i can't think of an example where that would be needed.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kshitizgp
Forum Newbie
Posts: 10
Joined: Sat Dec 03, 2011 12:56 am

Re: form file

Post by kshitizgp »

@ social experiment

Thnx alot :D muahh :P ..
Post Reply