file upload box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coolsek
Forum Newbie
Posts: 4
Joined: Sat Jun 20, 2009 3:40 pm

file upload box

Post by coolsek »

hello if anybody wants to hep me im having trouble writing an php script for upload box
http://w3schools.com/php/php_file_upload.asp i had found some instructions on that page

Code: Select all

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>
when i use that code the file is not saved

Code: Select all

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  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";
  }
?>
when im using this code it works and the file is uploaded to a folder called upload on the server but i do not want restrictions for the upload the onl restriction that i want is filess below 2 gb that can be uploaded
i dah played witht he script but i cant gte it wo work and i suck in php scripts

please help if you can
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file upload box

Post by requinix »

Can you see the part of the code that has those restrictions?
coolsek
Forum Newbie
Posts: 4
Joined: Sat Jun 20, 2009 3:40 pm

Re: file upload box

Post by coolsek »

tasairis wrote:Can you see the part of the code that has those restrictions?

Code: Select all

# if ((($_FILES["file"]["type"] == "image/gif")
# || ($_FILES["file"]["type"] == "image/jpeg")
# || ($_FILES["file"]["type"] == "image/pjpeg"))
# && ($_FILES["file"]["size"] < 20000))
is this what you mean i tried revoming this but still does not work
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: file upload box

Post by requinix »

You can't remove the whole thing - just the parts you don't want.

The first three lines restrict the upload to those types of images. The fourth line has to do with the file size. Look at it this way:

Code: Select all

if (
    (
        ($_FILES["file"]["type"] == "image/gif") ||
        ($_FILES["file"]["type"] == "image/jpeg") ||
        ($_FILES["file"]["type"] == "image/pjpeg")
    ) &&
    ($_FILES["file"]["size"] < 20000)
)
Now remove the parts that have to do with images and change the file size limit (the number is the size in bytes, so 20000 is about 20KB).

Code: Select all

if (
    (
    ) &&
    ($_FILES["file"]["size"] <= 2147483647)
)
Remove the extra () and the && you don't need and you're done.

Code: Select all

if (
    ($_FILES["file"]["size"] <= 2147483647)
)
You could even remove the second set of ()s if you wanted.
coolsek
Forum Newbie
Posts: 4
Joined: Sat Jun 20, 2009 3:40 pm

Re: file upload box

Post by coolsek »

i managed to get this and it works for text files and more

thank you

Code: Select all

<?php
if (
    ($_FILES["file"]["size"] < 20000))
  {
  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";
  }
?>
Post Reply