Пожалуйста, у меня есть несколько файлов PDF, сохраненных в папке загрузки, и я хочу просмотреть файлы, у меня есть код, но всякий раз, когда я нажимаю на кнопку просмотра, он возвращает страницу 404, не найденную. Я попытался вызвать функцию загрузки, а также функцию просмотра файла. Ниже приведен код файла представления.
<div class="box-body">
<div class="container-fluid no-padding">
<div class="row">
<?php $status = TRUE; $k = 0; if(isset($activitiesmedia[$activity->activitiesID])) { foreach ($activitiesmedia[$activity->activitiesID] as $attachment) { ?>
<?php if(count($activitiesmedia[$activity->activitiesID]) == 2) { $status = FALSE; ?>
<div class="col-md-6 no-padding">
<span class="activity-icon-box"><i class="fa <?=isset($activitiescategories[$activity->activitiescategoryID]) ? $activitiescategories[$activity->activitiescategoryID]->fa_icon : ''?>"></i></span>
<img style="width: 100%;height:300px" class="img-responsive pad" src="<?=base_url("uploads/activities/$attachment->attachment"); ?>" alt="Photo">
<a href="<?=base_url('Activities/viewfiles/'.$attachment->attachment)?>" target="_blank">Show My Pdf</a>
</div>
<?php $k++; } elseif(count($activitiesmedia[$activity->activitiesID]) > 1) { if($status) { ?>
<div class="col-md-4 no-padding">
<span class="activity-icon-box"><i class="fa <?=isset($activitiescategories[$activity->activitiescategoryID]) ? $activitiescategories[$activity->activitiescategoryID]->fa_icon : ''?>"></i></span>
<img style="width: 100%;height:200px" class="img-responsive pad" src="<?=base_url("uploads/activities/$attachment->attachment"); ?>" alt="Photo">
<a href="<?=base_url('Activities/viewfiles/'.$attachment->attachment)?>" target="_blank">Show My Pdf</a>
</div>
<?php } } else { ?>
<div class="col-md-12 no-padding">
<span class="activity-icon-box"><i class="fa <?=isset($activitiescategories[$activity->activitiescategoryID]) ? $activitiescategories[$activity->activitiescategoryID]->fa_icon : ''?>"></i></span>
<img style="width: 100%; max-height: 450px;" class="img-responsive pad" src="<?=base_url("uploads/activities/$attachment->attachment"); ?>" alt="Photo">
<a href="<?=base_url('Activities/viewfiles/'.$attachment->attachment)?>" target="_blank">Show My Pdf</a>
?>
</div>
Ниже представлен контроллер
<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Activities extends Admin_Controller
{ public function __construct()
{
parent::__construct();
$this->load->model("activities_m");
$this->load->model("activitiescategory_m");
$this->load->model("activitiesstudent_m");
$this->load->model("activitiesmedia_m");
$this->load->model("activitiescomment_m");
$this->load->model("studentrelation_m");
$this->load->model("classes_m");
$this->load->model("section_m");
$language = $this->session->userdata('lang');
$this->lang->load('activities', $language);
$this->load->helper('date');
}
public function index()
{
$schoolyearID = $this->session->userdata('defaultschoolyearID');
$loginuserID = $this->session->userdata('loginuserID');
$usertypeID = $this->session->userdata('usertypeID');
$this->data['user'] = getAllSelectUser();
$this->data['userID'] = $loginuserID;
$this->data['usertypeID'] = $usertypeID;
$this->data['activitiescategories'] = pluck($this->activitiescategory_m->get_activitiescategory(), 'obj', 'activitiescategoryID');
$this->data['activitiesmedia'] = pluck_multi_array($this->activitiesmedia_m->get_activitiesmedia(), 'obj', 'activitiesID');
$this->data['activitiescomments'] = pluck_multi_array($this->activitiescomment_m->get_order_by_activitiescomment(array('schoolyearID' => $schoolyearID)), 'obj', 'activitiesID');
if ($usertypeID == 2) {
$students = $this->studentrelation_m->get_order_by_student(['srschoolyearID' => $schoolyearID]);
$studentArray = pluck($students, 'studentID');
$this->data['activities'] = $this->activities_m->get_where_in_activities($studentArray, 'studentID', array('schoolyearID' => $schoolyearID));
} elseif ($usertypeID == 3) {
$this->data['activities'] = $this->activities_m->get_order_by_activities(array('schoolyearID' => $schoolyearID, 'studentID' => $loginuserID));
} elseif ($usertypeID == 4) {
$students = $this->studentrelation_m->get_order_by_student(['srschoolyearID' => $schoolyearID]);
$studentArray = pluck($students, 'studentID');
$this->data['activities'] = $this->activities_m->get_where_in_activities($studentArray, 'studentID', array('schoolyearID' => $schoolyearID));
} else {
$this->data['activities'] = $this->activities_m->get_order_by_activities(array('schoolyearID' => $schoolyearID));
}
if (($this->data['siteinfos']->school_year == $schoolyearID) || ($usertypeID == 1)) {
if ($_POST) {
$id = htmlentities(escapeString($this->uri->segment(3)));
if ((int) $id) {
if ($_POST['comment']) {
$array['activitiesID'] = $id;
$array['comment'] = $this->input->post('comment');
$array['schoolyearID'] = $schoolyearID;
$array['userID'] = $this->session->userdata("loginuserID");
$array['usertypeID'] = $this->session->userdata("usertypeID");
$array['create_date'] = date("Y-m-d H:i:s");
$this->activitiescomment_m->insert_activitiescomment($array);
$this->session->set_flashdata('success', $this->lang->line("menu_success"));
redirect(base_url("activities/index"));
}
}
}
}
$this->data["subview"] = "activities/index";
$this->load->view('_layout_main', $this->data);
}
public function add()
{
$schoolyearID = $this->session->userdata('defaultschoolyearID');
if (($this->data['siteinfos']->school_year == $schoolyearID) || ($this->session->userdata('usertypeID') == 1)) {
if($this->session->userdata('usertypeID') == 3 || $this->session->userdata('usertypeID') == 4) {
$this->session->set_flashdata('error', $this->lang->line('menu_permission_not_allowed'));
redirect(base_url("activities/index"));
}
$this->data['headerassets'] = array(
'css' => array(
'assets/datepicker/datepicker.css',
'assets/timepicker/timepicker.css',
'assets/select2/css/select2.css',
'assets/select2/css/select2-bootstrap.css',
'assets/tooltipster/css/tooltipster.bundle.min.css',
),
'js' => array(
'assets/datepicker/datepicker.js',
'assets/select2/select2.js',
'assets/tooltipster/js/tooltipster.bundle.min.js',
'assets/timepicker/timepicker.js',
),
);
$categoryID = htmlentities(escapeString($this->uri->segment(3)));
$this->data['classes'] = $this->classes_m->get_classes();
$this->data['sections'] = [];
$this->data['students'] = [];
if ((int) $categoryID) {
$this->data['activities_categories'] = $this->activitiescategory_m->get_activitiescategory();
if ($_POST) {
$classesID = $this->input->post("classesID");
$sectionID = $this->input->post("sectionID");
$studentID = $this->input->post("studentID");
if ((int) $classesID) {
$this->data['sections'] = $this->section_m->get_order_by_section(array('classesID' => $classesID));
$qArray['srclassesID'] = $classesID;
if ((int) $sectionID) {
$qArray['srsectionID'] = $sectionID;
}
$qArray['srschoolyearID'] = $schoolyearID;
$this->data['students'] = $this->studentrelation_m->get_order_by_student($qArray);
}
$rules = $this->rules();
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == false) {
$this->data["subview"] = "activities/add";
$this->load->view('_layout_main', $this->data);
} else {
$array = array(
"classesID" => $classesID,
"sectionID" => $sectionID,
"studentID" => $studentID,
"description" => $this->input->post("description"),
"activitiescategoryID" => $categoryID,
"schoolyearID" => $schoolyearID,
"usertypeID" => $this->session->userdata('usertypeID'),
"userID" => $this->session->userdata('loginuserID'),
);
if ($this->input->post("time_to") != "0:00") {
$array["time_to"] = date('H:i:s', strtotime($this->input->post("time_to")));
}
if ($this->input->post("time_from") != "0:00") {
$array["time_from"] = date('H:i:s', strtotime($this->input->post("time_from")));
}
if ($this->input->post("time_at") != "0:00") {
$array["time_at"] = date('H:i:s', strtotime($this->input->post("time_at")));
}
$array["create_date"] = date("Y-m-d H:i:s");
$array["modify_date"] = date("Y-m-d H:i:s");
$id = $this->activities_m->insert_activities($array);
if ($id) {
if (!empty($_FILES['attachment']['name'])) {
$filesCount = count($_FILES['attachment']['name']);
for ($i = 0; $i < $filesCount; $i++) {
$_FILES['attach']['name'] = $_FILES['attachment']['name'][$i];
$_FILES['attach']['type'] = $_FILES['attachment']['type'][$i];
$_FILES['attach']['tmp_name'] = $_FILES['attachment']['tmp_name'][$i];
$_FILES['attach']['error'] = $_FILES['attachment']['error'][$i];
$_FILES['attach']['size'] = $_FILES['attachment']['size'][$i];
$uploadPath = 'uploads/activities';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png|pdf|PDF';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('attach')) {
$fileData = $this->upload->data();
$uploadData[$i]['attachment'] = $fileData['file_name'];
$uploadData[$i]['activitiesID'] = $id;
$uploadData[$i]['create_date'] = date("Y-m-d H:i:s");
}
}
if (!empty($uploadData)) {
$this->activitiesmedia_m->insert_batch_activitiesmedia($uploadData);
}
}
}
$this->session->set_flashdata('success', $this->lang->line('menu_success'));
redirect(base_url("activities/index"));
}
} else {
$this->data["subview"] = "activities/add";
$this->load->view('_layout_main', $this->data);
}
} else {
$this->data["subview"] = "activities/add";
$this->load->view('_layout_main', $this->data);
}
} else {
$this->data["subview"] = "error";
$this->load->view('_layout_main', $this->data);
}
}
public function delete()
{
if (($this->data['siteinfos']->school_year == $this->session->userdata('defaultschoolyearID')) || ($this->session->userdata('usertypeID') == 1)) {
$id = htmlentities(escapeString($this->uri->segment(3)));
$usertypeID = $this->session->userdata('usertypeID');
$userID = $this->session->userdata('loginuserID');
if ((int) $id) {
$activities = $this->activities_m->get_activities($id);
if (($usertypeID == $activities->usertypeID && $userID == $activities->userID) || ($usertypeID == 1)) {
$this->activities_m->delete_activities($id);
$this->session->set_flashdata('success', $this->lang->line('menu_success'));
}
redirect(base_url("activities/index"));
} else {
redirect(base_url("activities/index"));
}
} else {
$this->data["subview"] = "error";
$this->load->view('_layout_main', $this->data);
}
}
public function delete_comment()
{
if (($this->data['siteinfos']->school_year == $this->session->userdata('defaultschoolyearID')) || ($this->session->userdata('usertypeID') == 1)) {
$id = htmlentities(escapeString($this->uri->segment(3)));
$usertypeID = $this->session->userdata('usertypeID');
$userID = $this->session->userdata('loginuserID');
if ((int) $id) {
$comment = $this->activitiescomment_m->get_activitiescomment($id);
$activities = $this->activities_m->get_activities($comment->activitiesID);
if (($usertypeID == $activities->usertypeID && $userID == $activities->userID) || ($usertypeID == 1)) {
$this->activitiescomment_m->delete_activitiescomment($id);
$this->session->set_flashdata('success', $this->lang->line('menu_success'));
}
redirect(base_url("activities/index"));
} else {
redirect(base_url("activities/index"));
}
} else {
$this->data["subview"] = "error";
$this->load->view('_layout_main', $this->data);
}
}
protected function rules()
{
$rules = array(
array(
'field' => 'classesID',
'label' => $this->lang->line("menu_classes"),
'rules' => 'trim|required|xss_clean|callback_unique_data',
),
array(
'field' => 'sectionID',
'label' => $this->lang->line("menu_section"),
'rules' => 'trim|required|xss_clean|callback_unique_data',
),
array(
'field' => 'studentID',
'label' => $this->lang->line("menu_student"),
'rules' => 'trim|required|xss_clean|callback_unique_data',
),
array(
'field' => 'description',
'label' => $this->lang->line("activities_description"),
'rules' => 'trim|required|xss_clean',
),
array(
'field' => 'attachment',
'label' => $this->lang->line("attachment"),
'rules' => 'trim|xss_clean',
),
array(
'field' => 'time_from',
'label' => $this->lang->line("activities_time_from"),
'rules' => 'trim|max_length[10]|xss_clean',
),
array(
'field' => 'time_to',
'label' => $this->lang->line("activities_time_to"),
'rules' => 'trim|max_length[10]|xss_clean',
),
array(
'field' => 'time_at',
'label' => $this->lang->line("activities_time_at"),
'rules' => 'trim|max_length[10]|xss_clean',
),
);
return $rules;
}
public function get_section()
{
$classesID = $this->input->post('classesID');
if ((int) $classesID) {
$sections = $this->section_m->get_order_by_section(array('classesID' => $classesID));
echo "<option value='0'>" . $this->lang->line("menu_please_select") . "</option>";
foreach ($sections as $section) {
echo "<option value=" . $section->sectionID . ">" . $section->section . "</option>";
}
}
}
public function get_student()
{
$classesID = $this->input->post('classesID');
if ((int) $classesID) {
$schoolyearID = $this->session->userdata('defaultschoolyearID');
$qArray['srclassesID'] = $classesID;
$sectionID = $this->input->post('sectionID');
if ((int) $sectionID) {
$qArray['srsectionID'] = $sectionID;
}
$qArray['srschoolyearID'] = $schoolyearID;
$students = $this->studentrelation_m->get_order_by_student($qArray);
echo "<option value='0'>" . $this->lang->line("menu_please_select") . "</option>";
foreach ($students as $student) {
echo "<option value=" . $student->studentID . ">" . $student->srname . "</option>";
}
}
public function viewfile(){
$fname = $this->uri->segment(3);
$tofile= realpath("uploads/activities/".$fname);
header('Content-Type: application/pdf');
readfile($tofile);
}
public function unique_data($data)
{
if ($data == 0) {
$this->form_validation->set_message("unique_data", "The %s field is required");
return false;
}
return true;
}
}
Ниже представлена модель
class Activitiesmedia_m extends MY_Model {
protected $_table_name = 'activitiesmedia';
protected $_primary_key = 'activitiesmediaID';
protected $_primary_filter = 'intval';
protected $_order_by = "activitiesID asc";
function __construct() {
parent::__construct();
}
function get_activitiesmedia($array=NULL, $signal=FALSE) {
$query = parent::get($array, $signal);
return $query;
}
function get_order_by_activitiesmedia($array=NULL) {
$query = parent::get_order_by($array);
return $query;
}
function get_single_activitiesmedia($array=NULL) {
$query = parent::get_single($array);
return $query;
}
function download($id){
$query = $this->db->get_where('files',array('id'=>$id));
return $query->row_array();
}
function insert_activitiesmedia($array) {
$id = parent::insert($array);
return $id;
}
function insert_batch_activitiesmedia($array) {
$insert = $this->db->insert_batch($this->_table_name, $array);
return $insert ? true:false;
}
function update_activitiesmedia($data, $id = NULL) {
parent::update($data, $id);
return $id;
}
public function delete_activitiesmedia($id){
parent::delete($id);
}
}
Ниже приведен файл загрузки. php
<?php
if(form_error('attachment'))
echo "<div class='form-group has-error' >";
else
echo "<div class='form-group' >";
?>
<label for="attachment" class="col-sm-2 control-label">
<?=$this->lang->line("attachment")?>
</label>
<div class="col-sm-6">
<input id="fileupload" multiple="multiple" type="file" name="attachment[]"/>
</div>
<span class="col-sm-4">
<?php echo form_error('attachment'); ?>
</span>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<input type="submit" class="btn btn-success" value="<?=$this->lang->line("add_activities")?>" >
</div>
</div>
</form>
</div>
</div>
</div>
<div class="box-footer clearfix">
<div id="dvPreview"></div>
</div>