form question

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
anishgiri
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 12:49 am

form question

Post by anishgiri »

<input type="file" name="varname" >

In the html code above is it possible to test the value of name like this?

Code: Select all

 if (isset($_POST['varname']))

I know with input type text the code above is correct but with input type file, is the condition above correct?

Below is the code for file upload

Code: Select all

		$newname = uniqid("whc").".jpg";
move_uploaded_file($_FILES['mailfile']['tmp_name'],
"images/$newname");
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: form question

Post by cpetercarter »

isset() tests whether a variable exists and has a none-NULL value. So :

Code: Select all

if (isset($_POST['varname']))  {
           // do something
}
will run the "do something" part of the script only if the $_POST array contains an offset "varname" and the value of $_POST['varname'] is not NULL. It is often a good idea to test whether a variable is set, in order to avoid error messages if the script finds a case where the variable does not exist.

If you want to test whether a file exists and was uploaded via HTTP POST, you can use the is_uploaded_file() function.

Code: Select all

if (is_uploaded_file($_FILES['mailfile']['tmp_name'])) {
          // do something
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: form question

Post by AbraCadaver »

In the case of most form controls you'll want to check if it is empty because if you submit a blank form you will still have vars that are set:

Code: Select all

 if (isset($_POST['varname']) && !empty($_POST['varname']))
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: form question

Post by cpetercarter »

A variable is empty if it isn't set; or if it has a zero or NULL or FALSE or "" or "0" value. So you don't need to test for empty() and for isset(). Testing for empty() alone will do.
Last edited by cpetercarter on Thu Jun 10, 2010 1:38 am, edited 1 time in total.
anishgiri
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 12:49 am

Re: form question

Post by anishgiri »

cpetercarter wrote:
If you want to test whether a file exists and was uploaded via HTTP POST, you can use the is_uploaded_file() function.

Code: Select all

if (is_uploaded_file($_FILES['mailfile']['tmp_name'])) {
          // do something
}
Thanks for the response guys. The above code is the condition that I am looking for.
What I want is if I click the submit botton and I did not click the browse button, it should not replace the old image path with a new one.
The reason why I asked the question above is because I tried these conditions

Code: Select all

if (isset($_POST['mailfile']) && !empty($_POST['mailfile']))
{

$newname = uniqid("whc").".jpg";
move_uploaded_file($_FILES['mailfile']['tmp_name'],
"images/$newname");

	mysql_query("UPDATE table1 SET imagepath='images/$newname' WHERE id='$id'") 
	or die(mysql_error()); 
}
On the code above if a file was uploaded on a folder (for editing), the path is not inserted on sql, but the file was uploaded on a folder. And when I click the submit botton (I did not click the file browse button) it replace the old path on sql.

Code: Select all

if (isset($_FILES['mailfile']) && !empty($_FILES['mailfile']))
{

$newname = uniqid("whc").".jpg";
move_uploaded_file($_FILES['mailfile']['tmp_name'],
"images/$newname");

	mysql_query("UPDATE table1 SET imagepath='images/$newname' WHERE id='$id'") 
	or die(mysql_error()); 
}
In this condition when I click the submit botton (I did not click the file browse button) it also replace the old image path with a new one on sql. But here when I click the browse button to upload a new image to replace the old one, it uploads the image on the file, it also save the path of that file to sql.
Post Reply