<?php
//database configuration file
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "github";
$description = $_POST["description"];
$dateTime=date('Y-m-d H:i:s');
$dirc = $_POST["directoryName"];
$user = $_SESSION["username"];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "hello1";
if(isset($_POST['submit'])){
// File upload configuration
$targetDir = "project1/";
$allowTypes = array('php','html','css','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
$fileNames = array_filter($_FILES['files']['name']);
echo "hello2";
if(!empty($fileNames)){
echo "hello3";
foreach($_FILES['files']['name'] as $key=>$val){
// File upload path
$fileName = basename($_FILES['files']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
echo "hello4";
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to server
echo "hello5";
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){
// Image db insert sql
echo "hello7";
$insertValuesSQL .= ",('".'1'."','".$fileName."','".'2'."','".'3'."','".'4'."','".'5'."','".'6'."','".'7'."')";
echo "<br>".$insertValuesSQL."<br>";
}else{
$errorUpload .= $_FILES['files']['name'][$key].' | ';
}
}else{
$errorUploadType .= $_FILES['files']['name'][$key].' | ';
}
}
// Error message
$errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):'';
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):'';
$errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
if(!empty($insertValuesSQL)){
$insertValuesSQL = trim($insertValuesSQL, ',');
echo "<br>".$insertValuesSQL;
// Insert image file name into database
echo "hello6";
$insert = $db->query("INSERT INTO project VALUES $insertValuesSQL");
echo "hello10";
if($insert){
$statusMsg = "Files are uploaded successfully.".$errorMsg;
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = "Upload failed! ".$errorMsg;
}
}else{
$statusMsg = 'Please select a file to upload.';
}
}
//header("Location: ../");
?>