Jump to content

How to make file restriction


Warptweet

Recommended Posts

Umm... somebody tried helping before, but it didnt work.

How can I add a file extension restriction to this code?
I know this has been done a million times, but I dont know where to add a file extension restriction code in this... can you help me?

[code]<head><title>Warptweet.com - Uploader</title>
<style type="text/css">
<!--
body {background-color: white; 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"] . "
";
  }
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
      {
      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[/url]</center>
</body>
</html> [/code]
Link to comment
Share on other sites

What kind of files are you wanting to accept? Judging a file by its extension is not secure. If you only want to allow images your best to use [url=http://php.net/getimagesize]getimagesize[/url], this will allways return false if the file is not an image.
Link to comment
Share on other sites

Well all you need do is check the extension against an array of extensions. Something like....

[code]
<?php
  $allowed = array('gm6','gm5','gmd','zip');
  foreach($_FILES as $file) {
    if (!inarray(substr($file['type'],-3,3),$allowed)) {
      // file not allowed.
    }
  }
?>
[/code]
Link to comment
Share on other sites

You could probably put it in the [code=php:0]else[/code] section below the [code=php:0] if($_FILES['file']['error'] > 0) [/code]

So if there were no file errors, then you look through the files that were uploaded and check to see if any were invalid extension.

If they were all the correct extention, then you can do you [code=php:0]move_uploaded_file()[/code] to move the tempoary file to the correct location.
Link to comment
Share on other sites

Errrrr.... *cough*......

les de intelligance language?

Ummmm....
I dont get it.
Considering that code has the

<? php

and ends with the

?>

it kind confuses me what to take away and on, can you please do it for me?
I'm REALLY sorry, but at least all it takes is to copy and paste it onto my code. :D
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.