Codeigniter: Comment télécharger des fichiers chemin d'accès à la base de données?
Je suis en train de mettre le chemin du fichier de base de données, j'ai déjà téléchargé le fichier mais je ne sais pas comment pour obtenir le chemin d'accès du fichier pour le mettre dans la base de données?
Ici est le contrôleur:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class dogo extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('insert_article');
}
public function index()
{
$this->load->view('dogo/dashboard.php');
}
//add new post to database including uploading image
public function new_post()
{
//used for the dropdown menu (category)
$this->load->model('dogo_cat');
$data['categores_dropdown'] = $this->dogo_cat->get_categories();
//form validation
$this->load->library('form_validation');//load the validation library
$this->form_validation->set_rules('title', 'title of the post', 'required');
$this->form_validation->set_rules('text', 'text of the post', 'required');
//$this->form_validation->set_rules('image', 'image of the post', 'required');
$this->form_validation->set_rules('category', 'category of the post', 'required');
//the form validation condition
if($this->form_validation->run() == FALSE){
//error
//$this->view_data
$this->load->view('dogo/new_post.php', $data);
}else{
//no error in the form
$title = $this->input->post('title');
$text = $this->input->post('text');
$category = $this->input->post('category');
$publish = $this->input->post('publish');
//$img_nw = $this->input->post('img_nw');
//$img_nw = $this->input->post('img_nw');
$image_file = $this->input->post('image_file');
//uploading
$this->load->model('upload_new_post');
if($this->input->post('upload')){
$this->upload_new_post->do_upload();
//$this->insert_article->insert_new_post($title, $category, $img_nw, $text, $source, $publish);
$data['images'] = $this->upload_new_post->get_images();
echo "title of the post: " . $title . "<br /> and the text of the post " . $text . "<br /> and category is: " . $category . "<br /> and publish is: " .$publish . "<br /> and image: <pre>" . $do_upload ."</pre>";
//echo $img_nw;
$this->load->view('dogo/new_post.php', $data);
}
}
}
}
Et voici le modèle à télécharger:
<?php
class upload_new_post extends CI_Model{
//retreive categories
var $file_path;
var $file_path_url;
function __construct() {
parent::__construct();
$this->file_path = realpath(APPPATH . '../post_data/images');
$this->file_path_url = base_url().'post_data/images/';
}
function do_upload(){
$config=array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->file_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->file_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images(){
$files = scandir($this->file_path);
$files = array_diff($files, array('.', '..', 'thumbs'));
$images = array();
foreach ($files as $file){
$images [] = array(
'url' => $this->file_path_url . $file,
'thumb_url' => $this->file_path_url . 'thumbs/' .$file
);
}
return $images;
}
}
Et ici le modèle de requête d'insertion:
<?php
class Insert_article extends CI_Model{
//insert new post
function __construct() {
parent::__construct();
}
function insert_new_post($title, $category, $img_nw, $text, $source, $publish)
{
$query_insert = "INSERT INTO hs_news_nw (idcat_nw, idsc_nw, idusr_nw, title_nw, img_nw, text_nw, active_nw, totalview_nw, date_nw) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$this->db->query($query_insert, array($category, $source, 1, $title, $img_nw, $text, 1, 1000, '2011-10-12 02:01:24'));
}
}
OriginalL'auteur ahmedsaber111 | 2011-10-22
Vous devez vous connecter pour publier un commentaire.
Vous devez retourner
$image_data
dedo_upload()
fonction de votreupload_new_post
modèle.$image_data
contient toutes les infos à propos de fichier uploadé, y compris le nom de fichier et le chemin d'accès (faire un print_r). Ensuite, vous pouvez passer surInsert_article
modèle à partir de votre contrôleur de stocker dans la base de données.Pour référence:
http://codeigniter.com/user_guide/libraries/file_uploading.html
Pouvez-vous mettre à jour le code avec ce que vous avez fait?
voici le modèle {upload_new_post} - je ajouter return $image_data à la fin de do_upload fonction comme indiqué ci-dessous: fonction do_upload(){ .. return $image_data; } puis-je ajouter ce code dans le contrôleur {dogo} if($this->input->post('upload')){ $data['upload_img'] = $this->upload_new_post->do_upload(); $this->upload_new_post->do_upload(); .. lorsque je utiliser print_r($data); je vois que je voulais est dans ce tableau [full_path], mais je ne sais pas comment les récupérer à la sortie
1. Vous permettra de récupérer en tant que $data['upload_img']['full_path'] 2. Pourquoi faites-vous appel à do_upload() deux fois?
OriginalL'auteur Vikk
S'il vous plaît essayer si
$fpath
vous donne le chemin d'accès correct:OriginalL'auteur Anju
vous pouvez utiliser realpath de passer à l'emplacement du fichier si j'ai bien compris vous
selon ceci vous devriez être capable de trouver un chemin d'accès au fichier dans
$image_data['full_path']
c'était super, merci les gars
marque quelque chose
answered
merci à vous, vous aussi m'a inspiré
OriginalL'auteur user973254
Vous pouvez essayer de télécharger par de Traitement du Fichier de Bibliothèque..
Contrôleur :
Modèle :
OriginalL'auteur Inspire Shahin