Jump to content

PHP upload problem.


Recommended Posts

Hi,

I am using this code from w3schools

but it giving me error (Invalid file ).

 

This is the code

<?php$allowedExts = array("gif", "jpeg", "jpg", "png");$temp = explode(".", $_FILES["file"]["name"]);$extension = end($temp);if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/jpg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/x-png")|| ($_FILES["file"]["type"] == "image/png"))&& ($_FILES["file"]["size"] < 20000)// if i change this by ">" then it taking every size&& 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("rajesh/" . $_FILES["file"]["name"])) {      echo $_FILES["file"]["name"] . " already exists. ";    } else {      move_uploaded_file($_FILES["file"]["tmp_name"],      "rajesh/" . $_FILES["file"]["name"]);      echo "Stored in: " . "rajesh/" . $_FILES["file"]["name"];    }  }} else {  echo "Invalid file";}?>

Can you please check and give the correct code?

Thanks

Link to post
Share on other sites

If it says invalid file then that if statement is failing. So the mime type is not in the list, or the size is too big, or it has the wrong extension.

Hi,

that i know to but what i want to say, the whole code said that if the size go beyond 20000 then it will show invalid file but in this case every time it showing invalid file and if change it to > then its working fine but it taking every size either less then or more than 20000.

Thanks

Link to post
Share on other sites

I didn't get what you asking for?

Thanks

The markup is the HTML. I was hoping to see the form that you're (presumably) using to submit the file upload with.

Link to post
Share on other sites

the whole code said that if the size go beyond 20000 then it will show invalid file but in this case every time it showing invalid file

The size is not the only reason why it would show invalid file.
Link to post
Share on other sites

all these conditions are part of the same if block, who's else block outputs Invalid file

if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/jpg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/x-png")|| ($_FILES["file"]["type"] == "image/png"))&& ($_FILES["file"]["size"] < 20000)// if i change this by ">" then it taking every size&& in_array($extension, $allowedExts)) {

You need to debug all parts of your code so you can actually tell what values are what, and be able to follow what your code doing.

Link to post
Share on other sites

Hi i started with this code and had the same issue with it i have since manage to update the code to allow the file to uploaded to the server's tmp folder (however this is now where i am stuck as i am not allowed to move the file fue to hosting security reason and i am in the process of trying to find out how to change the server tmp location to a location with in my own account folder..Here is a link to the topic i had created.. in here you will be able to see the code as well.. Hope its helpshttp://w3schools.invisionzone.com/index.php?showtopic=50284

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...