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
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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
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.