Jump to content

Upload File Extensions


steve-t

Recommended Posts

Can anyone suggest why the following code wont allow you to upload PDF or Powerpoint files? It displays the error message no matter what filetype is uploaded.

[code]if (is_uploaded_file($_FILES['user_file']['tmp_name']))
{
  $array = explode(".", $_FILES[$userfile]['name']);
  $ext = $array[count($array)-1];
  if (!preg_match('/(pdf|ppt)/i', $ext))
            {
  $message="File is not a PDF or Powerpoint file. Please try another file.<br>";
    include("file_manager.inc");
      exit();
  }
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28961-upload-file-extensions/
Share on other sites

Is [code=php:0]$ext[/code] the value you expect it to be?

If yes, then the problem is with the preg_match().  If no, then the problem is with how $ext was set.  You can find out where it goes wrong by printing out the values of each variable that led to the value of $ext.
You could try a different approach to extract the extention of the file:

[code]
<?php
if (is_uploaded_file($_FILES['user_file']['tmp_name'])) {
if(!preg_match('/^((\w)(.[pdf|ppt]))$/', $_FILES[$userfile]['name']), $match = array()) {
  $message="File is not a PDF or Powerpoint file. Please try another file.<br>";
  include("file_manager.inc");
  exit();
}
$completeName = $match[0];
$filename = $match[1];
$ext = $match[2];
}
?>
[/code]
Cheesier Angel: Imagine this filename: mysql.db.php
That would make:
$completeName = "mysql.db.php";
$filename = "mysql";
$ext = "db";

[code]<?php
$filename = "mysql.db.php";

$array = explode(".",$filename);
$ext = $array[count($array)-1];

echo $ext; // outputs: php
?>[/code]
[quote author=Daniel0 link=topic=116816.msg476227#msg476227 date=1164896617]
Cheesier Angel: Imagine this filename: mysql.db.php
That would make:
$completeName = "mysql.db.php";
$filename = "mysql";
$ext = "db";
[/quote]

Is true, but if you adjust the regex this should be no problem.
Code:

[code]if($_FILES['user_file']['tmp_name'] == "none")
{
$message="File did not successfully upload. Check the filesize. File must not exceed 2MB.<br>";
include("file_manager.inc");
exit();
}
if (is_uploaded_file($_FILES['user_file']['tmp_name']))
{
if(!preg_match('/^((\w)(.[pdf|ppt]))$/', $_FILES[$userfile]['name']), $match = array())
{
$message="File is not a PDF or Powerpoint file. Please try another file.<br>";
include("file_manager.inc");
exit();
}
$completeName = $match[0];
$filename = $match[1];
$ext = $match[2];
}[/code]

It was this line that the error message referred to:

[code] if(!preg_match('/^((\w)(.[pdf|ppt]))$/', $_FILES[$userfile]['name']), $match = array()) [/code]
No errors are now generated it just doesnt recognise the file as being a PDF.

Here is the full code if that helps:
[code]<?php
session_start();
if (@$_SESSION['auth'] != "yes")
{
header("Location: Login.php");
exit();
}
if(!isset($_POST['Upload']))
{
include("file_manager.inc");
} #endif
else
{
if($_FILES['user_file']['tmp_name'] == "none")
{
$message="File did not successfully upload. Check the filesize. File must not exceed 2MB.<br>";
include("file_manager.inc");
exit();
}
if (is_uploaded_file($_FILES['user_file']['tmp_name']))
{
if(!preg_match('/^((\w)(.[pdf|ppt]))$/', $_FILES[$userfile]['name'], $match = array()))
{
    $message="File is not a PDF or Powerpoint file. Please try another file.<br>";
    include("file_manager.inc");
    exit();
}
$completeName = $match[0];
$filename = $match[1];
$ext = $match[2];
}
else
{
$destination = 'user_files/' . "/" .$_FILES['user_file']['name'];
$temp_file = $_FILES['user_file']['tmp_name'];
move_uploaded_file($temp_file,$destination);
$message="<p>The file has successfully uploaded: {$_FILES['user_file']['name']}</p>";
include("user.inc");
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$cat = $_REQUEST['categories'];
$query = "SELECT * FROM fileCategory WHERE categoryName='$cat'";
$result1 = mysql_query($query)
or die ("Couldn't execute query1");
$row = mysql_fetch_array($result1,MYSQL_ASSOC);
extract($row);
$catNum = $categoryNumber;
$fileName = $_FILES['user_file']['name'];
$sql = "INSERT INTO File (fileName,categoryNumber) VALUES ('$fileName','$catNum')";
$result = mysql_query($sql)
or die ("Couldn't execute query");
include("file_manager.inc");
}
}
?>[/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.