Page 1 of 1

file upload box

Posted: Sat Jun 20, 2009 3:48 pm
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

Re: file upload box

Posted: Sat Jun 20, 2009 5:37 pm
by requinix
Can you see the part of the code that has those restrictions?

Re: file upload box

Posted: Sat Jun 20, 2009 5:45 pm
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

Re: file upload box

Posted: Sat Jun 20, 2009 8:41 pm
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.

Re: file upload box

Posted: Sun Jun 21, 2009 6:44 am
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";
  }
?>