0

I am using the following php file upload script to upload a file, create a folder directory and store the file in a folder then log the details of that file in my database.

My problem is that this works on one of my PDF documents aroun 54kb but when I try and upload a larger pdf around 5mb it says invalid file type. also I am having trouble uploading document file types in general. I don't know if this is because documents can be either docx or doc, but my script should support both types if possible.

Can someone please show me where I am going wrong? Thanks,

<?php
session_start();
include("config.php");


$supplier_name = $_POST['supplier_name'];
$supplier_number = $_POST['supplier_number'];
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
$end_date = date('Y-m-d', strtotime($_POST['end_date']));
$notice = $_POST['notice'];
$steak = $_POST['steak'];

$supplier_name = stripslashes($supplier_name);
$supplier_name = mysql_real_escape_string($supplier_name);
$supplier_number = stripslashes($supplier_number);
$supplier_number = mysql_real_escape_string($supplier_number);
$start_date = stripslashes($start_date);
$start_date = mysql_real_escape_string($start_date);
$end_date = stripslashes($end_date);
$end_date = mysql_real_escape_string($end_date);
$notice = stripslashes($notice);
$notice = mysql_real_escape_string($notice);
$steak = stripslashes($steak);
$steak = mysql_real_escape_string($steak);

$rand = "HC" . substr(md5(microtime()),rand(0,26),5);
mkdir("../data/contracts/$rand");   

$allowedExts = array("pdf", "doc");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/doc")
&& ($_FILES["file"]["size"] < 10000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 10000) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

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


      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../data/contracts/$rand/" . $_FILES["file"]["name"]);

     $sql = "INSERT INTO supplier_contracts (id, supplier_name, supplier_number, contract_number, contract_start, contract_end, contract_termination_period, date, owner, steak_holder) VALUES ('', '$supplier_name', '$supplier_number','$rand', '$start_date', '$end_date', '$notice', now(), '{$_SESSION['user']}', '$steak')";
    $result2 = mysql_query($sql);

    if($result2) {
        $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">&#10006;</div><h23>Thank You!</h23><p>Your Contract was successfully created. Your Contract number is: '.$rand.'.</p></div>';
        header('Location: ' . $_SERVER['HTTP_REFERER']);

    }else{
    $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">&#10006;</div><h23>Oooops!</h23><p>There was an error trying to create this Contract. Please try again later.</p></div>';
    header('Location: ' . $_SERVER['HTTP_REFERER']);
    }
  } } }else{
      $_SESSION['message2'] = '<div id="message_box2"><div class="boxclose" id="boxclose" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">&#10006;</div><h23>Oooops!</h23><p>There was an error trying to create this Contract. Please try again later.</p></div>';
    header('Location: ' . $_SERVER['HTTP_REFERER']);



    } 
?>
4
  • stackoverflow.com/questions/2184513/… Commented Jan 28, 2015 at 11:35
  • @Bulk thanks but I have already amended my PHP.ini file max upload and post size to 40m but im still getting the same problem Commented Jan 28, 2015 at 12:03
  • Your custom php.ini is in the same folder of your php file? Oh, and to accept docx, per example, you need to change your $allowedExts var too. Commented Jan 28, 2015 at 12:58
  • @DimasPante any idea how I would do that? could you please show me an example? im really new to php so sorry if I'm being a bit dumb Commented Jan 28, 2015 at 13:00

1 Answer 1

1

If you created a custom php.ini (like the code below), put it right into the folder where you php files are.

file_uploads = On
upload_max_filesize = 10M
post_max_size = 10M

To accept another types, you have to add them into your allowedExts array, like:

$allowedExts = array("pdf", "doc", "docx", "xlsx");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.