Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:12:54+00:00 2026-05-24T03:12:54+00:00

I am attempting to store files in a MySql database based upon this article

  • 0

I am attempting to store files in a MySql database based upon this article. Before you suggest I store files on the file system and not in the database the files uploaded will be stored and relate to records in the database. I could keep a path to the files and just the file names but I want users to be able to modify and delete the files directly through the web interface. So, let’s just pretend that storing in the database is a good option and not discuss others.

My issue is when I go to download the file the data returned from the BLOB in the MySql database is not exactly what I uploaded in test. If I use the function of addslashes then binary wise it is completely off. If I remove that function from the routine then I get much closer.

Using a Binary Diff application I can see that, while not using addslashes the file data returned has one additional byte of data at the beginning of the file and in turn one less byte at the end. The remainder of the file’s data is offset by one due to this little feature of the download.

I am using a FORM to submit the data and enctype is set to multipart/form-data as it should be. Here’s some code I’m using to get it all to work. DAL in the code references a class I created for my Data Access Layer. It handles all saving and retrieving routines and doesn’t seem to be injecting any new data. I’ve checked by looking at the file’s content before it goes to my DAL class for saving and the extra byte is already there.

upload

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
    $fp = fopen($_FILES['userfile']['tmp_name'], 'r');
    $_FILES['userfile']['content'] = fread($fp, $_FILES['userfile']['size']);
    $_FILES['userfile']['content'] = addslashes($_FILES['userfile']['content']);
    fclose($fp);

    if(!get_magic_quotes_gpc())
    {
        $_FILES['userfile']['name'] = addslashes($_FILES['userfile']['name']);
    }

    $fileDAL = new DAL("files");
    $newID = $fileDAL->SaveItem($_FILES['userfile'], false);

    echo "<br>File ".$_FILES['userfile']['name']." uploaded as ID $newID<br>";
}

download

if(isset($_GET['id']))
{
    $id = $_GET['id'];
    $objFiles = new DAL('files');
    $objFile = $objFiles->GetItem($id);

    header("Content-length: $objFile->size");
    header("Content-type: $objFile->type");
    header("Content-Disposition: attachment; filename=$objFile->name");
    echo $objFile->content;
    exit;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:12:55+00:00Added an answer on May 24, 2026 at 3:12 am

    Turns out that I add an extra line feed or carriage return at the end of a PHP file that was in my headers prior to this code being executed. It caused the header upon writing to include the line feed (OA) in the binary data for file saving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm (attempting) to write a MySQL stored procedure that parses a large text file.
I'm using h5py to access HDF5 files and store the h5py File objects in
I am attempting to detect the file type of a library of files on
I am attempting to store NSMutableDictionaries as a blobs in sqlite, by first converting
I am attempting to create a Stored Procedure for a newly created database. However
I'm attempting to rename a stored procedure in SQL Server 2008 with sp_rename system
I'm attempting to extract a given pattern within a text file, however, the results
I have my PHP files stored under FreeBSD under this directory: /home/david/public_html/code/php In my
I'm attempting to migrate a Core Data SQLite store in my Mac OS X
I am attempting to understand how TFS 2008 (and 2010 if it's different) store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.