-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_upload.php
More file actions
52 lines (46 loc) · 1.78 KB
/
Copy pathdocument_upload.php
File metadata and controls
52 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Created by PhpStorm.
* User: Pauline
* Date: 12/04/2019
* Purpose: upload file reference to database and save file in uploads directory
*/
//Initialise code to create a session and link to the database
session_start();
if (!IsSet($_SESSION["userID"])) //user variable must exist in session to stay here
header("Location: login.php"); //if not, go back to login page
$username=$_SESSION["userID"]; //get user name into variable $username
include("config.php");
//takes input from create/documents_form.php and prepares for upload to database
$statusMsg='';
$targetDir = "uploads/";
$fileName=basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = strtolower(pathinfo($targetFilePath,PATHINFO_EXTENSION));
$description = $_POST["description"];
$mod_code=$_POST["mod_code"];
$uploadOk=1;
//upload to filename etc to database and save file in uploads directory
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
//check to see if file already exists prior to uploading
if (file_exists($targetFilePath)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = $db->query("INSERT INTO documents (filename, mod_code, description) VALUES ('".$fileName."', '$mod_code', '$description')");
if($insert){
$statusMsg = "The file ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else {
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Please select a file to upload.';
}
header("location: module.php");
$db->close();
?>