954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

saving Content of word document in database with php

please if anyone could tell me, how it would be possible,


i want to save the content of a uploaded word document in database field with php, no problem with encrypted file like ( ´V z2Ö<Ö<ôÿÿÿÿÿÿ·üü?????ÿÿÿÿSSS8‹DÏLS¥9à1) as it shows if you open a word document in a browser. just want to save content of whole document in field.

thanks for help in advance. i read it somewhere open file with fopen save it in variable n then save it in database, is it possible in that way ?

Sahilsahni
Light Poster
25 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

> i read it somewhere open file with fopen save it in variable n then save it in database, is it possible in that way ?

Have you tried it? It would only take a handful of lines of code.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

> i read it somewhere open file with fopen save it in variable n then save it in database, is it possible in that way ?

Have you tried it? It would only take a handful of lines of code.


only fopen() didn't work so with lots of tries found the solution for refrence of other people am shearing it here.

<?php // to upload files
include("includes/connection.php");?>
<!DOCTYPE html>
<head>
    <title>MySQL file upload example</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <form action="add_file.php" method="post" enctype="multipart/form-data">
        <input type="file" name="uploaded_file">
        <input type="submit" value="Upload file">
    </form>
    <p>
        <a href="list_files.php">See all files</a>
    </p>
</body>
</html>
<?php //to open and save it in database
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'user', 'password', 'db_name');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }
 
        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);
 
        // Create the SQL query
       $query = "UPDATE file SET
							
							name='{$name}',
							mime='{$mime}',
							size='{$size}',
							data='{$data}',
							created= NOW()
							
												  

WHERE name='XYZ'"; 
 // can also use insert query here according to your need
        // Execute the query
        $result = $dbLink->query($query);
 
        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }
 
    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}
 
// Echo a link back to the main page
?>
Sahilsahni
Light Poster
25 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Great. DIY is so much sweeter. However, is there an advantage of doing this, since you've already uploaded the file. Why not just save the location in the DB? Seems like a lot of work.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
Great. DIY is so much sweeter. However, is there an advantage of doing this, since you've already uploaded the file. Why not just save the location in the DB? Seems like a lot of work.

earlier i was using this thing, saving only file name in database, now as i am working on a search query where i required data from uploaded docs (around 100,000 docs), so for that had to make it save in database. well with that docs get save in an encrypted language, but you can get required data by using LIKE in query

Sahilsahni
Light Poster
25 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Does Word save overwritten work as well? I know in earlier forms, versioning was an option. If this is still the case, you could end up getting a positive search result for a file, only to find the search term is no longer in the actual document. Just a thought.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: