Jump to content

Check file name when uploading


Cory94bailly

Recommended Posts

My code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Free ET File Redirect</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
if(isset($_POST['submit'])) {
$url = $_POST['location'];
if (($_FILES["file"]["type"] == ".pk3")
&&($_FILES["file"]["size"] < 52428800))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "File Name: " . $_FILES["file"]["name"] . "<br />";

    if (file_exists("$url" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
  

  move_uploaded_file($_FILES["file"]["tmp_name"],
      "$url/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../uploaded/" . $_FILES["file"]["name"];
      }
    }
}
else
{
echo "Invalid file";
}
}
?>
<h1>Free ET File Redirect</h1>
<br>
<h3>Welcome to the <i><u>Free ET File Redirect</u></i>!</h3>
<br>
<h4>To view currently uploaded files, click a link below:</h4>
<br>
<a href="etmain">EtMain</a>
<br>
<a href="etpub">EtPub</a>
<br>
<a href="etpro">EtPro</a>
<br>
<a href="jaymod">JayMmod</a>
<br>
<a href="noquarter">NoQuarter</a>
<br><br>
<h2><font color="red">Currently down:</font></h2>
<h4>To upload a file, choose which file you want it to be in, browse for it, then click <b>Upload</b>.</h4>
<br>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"
enctype="multipart/form-data">
<b>Filename:</b>
<input type="file" name="file" /> 
<br />
<b>File Location:</b><br />
EtMain: <input type="radio" name="location" value="etmain" />
<br />
EtPub: <input type="radio" name="location" value="etpub" />
<br />
EtPro: <input type="radio" name="location" value="etpro" />
<br />
JayMod: <input type="radio" name="location" value="jaymod" />
<br />
NoQuarter: <input type="radio" name="location" value="noquarter" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

 

 

Well I'm just trying to make it so only files with the extension ".pk3" can be uploaded and it uploads it to the selected radio button..

 

But I don't think ".pk3" has a MIME type soo I need to know how to physically check the file extension before it gets uploaded and only allow .pk3.

 

 

I saw that w2box has this ability but idk how. :/

Link to comment
https://forums.phpfreaks.com/topic/112482-check-file-name-when-uploading/
Share on other sites

I use this... That way you can define as many extensions as you like...

 

$pattern="(\.csv$)|(\.CSV$)|(\.xls$)|(\.XLS$)";
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))	{
if(eregi($pattern, $file))
###################################

###################################
}
closedir($handle);
}

$path = pathinfo('somerandomfilename.pk3');
echo $path['extension'];

 

Huh???

 

 

I use this... That way you can define as many extensions as you like...

 

$pattern="(\.csv$)|(\.CSV$)|(\.xls$)|(\.XLS$)";
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))	{
if(eregi($pattern, $file))
###################################

###################################
}
closedir($handle);
}

 

 

Well, I want it to check the file type AS it uploads and if it's not a .pk3, don't upload it..

$path = pathinfo('somerandomfilename.pk3');
echo $path['extension'];

 

Huh???

 

 

I use this... That way you can define as many extensions as you like...

 

$pattern="(\.csv$)|(\.CSV$)|(\.xls$)|(\.XLS$)";
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))	{
if(eregi($pattern, $file))
###################################

###################################
}
closedir($handle);
}

 

 

Well, I want it to check the file type AS it uploads and if it's not a .pk3, don't upload it..

Then you need to use javascript. But to implement it into the above:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Free ET File Redirect</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
if(isset($_POST['submit'])) {
$url = $_POST['location'];
$pathinfo = pathinfo($_FILES["file"]["name"]);
if (($pathinfo['extension'] == "pk3")
&&($_FILES["file"]["size"] < 52428800))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "File Name: " . $_FILES["file"]["name"] . "<br />";

    if (file_exists("$url" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {



  






  move_uploaded_file($_FILES["file"]["tmp_name"],
      "$url/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../uploaded/" . $_FILES["file"]["name"];
      }
    }
}
else
{
echo "Invalid file";
}
}
?>
<h1>Free ET File Redirect</h1>
<br>
<h3>Welcome to the <i><u>Free ET File Redirect</u></i>!</h3>
<br>
<h4>To view currently uploaded files, click a link below:</h4>
<br>
<a href="etmain">EtMain</a>
<br>
<a href="etpub">EtPub</a>
<br>
<a href="etpro">EtPro</a>
<br>
<a href="jaymod">JayMmod</a>
<br>
<a href="noquarter">NoQuarter</a>
<br><br>
<h2><font color="red">Currently down:</font></h2>
<h4>To upload a file, choose which file you want it to be in, browse for it, then click <b>Upload</b>.</h4>
<br>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"
enctype="multipart/form-data">
<b>Filename:</b>
<input type="file" name="file" /> 
<br />
<b>File Location:</b><br />
EtMain: <input type="radio" name="location" value="etmain" />
<br />
EtPub: <input type="radio" name="location" value="etpub" />
<br />
EtPro: <input type="radio" name="location" value="etpro" />
<br />
JayMod: <input type="radio" name="location" value="jaymod" />
<br />
NoQuarter: <input type="radio" name="location" value="noquarter" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Thanks, that works almost perfectly..

 

Now it doesn't check if it already exists...

 

 

(Oh and if someone wants to work on it, I updated it a tiny bit..)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Free ET File Redirect</title>
</head>

<body>
<?php
if(isset($_POST['submit'])) {
$url = $_POST['location'];
$pathinfo = pathinfo($_FILES["file"]["name"]);
if (($pathinfo['extension'] == "pk3")
&&($_FILES["file"]["size"] < 52428800))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "File Name: " . $_FILES["file"]["name"] . "<br />";

    if (file_exists("$url" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
  move_uploaded_file($_FILES["file"]["tmp_name"],
      "$url/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "$url/" . $_FILES["file"]["name"];
      echo "<meta http-equiv='refresh' content='2'>";
      }
    }
$filename = $_FILES["file"]["name"];
$ipaddress = ($_SERVER['REMOTE_ADDR']);
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
@mysql_query("INSERT INTO recent (filename, ipaddress, hostname) VALUES ('$filename', '$ipaddress', '$hostname')");
die();
}
else
{
die("Sorry, only <i>.pk3</i>s are allowed!<meta http-equiv='refresh' content='2'>");
}
}
?>
<h1>Free ET File Redirect</h1>
<br>
<h3>Welcome to the <i><u>Free ET File Redirect</u></i>!</h3>
<br>
<h4>To view currently uploaded files, click a link below:</h4>
<br>
<a href="etmain">EtMain</a>
<br>
<a href="etpub">EtPub</a>
<br>
<a href="etpro">EtPro</a>
<br>
<a href="jaymod">JayMod</a>
<br>
<a href="noquarter">NoQuarter</a>
<br><br>
<h2><font color="red">Currently down:</font></h2>
<h4>To upload a file, choose which file you want it to be in, browse for it, then click <b>Upload</b>.</h4>
<br>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"
enctype="multipart/form-data">
<b>Filename:</b>
<input type="file" name="file" />
<br />
<b>Allowed file types:</b> <i>.pk3</i>
<br />
<br />
<b>File Location:</b><br />
EtMain: <input type="radio" name="location" value="etmain" />
<br />
EtPub: <input type="radio" name="location" value="etpub" />
<br />
EtPro: <input type="radio" name="location" value="etpro" />
<br />
JayMod: <input type="radio" name="location" value="jaymod" />
<br />
NoQuarter: <input type="radio" name="location" value="noquarter" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

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.