Jump to content

File Upload - Add File Restriction Please!


Warptweet

Recommended Posts

I know this has been easily done many times...
And I've even read the file upload tutorials, BUT.... I don't know WHERE in this code to put it...
What code do I put in, and WHERE do I put the code in, to make it so that the only file types allowed to be uploaded on this code are .gm6,.gm5, .gmd, and .exe?
So basically, where do I put a code for making file restrictions?

CODE:
[sub]<html>
<head><title>Warptweet.com - Uploader</title>
<style type="text/css">
<!--
body {background-color: black; color: white;}
a {color: white;}
a:hover {font-size: 110%;}
-->
</style>
</head>
<body>
<div align="center">
<fieldset>
<legend>Warptweet.com File Upload</legend>
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Filename: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  }
if (file_exists("filer/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "filer/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "http://www.warptweet.com/wh/filer/" . $_FILES["file"]["name"];
      }
?>
<center><a href="index.htm"><--Back Home</a></center>
</body>
</html>[/sub]
Link to comment
https://forums.phpfreaks.com/topic/31780-file-upload-add-file-restriction-please/
Share on other sites

Try this

[code]
<html>
<head><title>Warptweet.com - Uploader</title>
<style type="text/css">
<!--
body {background-color: black; color: white;}
a {color: white;}
a:hover {font-size: 110%;}
-->
</style>
</head>
<body>
<div align="center">
<fieldset>
<legend>Warptweet.com File Upload</legend>
<?php

$allowed_files = array(
'.ext1',
'.ext2',
'.ext3'    // Change these to match what you want
);


if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "
";
  }
else
  {
  echo "Filename: " . $_FILES["file"]["name"] . "
";
  echo "Type: " . $_FILES["file"]["type"] . "
";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
  }
if (file_exists("filer/" . $_FILES["file"]["name"]))
{
  echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
if(in_array(substr($_FILES["file"]["name"], -1, 4), $allowed_files)) {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "filer/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "http://www.warptweet.com/wh/filer/" . $_FILES["file"]["name"];
}
}
?>
<center><a href="index.htm"><--Back Home</a></center>
</body>
</html>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.