Jump to content

Upload script


Drongo_III

Recommended Posts

Hi Guys

 

Can someone see where I'm going wrong with this upload script. It's meant to only accept csv files.

 

when i try it out all i get is is the error "invalid file" but i'm uploading a csv file.

 

Upload form

<html>
<body>

<form action="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> 

 

Upload script

<?php
if ((($_FILES["file"]["type"] == "text/csv"))
&& ($_FILES["file"]["size"] < 200000))
  {
  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"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

 

Any help would be appreciated.

 

Drongo

Link to comment
Share on other sites

So the first if is failing, either ($_FILES["file"]["type"] == "text/csv") or ($_FILES["file"]["size"] < 200000) is not matching or both. Try var_dumping these variables to see what they contain to try to figure out why your if clause is failing.

Link to comment
Share on other sites

Hi TenDoLLA

 

I did as you suggested - didn't know that existed. But the result has left me a little confused.

 

I did :

 

var_dump($_FILES["file"]["type"]);

echo "<br/><br/>";
var_dump($_FILES["file"]["size"]);

 

And got the result

 

string(24) "application/vnd.ms-excel"

int(50) 

 

Which didn't mean a whole lot to me. I would love an explanation of that.

 

Any suggestions based on the var dump?

 

Thanks

 

Drongo

 

 

So the first if is failing, either ($_FILES["file"]["type"] == "text/csv") or ($_FILES["file"]["size"] < 200000) is not matching or both. Try var_dumping these variables to see what they contain to try to figure out why your if clause is failing.

Link to comment
Share on other sites

From http://www.php.net/manual/en/features.file-upload.post-method.php :

You could use the $_FILES['userfile']['type'] variable to throw away any files that didn't match a certain type criteria, but use this only as first of a series of checks, because this value is completely under the control of the client and not checked on the PHP side.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.