Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.
I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.
My form:
<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
My Controller function:
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$image_data = $this->upload->data();
echo '<pre>'; print_r($image_data); echo '</pre>';
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I am using
echo print_r($image_data); echo;
To display the associated image data but this is all that is returned:
Array
(
[file_name] =>
[file_type] =>
[file_path] => c:/wamp/www/honest/images/
[full_path] => c:/wamp/www/honest/images/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?
Many thanks,
DP.
You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
Related
I made insert data along upload images into the database , when the run was successful , but when the insert data and upload images contained errors.
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
how to cope if I insert the data without uploading images is not error ? and the image database went into default image ?
This my controllers
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => $url.$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
}
else
{
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
}
This my model
function savealat($nama_foto) {
$data = array(
'id' => $this->input->post('id'),
'namaalat' => $this->input->post('namaalat'),
'dayalistrik' => $this->input->post('dayalistrik'),
'merk' => $this->input->post('merk'),
'namasupplier' => $this->input->post('namasupplier'),
'nokatalog' => $this->input->post('nokatalog'),
'noseri' => $this->input->post('noseri'),
'category' => $this->input->post('category'),
'lokasi' => $this->input->post('lokasi'),
'pengguna' => $this->input->post('pengguna'),
'status' => $this->input->post('status'),
'jadwalkal' => $this->input->post('jadwalkal'),
'manual' => $this->input->post('manual'),
'dateinput' => $this->input->post('date'),
'foto' => $nama_foto
//'created' => $tanggal
);
$this->db->insert('tbdetail', $data);
}
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => base_url().$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
else
{
//Moved your code up there
}
}
If I'm right the problem is that you put the upload in else. Try to move the code and tell me if it works
MaY be this answer can help you out. There are few more suggestion in comment part which can figure some way out for you. Your server does not support the GD function required to process this type of image.Ci
I'm using codeIgniter and uploading image path along with form data in database. The problem that i'm facing is they're not displaying in table. Images are successfully uploaded in the folder and the path is also saved in db but it's not displaying the images.
Code that i've written is below.
In view file:
<?php echo form_open_multipart('welcome/insertion'); ?>
<input type="file" name="userfile" id="userfile" size="40" maxlength="90" tabindex="3"/>
<input name="saveForm" type="submit" value="Insert Record" class="iconSave" />
In controller i write:
public function insertion()
{
$this->load->model('data_maintenance','',TRUE);
$this->data_maintenance->insertion();
}
And finally in model:
public function insertion()
{
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
//$file_data = $this->upload->data('image');
$file_data = $this->upload->data();
$new_staff = array(
'agenda_id' => $this->input->post('agenda_id'),
't_name' => $this->input->post('t_name'),
'exp' => $this->input->post('exp'),
'leaves_allow_monthly' => $this->input->post('leaves_allow_monthly'),
'leaves_allow_annualy' => $this->input->post('leaves_allow_annualy'),
'gender' => $this->input->post('gender'),
'cell_no' => $this->input->post('cell_no'),
'dob' => $this->input->post('dob'),
'file' => $file_data['file_name']
);
}
$d="/SampleOOP/application/uploads/" . $new_staff['file'];
$this->db->set('image', $d);
//$this->db->insert('teachers_record');
//$d="/SampleOOP/application/uploads/".$new_staff['file'];
$this->db->set('teacher_name', $new_staff['t_name']);
//$this->db->set('image',$d);
$this->db->insert('teachers_record');
}
try print_r($file_data) and see what comes back, you can also try: $file_data['orig_name']
Or:
$file_name = $file_data['file_name'];
$new_staff = array(
'agenda_id' => $this->input->post('agenda_id'),
't_name' => $this->input->post('t_name'),
'exp' => $this->input->post('exp'),
'leaves_allow_monthly' => $this->input->post('leaves_allow_monthly'),
'leaves_allow_annualy' => $this->input->post('leaves_allow_annualy'),
'gender' => $this->input->post('gender'),
'cell_no' => $this->input->post('cell_no'),
'dob' => $this->input->post('dob'),
'file' => $file_name
);
UPDATE, Try:
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload', $config);
Or
$this->load->library('upload');
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->upload->initialize($config);
$this->upload->data() result is
Array
(
[file_name] => 72f59510f9bbf05933c89e4951acc29d
[file_type] =>
[file_path] => ./inst/public/uploads/
[full_path] => ./inst/public/uploads/72f59510f9bbf05933c89e4951acc29d
[raw_name] => 72f59510f9bbf05933c89e4951acc29d
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
error:
Array
(
[error] => You did not select a file to upload.
)
upload function
function upload(){
if(isset($_POST['userfile']) AND !empty($_POST['userfile']))
{
$Info = $this->login();
if(#$Info)
{
$config['upload_path'] = './inst/public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '230';
$config['max_height'] = '280';
$config['min_width'] = '220';
$config['min_height'] = '270';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['file_name'] = md5(uniqid("100_ID", true));
$this->load->library('upload', $config);
$Setting = $this->Setting;
$this->load->view('header',$Setting);
if ( ! $this->upload->do_upload("userfile"))
{
$response['error'] = array('error' => $this->upload->display_errors());
echo '<pre>';
print_r( $this->upload->data());
$this->load->view('upload_done', $response);
}
else
{
$response['success'] = array('upload_data' => $this->upload->data());
$this->load->view('upload_done', $response);
}
}
}
}
form code
<?php
echo form_open('/Home/upload');
?>
<br><div class="form-group"><input class ='form-control' placeholder="<?php echo lang('fileu'); ?>" type="file" name="userfile" size="20" /></div>
<div class="alert alert-info"><?php echo lang('filetext'); ?></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('Close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo lang('uploadsub'); ?></button>
<?php echo form_close(); ?>
you have to use below code to upload files. You are missing multipart attribute in your form.
echo form_open_multipart('/Home/upload');
First change your helper function to form_open_multipart(). If you still get the error after changing to the correct function, it could be your maxsize property as well .If an uploaded file is larger than the allowable size, the FILES variable will be empty.
Try changing...
$config['max_size'] = '1000';
...to something like...
$config['max_size'] = '30000';
php.net File Upload
What I am trying to do is after uploading the file parse the cvs file to the screen with print_r so I can do some things with it. I cannot seem to figure out how you access the file name with the $data. I have tried several things like $file_name, $data['file_name], and $this->data->$file_name. The relevant part is on line 37.
Not sure what I am doing wrong. I have looked at the code igniter documentation for the uploader library but it doesn't be sufficient to answer my question. Thanks for any help!
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('getcsv');
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
echo 'The file name is: '.$file_name;
}
}
}
?>
The filename can be anything you want. This is clearly in the documentation, you might've been reading the wrong page or something:
http://ellislab.com/codeigniter%20/user-guide/libraries/file_uploading.html
file_name: If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type.
OR:
You can get it from $this->upload->data(), which is an array.
$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
Use the following code :
if($this->upload->do_upload($file)){
//Get uploaded file data here
$allData=$this->upload->data();
$myFile = $allData['file_name'];
}
i have problem when upload images in codeigniter, So i have controller that upload images like this :-
public function index()
{
$this->load->model('blog');
$type = "text";
if (isset($_POST['post'])) {
if (isset($_POST['type']) && $_POST['type'] == "image") {
$type = "image";
}
if (strlen($_FILES['inputUpProfile']['name']) > 0) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('inputUpProfile')) {
$error = $this->upload->display_errors();
if (is_array($error)) {
foreach ($error as $er) {
$this->errors[] = $er;
}
} else {
$this->errors[] = $error;
}
} else {
$updata = $this->upload->data();
$imagePath = './uploads/' . $eventpic;
if (file_exists($imagePath)) {
#unlink($imagePath);
}
$eventpic = $updata['raw_name'] . $updata['file_ext'];
}
}
$result = $this->blog->addPost($_SESSION['user_id'], $type, $this->input->post('post'), $eventpic);
}
$result = $this->blog->getPosts($_SESSION['user_id'], 0, 10);
$this->template->build("home_view", array("response" => $result));
}
and view is like this :-
<div class="textstatus">
<input id="inputUpProfile" name="inputUpProfile"
class="inputUpProfile hidefile" type="file"/>
<input type="button" id="PicUpProfile" class="sentpic" value="addpic">
<input name="post" type="text" id="text" placeholder="message ...">
<input type="submit" id="sent" value="Send">
</div>
</form>
</div>
when i upload thee images in my site the error is display like :-
Severity: Notice
Message: Undefined index: inputUpProfile
Filename: controllers/home.php
Line Number: 47
and
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: eventpic
Filename: controllers/home.php
Line Number: 74
and
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\sys\system\core\Exceptions.php:185)
Filename: core/Common.php
Line Number: 442
so where are the problem in my code.
/****************
edit :-
Array ( [upload_data] => Array ( [file_name] => 67429_133961013479569_306349156_n3.jpg [file_type] => image/jpeg [file_path] => D:/AppServ/www/d5n/rashaqa2/uploads/ [full_path] => D:/AppServ/www/d5n/rashaqa2/uploads/67429_133961013479569_306349156_n3.jpg [raw_name] => 67429_133961013479569_306349156_n3 [orig_name] => 67429_133961013479569_306349156_n.jpg [client_name] => 67429_133961013479569_306349156_n.jpg [file_ext] => .jpg [file_size] => 34.05 [is_image] => 1 [image_width] => 720 [image_height] => 540 [image_type] => jpeg [image_size_str] => width="720" height="540" ) )
i need from this array [file_name] to save in DB, how can i read this.
if($_FILES['nameofinputtype']['error'] == UPLOAD_ERR_OK)
{
// A file was uploaded
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$imagen = $this->upload->do_upload();
}