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

Symfony2 login issue using dynamic login url
Sorting arrays by key number PHP
Facebook api, sdk 4.0, php - uploading a photo from the server to user's facebook
Convert integer value into years, months, weeks and days
MongoDB: Atomic Updates & Array Comparisons
Doctrine 2: Hydrate key-value array
PHP/html Get li's content
How to host a Laraval 4 project on a domain name?
Most Efficient Way To Run A Query On PROCEDURE Results
Set the cookie so that it is recognized in users
Exporting 2 values with one input with html form
PDO LIMIT with default function value
Regex to match paths
Php - allow space in rewriteRule
linking jpeg image in html to php code
Symfony - Using twig extension with form prevents errors from showing up

Categories

HOME
android-studio
blast
bokeh
opencv-python
command-prompt
google-webmaster-tools
fedora
phpmqtt
telegraf
timber
tup
port
roku
stack-overflow
migrate
cqrs
alljoyn
amazon-emr
cname
createjs
infrared
kamailio
fluent-migrator
window-managers
hdmi
xdebug
jackrabbit-oak
django-import-export
microprocessors
amazon-iam
vcenter
commonsware-cwac
delayed-job
john-the-ripper
lxml
cups
keyboard-layout
cheat-engine
suds
ol3-google-maps
photoswipe
renderman
winston
pen
ptvs
streamwriter
okio
livecycle
system.net.mail
gcloud-node
between
piping
eclemma
dnx50
pythonxy
postal-code
tree-structure
ssmtp
datagridcomboboxcolumn
flurry-analytics
rebar
object-code
quantify
angular-ui-select
siena
redpitaya
incognito-mode
refit
tabletools
assetic
discovery
frontbase
django-facebook
disjoint-union
objective-c-runtime
session-0-isolation
idispatch
msinfo32
kaazing
pcspim
hardcode
mkannotation
acpi
fbml
microblogging
drawimage
getopts
directshow.net
digiflow
jmenu
qcar-sdk
formal-semantics
ninject-interception
resharper-5.1
fgetc
google-translator-toolkit
xap
icon-language
regioninfo

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App