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 ?

Recommended Answers

All 6 Replies

Member Avatar for diafol

> 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.

> 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"><br>
        <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
?>
Member Avatar for diafol

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.

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

Member Avatar for diafol

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.

when uploading word document how to save some fields from word document in database while uploading a file....and then when we upload a word or pdf document in database and how to download these doc and pdf files into excel formate while downloading only

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.