php - uploading a document file to the database
one text
I am trying to upload some information to the database in the table called "projects_already_done" but it's failing. It's giving me an error: "Type: ErrorException Code: 8 Message: Undefined index: file_name File: C:\xampp\htdocs\fypms_slim1\app\routes\coordinator\projects\add_project_already_done.php Line: 63"
here is the code in the "projects_already_done.php route"
<?php
use Logbook\User\ProjectCategory;
use Illuminate\Database\Capsule\Manager as DB;
$app->get('/coordinator/projects/add_project_already_done', $coordinator(), function() use($app){
// get project categories and types
$query = 'SELECT * FROM project_categories';
$query2 = 'SELECT * FROM project_types';
$project_categories = DB::select(DB::raw($query));
$project_types = DB::select(DB::raw($query2));
$app->render('coordinator/projects/add_project_already_done.php', [
'project_categories' => $project_categories,
'project_types' => $project_types
]);
})->name('coordinator.add_project_already_done');
$app->post('/coordinator/projects/add_project_already_done', $coordinator(), function() use($app){
// when the send for approval button is clicked
if(isset($_POST['save'])){
//store everything sent to this route
$request = $app->request;
//get user input
$project_name = $request->post('project_name');
$project_description = $request->post('project_description');
$project_cat = $request->post('project_cat');
$project_type = $request->post('project_type');
$file_name = $request->post('file_name');
//validate user input
$v = $app->validation;
$v->addRuleMessage('uniqueProjectCategory', 'This project has already been added.');
$v->addRule('uniqueProjectCategory', function($value, $input, $args) {
$pc = DB::table('projects_already_done')
->select(['project_name', 'project_description', 'project_cat', 'project_type', 'file_name'])
->where([
'project_name' => $value,
'project_description' => $value,
'project_cat' => $value,
'project_type' => $value
])
->first();
return ! (bool) $pc;
});
$v->validate([
'project_name' => [$project_name, 'required|alnumDash|max(150)|uniqueProjectCategory'],
'project_description' => [$project_description, 'required|alnumDash|max(150)|uniqueProjectCategory'],
'project_cat' => [$project_cat, 'alnumDash|max(150)|uniqueProjectCategory'],
'project_type' => [$project_type, 'alnumDash|max(150)|uniqueProjectCategory']
]);
if($v->passes()){
$file = $_FILES['file_name'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
//allowed files
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf', 'docx', 'txt', 'pptx', 'zip', 'rar');
if(in_array($fileActualExt, $allowed)){
// check for errors when uploading file
if($fileError === 0){
//check for file size
if($fileSize < 60000000){
// get proper file name
$fileNameNew = uniqid('', true).".".$fileActualExt;
//upload file to root folder
$file_path = $_SERVER['DOCUMENT_ROOT'].'/fypms_slim1/uploads/projects/'.$fileNameNew;
//upload file
move_uploaded_file($fileTmpName, $file_path);
//insert record into project category table
DB::table('projects_already_done')
->insert([
'project_name' => $project_name,
'project_description' => $project_description,
'project_cat' => $project_cat,
'project_type' => $project_type,
'file_name' => $file_name,
'file_path' => $file_path,
'new_file_name' => $file_name
]);
//flash message
$app->flash('success', 'Project has been successfully added.');
return $app->response->redirect($app->urlFor('coordinator.add_project_already_done'));
}else{
// flash message and redirect
$app->flash('error', 'Your file is too large. Only files below 50mb are allowed');
return $app->response->redirect($app->urlFor('student.complete_task',array('id' => $task_id)));
}
}else{
// flash message and redirect
$app->flash('error', 'There was an error uploading your file!');
return $app->response->redirect($app->urlFor('student.complete_task',array('id' => $task_id)));
}
}else{
//flash message
$app->flash('success', 'Project has been successfully added.');
return $app->response->redirect($app->urlFor('coordinator.add_project_already_done'));
}
}
}
})->name('coordinator.add_project_already_done.post');
here is the code in the "projects_already_done.php view"
{% extends 'templates/default.php' %}
{% block title %}Add Projects done outside this system{% endblock %}
{% block content %}
<div class="container-fluid text-center">
<div class="row content"><br>
<div class="col-sm-2 sidenav">
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">Project Categories</div>
<div class="panel-body">
<p><a href="{{ urlFor('coordinator.add_project_category') }}">Add</a></p>
<p><a href="{{ urlFor('coordinator.view_project_category') }}">View</a></p>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Project Types</div>
<div class="panel-body">
<p><a href="{{ urlFor('coordinator.add_project_type') }}">Add</a></p>
<p><a href="{{ urlFor('coordinator.view_project_type') }}">View</a></p>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Supervision</div>
<div class="panel-body">
<p><a href="{{ urlFor('coordinator.assign_supervisors') }}">Assign Supvervisors</a></p>
<p><a href="{{ urlFor('coordinator.view_assigned_supervisors') }}">View Assigned Supvervisors</a></p>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Student Projects</div>
<div class="panel-body">
<p><a href="{{ urlFor('coordinator.view_projects') }}">View Projects</a></p>
</div>
</div>
</div>
</div>
<div class="col-sm-8 text-left" id="container">
{% include 'templates/partials/error_messages.php' %}
{% include 'templates/partials/success_messages.php' %}
{% include 'templates/partials/info_messages.php' %}
{% include 'templates/partials/warning_messages.php' %}
<form action="{{ urlFor('coordinator.add_project_already_done.post') }}" method="POST" autocomplete="off">
<fieldset>
<legend class="text-center">Add Projects done outside this system</legend>
<div class="col-sm-6">
<div class="col-sm-12">
<div class="form-group">
<label for="projectStudentEmail">Project Name</label>
<input type="text" class="form-control" id="project_name" aria-describedby="project_nameHelp" placeholder="project name" name="project_name"{% if request.post('project_name') %} value="{{request.post('project_name')}}" {% endif %} required>
{% if errors.has('project_name')%}<small class="form-text text-muted" style="color: red;">{{errors.first('project_name')}}</small>{% endif %}
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="project_description">Project Description</label>
<textarea class="form-control" rows="5" id="project_description" aria-describedby="projectDescriptionHelp" placeholder="Add a brief description about the project" name="project_description">{{ request.post('project_description') ? request.post('project_description') : project.project_description }}</textarea>
{% if errors.has('project_description')%}<small class="form-text text-muted" style="color: red;">{{errors.first('project_description')}}</small>{% endif %}
</div>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="col-sm-6">
<div class="col-sm-12">
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="projectType">Project Type</label>
<select class="form-control" id="project_type" name="project_type" >
{% if project_types is empty %}
<option>No project type records </option>
{% else %}
<option>-- select project type --</option>
{% for pt in project_types%}
<option value="{{ pt.project_type }}">{{ pt.project_type }}</option>
{% endfor %}
{% endif %}
</select>
{% if errors.has('projectType')%}<small class="form-text text-muted" style="color: red;">{{errors.first('projectType')}}</small>{% endif %}
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="projectCategory">Project Category</label>
<select class="form-control" id="project_cat" name="project_cat">
{% if project_categories is empty %}
<option>No project category records</option>
{% else %}
<option>-- select project category --</option>
{% for pc in project_categories %}
<option value="{{ pc.project_category }}">{{ pc.project_category }}</option>
{% endfor %}
{% endif %}
</select>
{% if errors.has('projectCategory')%}<small class="form-text text-muted" style="color: red;">{{errors.first('projectCategory')}}</small>{% endif %}
</div>
</div>
<div class="form-group">
<label for="file_attachments">Add File:</label>
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-primary">
Browse… <input type="file" style="display: none;" id="file_attachments" name="file_attachments">
</span>
</label>
<input type="text" class="form-control" name="file_name" value="{{ request.post('file_name') ? request.post('file_name') : file_name }}" readonly>
</div>
{% if errors.has('file_attachments')%}<small class="form-text text-muted" style="color: red;">{{errors.first('file_attachments')}}</small>{% endif %}
</div>
<div class="col-sm-12">
<button type="submit" class="btn btn-primary" name="save">Add Project</button>
</div>
</div>
<div class="col-sm-6">
<input type="hidden" name="{{ csrf_key }}" value="{{ csrf_token }}">
</div>
</fieldset>
</form>
</div>
<div class="col-sm-2 sidenav">
<div class="well">
<label style="font-size: 20px;">Tip <span class="glyphicon glyphicon-info-sign"></span></label>
<p>This section is used for adding projects done outside this system</p>
</div>
</div>
</div>
</div>
{% endblock %}
Source