0

I have some problems. I have an mySQL database where i want to store some peronal files. I know it is not the best way to handle it but they are only some excel files so it shoeldnt be that big of a deal i thought. However i only download empty files or excel files with the PHP in it. THis is what is in my SQL table:

['name'] = The name of the file;
['mime'] = The type of file (application/vnd.ms-excel);
['size'] = The size of the data;
['data'] = And the data itself;

Also there is a user id for who can see the file.

This is what is in my download file:

if(isset($_GET['id']))
{
  $id=intval($_GET['id']);
  if($id <= 0)
  {
    die('Het opgegeven ID is niet correct');
  }
  else
  {
    $sql_facturen=mysqli_query($con,"SELECT mime, name, data FROM facturen WHERE id='" . $id . "'");
    echo '<p> doing query </p>';
    if($sql_facturen)
    {
      if(mysqli_num_rows($sql_facturen) == 1)
      {
        $row_facturen=mysqli_fetch_assoc($sql_facturen);
        header("Content-Length: " . $row_facturen['size']);
        header("Content-Type: " . $row_facturen['mime']);
        header("Content-Disposition: attachment; filename=" .basename( $row_facturen['name']));

        readfile($row_facturen['data']);
      }
      else
      {
        echo '<p>Geen factuur met dat id aanwezig</p>';
      }
    }
  }
}

I have tried multiple header configurations that i found on the web. I also tried echo instead of readfile. However stil not the text that should be in it. Any sugestions? I am using google chrome.

EDIT: Here is my upload file:

if(isset($_FILES['uploaded_file']))
                if($_FILES['uploaded_file']['error'] == 0) 
                {
                    $name = mysqli_real_escape_string($con, $_FILES['uploaded_file']['name']);
                    $mime = mysqli_real_escape_string($con, $_FILES['uploaded_file']['type']);
                    $data = mysqli_real_escape_string($con, ($_FILES['uploaded_file']['tmp_name']));
                    $size = mysqli_real_escape_string($con, $_FILES['uploaded_file']['size']);
            $year = $_POST['year'];
                    $user_id = $_GET['id'];

                    $sql_upload=mysqli_query($con,"INSERT INTO facturen (name, mime, size, data, created, year, user_id) VALUES ('" . $name . "','" . $mime . "','" . $size . "','" . $data . "', NOW(), '" . $year . "','" . $user_id . "')");
            if ($sql_upload)
            {
                echo 'Bestand is succesvol ingevoerd';
            }
            else
            {
                        echo 'Error uploading';
                        echo mysqli_error($con);
            }
                }

1 Answer 1

1

Try this

$fileSize = strlen($row_facturen['data']);

Send header as follows :

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$row_facturen['name'].'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $fileSize);
echo $row_facturen['data'];
exit();

[Added from here in favor of user]

Save file into database:

$tmpName=$_FILES['uploaded_file']['tmp_name'];                     
$fp      = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($content);
fclose($fp);

Also specify the mysql fieldtype(your data field) to MEDIUMBLOB

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry to say but is doesn't work. When i download the file, it is filled with the HTML of the website :(
What are you storing in your database actually? How do you store the file data in your table?
$tmpName=$_FILES['uploaded_file']['tmp_name']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content);//content here fclose($fp);

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.