php


Codeigniter can't read file name of uploaded file


Trying to read the file name of a file that is uploaded as part of a form. The print_r command that I'm using to test just shows a blank screen. I have read the manual (near the bottom here) pertaining to this and don't understand what I'm doing wrong.
Controller:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
$file_name = $this->upload->data('file_name');
print_r($file_name);
View:
<?php echo form_open_multipart('Corpmuns/do_upload', array('method' => 'post'));?>
... // some drop-down menus and text fields here
<INPUT TYPE="file" NAME="userfile" id="userfile" >
</form>
You didn't do the upload action so the file was not uploaded yet. That's why you can't get the uploaded file's name. Because it does not exists.
Code $this->upload->do_upload('userfile') and make sure the file is uploaded successfully before you get the filename.
Call do_upload function from upload library.
Update Controller:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) { //use this function
$data['error'] = false;
$upload_data = $this->upload->data();
$data['data'] = $upload_data;
$data['msg'] = 'Image Successfully Uploaded.';
} else {
$data['msg'] = $this->upload->display_errors('', '<br>');
}
print_r($data)
}
if ( ! $this->upload->do_upload('input_name'))
{
echo $this->upload->display_errors();
}
else
{
$file=$this->upload->data();
echo $image=$file['file_name'];//Set file name to varilable
}
}
<form action="" enctype="multipart/form-data" method="post"
name="uploadfile">
you should add enctype="multipart/form-data"

Related Links

how to prevent directly enter in URL and browser refresh button when using onbeforeunload function
How to check entered url is indexing on google or not using php
how to move an image from folder and resize before saving to another folder?
multi items setup in PayPal php sdk
Codeigniter Image upload issue
Cannot insert values in database tables
Editing data in multiple columns from front-end
mpdf watermark - inclined to vertical
how to convert a json data according to requirement?
error creating dynamic events and ykeys in morris line chart
How to display jQuery variables value in PHP on the same page?
Check a 2D associated array for a string, return the information (PHP)
single-{post-type}.php is not working in wordpress
Multiple post parts in same page
PHP Method Chaining without writing lots of return this
how do i reference a dynamically created list php

Categories

HOME
visual-studio
initialization
typeerror
mathematical-optimization
google-sheets-query
greasemonkey
supercollider
angular2-template
tweepy
spring-security-oauth2
graphdb
feature-extraction
squid
texas-instruments
l20n
string-formatting
codefluent
renderscript
pfobject
raml
azure-machine-learning
sipp
restier
cellular-network
points
jackrabbit-oak
mdns
envoy
winsock
seafile-server
devtools
polyml
db2-luw
buildforge
netstat
assertions
ms-solver-foundation
forms-authentication
recovery
python-sounddevice
windows-store
distributed-caching
1wire
yoast
solr-query-syntax
pecl
msxml
kendo-treeview
mifos
pjax
bbc-micro
meld
noclassdeffounderror
apple-configurator
frame-grab
utf-16
myfaces
servicestack-bsd
godeps
msgpack
fanotify
screen-orientation
search-form
uitest
sequence-sql
rfc5545
thoughtworks-go
string.format
visual-studio-6
nodeload
xmi
jchartfx
code39
codahale-metrics
objective-c-runtime
microformats
chefspec
system-requirements
taskkill
gwt-celltable
background-repeat
differentiation
snackjs
jquery-1.4
airprint
django-pagination
hungarian-notation
tessellation
google-translator-toolkit
spring-modules

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App