Zhaf
Zhaf

Reputation: 1884

PHP Upload Form

I want to build a working upload file form and I use codes in this tutorial - http://www.w3schools.com/PHP/php_file_upload.asp

Everything works well. The only problem is, the original code only includes file type of gif and jpg. I need png also.

What I did is modified this lines :

    > if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))

to this :

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
|| ($_FILES["file"]["type"] == "image/png"))

but it turns error. Any advice?

Upvotes: 0

Views: 226

Answers (3)

Damiqib
Damiqib

Reputation: 1077

You could modify it like this to make it look nicer and easier to modify later on:

if(in_array($_FILES['file']['type'], array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png'))

Now if you need to add more image types, you can just add a value to the array.

Upvotes: 0

There is a syntax error in the "if" statement:

Change

if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) || ($_FILES["file"]["type"] == "image/png"))

to:

if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png"))

Upvotes: 0

Kinjal Dixit
Kinjal Dixit

Reputation: 7935

remove the extra ) after pjpeg to make it look like this:

if (($_FILES["file"]["type"] == "image/gif") || 
($_FILES["file"]["type"] == "image/jpeg") || 
($_FILES["file"]["type"] == "image/pjpeg") || 
($_FILES["file"]["type"] == "image/png"))

There is also an extra ( before the IF which I have removed, but that could be a copy paste problem from you.

Upvotes: 5

Related Questions