Page 1 of 1

form question

Posted: Wed Jun 09, 2010 4:00 am
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");

Re: form question

Posted: Wed Jun 09, 2010 4:50 am
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
}

Re: form question

Posted: Wed Jun 09, 2010 10:01 am
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']))

Re: form question

Posted: Wed Jun 09, 2010 4:51 pm
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.

Re: form question

Posted: Wed Jun 09, 2010 8:09 pm
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.