2

Hi I know php basics and that. I've recently rediscovered it and teaching myself new techniques. So please bear with me...

In this task, I'm to upload a MS Word document via php, store it on mysql and retrieve it when needed.

Is this possible? Should a file such as a .doc be stored on the same database as first name, last name, email etc. Or should I store it as a BLOB on a separate database for file uploads? I'm not sure if mysql is able to read every line on a .doc file? Thanks. Are there any examples online for beginners like myself? I've already done the research. Some people say I shouldn't use mysql to store text files, other say I can. I currently have:

<form action="insert.php" method="post">
<label>First Name:</label><input type="text" name="firstname" id="firstname"/><br>
<label>Last Name:</label><input type="text" name="lastname" id="lastname"/><br>
<label>Email:</label><input type="text" name="email" id="email"/><br>
<div><label id="upload">Select file to upload:
<input type="file" id="upload" name="upload"/></label></div><br>
     <input type="submit" name=submit value="Submit"/>
</form>
Superunknown
  • 501
  • 2
  • 10
  • 30
  • 1
    Firstly, you won't be able to upload anything; not without first including a valid enctype in the form. – Funk Forty Niner Mar 09 '15 at 16:22
  • 1
    You could always have a "doc_path" column and store a reference to the file in the database, rather than the doc itself. – James Spence Mar 09 '15 at 16:51
  • 1
    I'd personally not store any FILES in a database, there's the FILEsystem for that. Saving a reference to the save location of the .doc in your database is enough. –  Mar 16 '15 at 14:48

1 Answers1

6

There are a few topic to be discussed here.

The form

To upload the file, change the form enctype attribute.

<form action="insert.php" method="post" enctype="multipart/form-data">
    :
</form>

Storing the file

You could store the file in a database, or just as file in the server disk system. Whatever you choose, it is not necessary to split the file into its lines. Store the file as a single unit.

Read this entry which discusses the topic.

It should suffice to say here that your database field should be an appropriate type and size to hold the file.

Personally, I am a fan on storing the file on disk, and the file name on the database.

File upload handling

You can save the file somwehere on disk. This is not the best way to do it, but the simplest to demostrate. There are enough example on SO, for example How to upload & Save Files with Desired name

 $info = pathinfo($_FILES['upload']['name']);
 $ext = $info['extension']; // get the extension of the file
 $newname = "newname.".$ext; 

 $target = 'mydocs/'.$newname;
 move_uploaded_file( $_FILES['upload']['tmp_name'], $target);

Downloading the file To have the file displayed and download, merely print the contents out to the browser.

ob_start();
 // do things. See below
ob_clean();
flush();
readfile($file);
ob_flush();

This will display the file and probably confuse the browser. To tell the browser to handle the file as a Word document, you must send the appropriate headers to the browser before sending the file.

    ob_start();
    if(isset($_REQUEST['dlink']))
    {
        $file = $_REQUEST['dlink'];
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
         exit;
    }
ob_flush();
Community
  • 1
  • 1
crafter
  • 6,246
  • 1
  • 34
  • 46