0

i'm on archlinux with lampp(last version) i learn php from w3c-school, i'm on page upload file and here my script can't upload nothing the only thing return me is Invalid File ever (.jpg .png ecc..) here is the code:

 <?php


include "config_db.php";

$name = "/upload/" . $_FILES['file']['name'];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = in_array(explode(".", $_FILES["file"]["name"]), $allowedExts);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      echo '<img src="upload/' . $_FILES["file"]["name"] . '">';
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
2
  • Count you post HTML code of your form as well? Commented Feb 28, 2013 at 4:19
  • First things first DO NOT learn from W3Schools see why at w3fools.com Commented Feb 28, 2013 at 6:26

6 Answers 6

1

If image name contains .(dot) then there is a problem. For example if the image name is like 12.212.jpg, when you explode the image name with .(dot) your extension will be .212. so in this case you need to use like below code.

$name = "/upload/" . $_FILES['file']['name'];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = explode(".", $_FILES["file"]["name"]);
$extension = end($extension);

It will give you the exact image format.

Sign up to request clarification or add additional context in comments.

Comments

0

Your are checking extension in incorrect way. Here is how it should be:

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
header ("Content-Type: text/plain");
if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/png")
  || ($_FILES["file"]["type"] == "image/pjpeg"))
  && ($_FILES["file"]["size"] < 100000)
  && in_array ($extension, $allowedExts)) echo "Success";
else echo "Error";

This code works fine for me together with the following form:

<form method="POST" enctype="multipart/form-data" action="upload.php">
  <input type="file" name="file" />
  <input type="submit" />
</form>

7 Comments

The code above works fine for me. Could you please show HTML code of your form?
<html> <body> <form action="check_upload.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="submit"> </form> </body> </html>
@user2041211 Could you insert print_r ($_FILES['file']) before if statement and show what will it print?
Array ( [name] => bubble.jpg [type] => image/jpeg [tmp_name] => /tmp/phpNas8q1 [error] => 0 [size] => 758414 )
@user2041211 Your file is invalid because it obviously violates $_FILES["file"]["size"] < 100000 rule.
|
0

Your error is in below line.

$extension = in_array(explode(".", $_FILES["file"]["name"]), $allowedExts);

Must be as below.

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

And also change if condition as below.

if (in_array($extension, $allowedExts))

1 Comment

i get invalid file changing the code.. maybe is a permission problem from linux?
0

change this condition

&& in_array($extension, $allowedExts))

to only this condition

&& $extension)

because you used in_array 2 times, 2nd one is unnecessary.

Comments

0

I think the problem is in in_array() function. You have already checked the extension in this line,

$extension = in_array(explode(".", $_FILES["file"]["name"]), $allowedExts);

Now $extension is value is TRUE.

So you cannot check this $extension in the if condition. Instead of that,

just check whether $extension is TRUE,

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000)
&& $extension)

2 Comments

i have try to print $extension and is return me "filejpg"
@user2041211 The method you are using to get extension is wrong. First please change that. Then check
0

I ran into the problem where if the extension was caps (i.e. image.JPG) it gave me invalid file. I used this code:

$t_image = strtolower($_FILES['file']['name']);

to solve the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.