Jump to content

Recommended Posts

Thanks, I'll give that a shot =]
BTW, this is only for me to upload, so it's okay if it's not secure.
[quote author=ChaosXero link=topic=101250.msg400459#msg400459 date=1153406292]
Not entirely sure but you could:
[code]<?
$filename = $_POST['file'];
$ftype = explode(".", $filename);
switch ($ftype['1']){
case ".php":
//etc
case ".png":
//etc
}
[/code]
[/quote]
Don't rely only on the file extension.

Unscrupulous people will change them on you to upload malicious code and "unwanted" files on your server.

A better method is to use an array of MIME filetypes and their associated extensions. This gives you two points of security that every file uploaded must adhere to in order to be considered valid. Like so:

[code]
<?php
$valid_files = array();
$valid_files[0] = array("image/png", "png");
$valid_files[1] = array("image/jpeg", "jpg");

$filename = $_POST['file'];
$ftype = $_POST['file']['type'];
$fext = explode(".", $filename);
// Since some people use "." in their filenames, we'll take the last item in the $fext array
$fext = $fext[count($fext)-1];

// Set a variable to flag if we find a valid file. Set to 0 by default (not valid until we verify)
$file_is_valid = 0;

foreach($valid_files as $key => $value){
  // $value[0] is the MIME type
  // $value[1] is the file extension
  if( ($value[0] == $ftype) && ($value[1] == $fext) ){
      $file_is_valid = 1;
  }
}

if( $file_is_valid = 1 ){
  // Upload file
} else {
  // Error out. File is not allowed
}
?>
[/code]
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.