File: /home/bibuzptr/tvetelearning.bibu-edu.us/current elearning/testdocx.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//require 'vendor/autoload.php';
include_once("includes/db_connect.php");
// MySQL database connection
$conn=$con;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//use PHPWord\src\PhpWord\IOFactory;
if(isset($_POST['submit'])){
$title = $_POST['title'];
// File upload handling
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file is a valid Word document
if($fileType != "docx") {
echo "Sorry, only DOCX files are allowed.";
$uploadOk = 0;
}
// Check if file was uploaded successfully
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// Extract content from Word document
$zip = zip_open($target_file);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
} // end while
zip_close($zip);
// Save to database
$content = $conn->real_escape_string($content);
$sql = "INSERT INTO documents (title, content) VALUES ('$title', '$content')";
if ($conn->query($sql) === TRUE) {
echo "Document uploaded and saved successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Word Document</title>
</head>
<body>
<h2>Upload a Word Document</h2>
<form action="" method="post" enctype="multipart/form-data">
<label for="title">Title:</label><br>
<input type="text" id="title" name="title"><br><br>
<input type="file" name="fileToUpload" id="fileToUpload"><br><br>
<input type="submit" value="Upload Document" name="submit">
</form>
</body>
</html>