tags:

views:

67

answers:

6
if ( ( ($_FILES["file"]["type"] == "image/gif") ||
       ($_FILES["file"]["type"] == "image/jpeg") || 
       ($_FILES["file"]["type"] == "image/pjpeg") ) && 
     ($_FILES["file"]["size"] < 20000))
   {
      header ("Location: index.html");
   }

i am using the above code to redirect the page to index.html if user has uploaded any image. but it is not working. the page is not getting redirected. can anybody help me?

Thanks in advance

+1  A: 

if user has uploaded any image. but it is not working. the page is not getting redirected. can anybody help me?

That's very vague - what are they uploading? How do you know its not working?

if user has uploaded any image

No - only if the users browser thinks that the file is one of the 3 types you've listed.

symcbean
the user are supposed to submit pdf or word file. the page remains blank instead of redirecting the page
Rachel
maybe your if-statement actually works, the conditions are just not met
xor_eq
@Rachel: If the user is supposed to submit pdf or word file, why are you checking for gif, jpeg and pjpeg?
Svish
actually when i was testing i tried to upload jpg file and it accepted it thats why i am checking this condition
Rachel
@Rachel: And what content-type was sent by your browser? (note you should NEVER assume that data supplied the client is valid)
symcbean
A: 

PHP: header - manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

maro
as you can see in my code no output is being sent before header()
Rachel
A: 

Given this comment...

the user are supposed to submit pdf or word file. the page remains blank instead of redirecting the page

This statement checks that the image is a gif, jpeg or pjpeg and if it IS one of these it redirects. As the image will be of type pdf or doc, it won't satisfy the condition.

if ( ( ($_FILES["file"]["type"] == "image/gif") ||
       ($_FILES["file"]["type"] == "image/jpeg") || 
       ($_FILES["file"]["type"] == "image/pjpeg") ) && 
     ($_FILES["file"]["size"] < 20000))
   {
      header ("Location: index.html");
   }

I would suggest that you want to either remove this if statement entirely and just use the "header(..." line - or you want to check that the file is the correct type and display a message telling people that it isn't...

if ($_FILES["file"]["type"] == "application/pdf") {
    header ("Location: index.html");
} else {
    echo 'Incorrect File Type';
}
Sohnee
how image can be ot type pdf or doc? i am saying that if user is uploading image that he should be redirected to home page else to the next page
Rachel
A: 

Does the Redirect work, without the if-condition? Maybe it's not the if-condition which is not working, but some mistake within your redirect-command. This sometimes is kinda weird, with header('Location: ...');

faileN
yes the redirect works without if condition
Rachel
okay this way, we know, that it is really the if-condition that fails. Try to remove single conditions in your if-condition to see, which of the conditions fail and which not. This way we get to know where exactly to search for an error.
faileN
A: 

$_FILES["file"]["type"] depends on what the client sends to the server.
Use getimagesize() to determine the mimetype on the server.

Invoke the exit(); after the redirect.
Because the script keeps running and could modify the HTTP status from 301(redirect) back to something like 200, 404, 500, etc.

You could further debug your if statement by adding:

  // ...
  header ("Location: index.html");
  exit();
} else {
  var_dump($_FILES["file"]);
}
Bob Fanger
A: 

thanks a lot everyone i have got the solution. actually i was missing out to complete a brace

Rachel