Jump to content

Check to see if the uploaded file is a valid format.


TeroYukio

Recommended Posts

I got it able to upload, however I'm trying to restrict what type of files can be uploaded. (I want just .s2z files and .honmod files to be able to be uploaded.)  What should I use to restrict something like that because I tried this:

 

if ($uploaded_type =="text/x-generic") {
echo "Blah Blah Blah<br>";
$ok=0;
}

 

Which did not work.  What do you suggest?

 

<?php

$target = "/*/*/public_html/upload/";

$target = $target . basename( $_FILES['uploaded']['name']) ;

$ok=1;

if ($ok==0) {

echo "Sorry your file was not uploaded";

}else{

if(move_uploaded_file($_FILES['uploaded']['tmp_name'],$target)){

echo "The file <b>".basename( $_FILES['uploaded']['name'])."</b> has been uploaded";

}else{

echo "Sorry, there was a problem uploading your file."; 

}
}

?>

Link to comment
Share on other sites

First test with actual extension

 

$extensionorig = end(explode('.', $filename));
if ($extensionorig == 's2z')
{
   $ext = ".s2z";
} 
else if ($extensionorig == 'honmod')
{
   $ext = ".honmod";
} 
else 
{
echo "Invelid File Type. only .honmod, .s2z can be uploaded.";
        flag++;
}

 

Second test by reading  MIME Types

$mimetype = $_FILES['uploadedfile']['type'];
if($mimetype != 'application/x-whatever' || $mimetype != 'application/x-whatever2' )
{
$flag++;
}

 

Third test reading actual file.

 

if ($extensionorig == 'jar')  // For ex. examining a valid jar (Java) file
{
   $f = @fopen($_FILES['uploadedfile']['tmp_name'],'r'); // open the file
   $s = @fread($f,2); // read first two characters. 
   @fclose($f); 
   if($s != "PK"){

   if($flag == 0){
   $error = "<RED>The file you are attempting to upload does not appear to be a valid jar (java) game file.We do not allow you to upload this game.</RED>";
   }
   $flag++;
   }

}

Flag increment here shows error message

 

 

 

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.