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:
First Name:
Last Name:
Email:
解决方案
There are a few topic to be discussed here.
The form
To upload the file, change the form enctype attribute.
:
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.
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();