Help with Image/$_FILES Handling please

Help with Image/$_FILES Handling please

am 15.11.2007 12:46:36 von skulkrinbait

I've a HTML form that allows a user to specify the location to upload
a file from:



I then want to check if the user entered something or not as this
isn't mandatory. Here's an excerpt of the relavent code:

if (isset($_POST['submit']))
{
if (isset($_FILES['imagefile']))
{
if ($_FILES['imagefile']['type'] == "image/gif")
{
copy ($_FILES['imagefile']['tmp_name'],
"images/".$_FILES['imagefile']['name'])
or die ("Could not copy");

The problem is the line: if (isset($_FILES['imagefile'])) isn't
doing what I expect it to do, it is always returning true and
executing the code in the if construct.

How can I solve this issue so that if the user isn't submitting
anything to be uploaded that it doesn't execute that code and vice
versa?

Thanks.

Re: Help with Image/$_FILES Handling please

am 15.11.2007 13:14:28 von igrosny

On 15 nov, 08:46, "skulkrinb...@googlemail.com"
wrote:
> I've a HTML form that allows a user to specify the location to upload
> a file from:
>
>


>
> I then want to check if the user entered something or not as this
> isn't mandatory. Here's an excerpt of the relavent code:
>
> if (isset($_POST['submit']))
> {
> if (isset($_FILES['imagefile']))
> {
> if ($_FILES['imagefile']['type'] == "image/gif")
> {
> copy ($_FILES['imagefile']['tmp_name'],
> "images/".$_FILES['imagefile']['name'])
> or die ("Could not copy");
>
> The problem is the line: if (isset($_FILES['imagefile'])) isn't
> doing what I expect it to do, it is always returning true and
> executing the code in the if construct.
>
> How can I solve this issue so that if the user isn't submitting
> anything to be uploaded that it doesn't execute that code and vice
> versa?
>
> Thanks.

Hi, try this

if (isset($_FILES['imagefile']['name']))

hope it helps

Re: Help with Image/$_FILES Handling please

am 15.11.2007 13:41:46 von luiheidsgoeroe

On Thu, 15 Nov 2007 12:46:36 +0100, skulkrinbait@googlemail.com =

wrote:

> I've a HTML form that allows a user to specify the location to upload
> a file from:
>
>


>
> I then want to check if the user entered something or not as this
> isn't mandatory. Here's an excerpt of the relavent code:
>
> if (isset($_POST['submit']))
> {
> if (isset($_FILES['imagefile']))
> {
> if ($_FILES['imagefile']['type'] == "image/gif=
")


Never ever trust a user given mime-type. Use exif_imagetype() or =

getimagesize() to check images.

> {
> copy ($_FILES['imagefile']['tmp_name'],
> "images/".$_FILES['imagefile']['name'])
> or die ("Could not copy");
>
> The problem is the line: if (isset($_FILES['imagefile'])) isn't
> doing what I expect it to do, it is always returning true and
> executing the code in the if construct.
>
> How can I solve this issue so that if the user isn't submitting
> anything to be uploaded that it doesn't execute that code and vice
> versa?

RTFM, UPLOAD_ERR_NO_FILE

if($_FILES['imagefile']['error']!=3D UPLOAD_ERR_OK){
switch($_FILES['imagefile']['error']){
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
//file was bigger then allowed
break;
case UPLOAD_ERR_PARTIAL:
//file was partially uploaded =

break;
case UPLOAD_ERR_NO_FILE:
//No file was uploaded.
break;
case UPLOAD_ERR_NO_TMP_DIR:
//Missing a temporary folder to store the upload
break;
case UPLOAD_ERR_CANT_WRITE:
//can't write upload to disk
case UPLOAD_ERR_EXTENSION:
//extention was blocked
break;
default:
//screeching halt. unkown error. abort, abort,mayday (m'aidez), alarm=
=

(a l'arme)
}
} else {
//everything fine and dandy
}

-- =

Rik Wasmus

Re: Help with Image/$_FILES Handling please

am 15.11.2007 14:02:45 von skulkrinbait

Many thanks, that solved the problem.