I'm have a problem uploading a picture using codeigniter. I followed the online upload tutorial, but can't figure out the problem. I keep getting "The upload path does not appear to be valid". So, here's my form structure:
<?php if (isset($error)) {?>
<div class="col-md-12">
<div class="alert alert-danger" role="alert">
<?= $error ?>
</div>
</div>
<?php } ?>
<?php echo form_open_multipart('pages/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
I also checked for my directory to be a valid path and to be writable with this code:
<?php if(is_writable('./uploads') && is_dir('./uploads')){
echo 'valid';
}
else { echo (base_url('uploads'). ' ' ."is not writable");} ?>
And this returns valid.
The following is my controller structure:
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000000000;
$config['max_width'] = 1920;
$config['max_height'] = 1080;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('user/profile/profile', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
In construct i loaded the upload library.
With all this being said, I keep getting the error I mentioned before.
Try Using
$config["upload_path"] = $_SERVER['DOCUMENT_ROOT']."/your/desired/location/";
Related
I'm trying to upload a file , but file name is a variable,so to do that I have get the name of input type(i can do it by getting all data on post and run a loop and get the keyname)
then I'm getting all post data , but then it returns me a empty array,when I remove multipart/form data from the form it will return the array with the value.then I can get the file type name, but it doesn't upload the file because I remove the multipart from the form.
How can I do this? if read the code u will understand. Please help me to do this.
This is my view page
public function addTest($app_id)
{
$data=$this->input->post();
$id=$this->session->userdata['loggedHospitalData']['hp_id'];
while ($test = current($data)) {
$dataArr=array(
"hospital_id" => $id,
);
$test_id=key($data);
$wherArr = array("test_id"=>$test_id);
//$result = $this->Hospital_model->updateTestData($dataArr,$wherArr);
$report=$this->input->post($test_id);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = 5000000000000;
$config['max_width'] = 10240;
$config['max_height'] = 10240;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($report))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data1 = array('upload_data' => $this->upload->data());
}
/*$dataRepArr=array(
"r_test_id " => $test_id,
"file" => $photo1,
);*/
next($data);
//header('Location: http://localhost/docto/index.php/Hospital/patientView/'.$app_id);
}
}
And this my View
<form action="<?php echo base_url(); ?>index.php/Hospital/addTest/<?php echo $appoinmentData->app_id; ?>" role="form" method='post' onmouseover="return form_validate()" name="vform">
<div class="">
<ul class="to_do">
<?php
foreach ($testData as $test) {
echo '
<li>
<p>
'.$test->test_name.' </p>
</br>
<input type="file" class="btn btn-primary" name="'.$test->test_id.'">
</li>
';
}
?>
</ul>
</div>
<input type="submit" class="btn btn-success btn-lg btn-block" value="Update" name="">
</form>
You need multipart/form-data for the rules to be uploaded. Put it back.
Then loop over $_FILES to get all the files that were uploaded.
multipart/form-data MUST be there for file uploads
You can try something like this:
$this->load->library('upload');
$config['upload_path'] = './assets/uploads/'
$config['allowed_types'] = 'jpg|png|jpeg|pdf';
$config['max_size'] = '0';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
foreach($_FILES as $k => $v)
{
if ($_FILES[$k]['name'] != '')
{
//This do the trick, do_upload need the name of the input
//And in this case, name is the KEY of an array.
if($this->upload->do_upload($k))
{
//Upload Successful
}
}
}
I want to upload file in folder, following is my code for upload file in folder, the code is successfully working in my local system and also file moved to folder but on server side i got 'The upload path does not appear to be valid'. when i used $config['upload_path'] = './uploads/'; but when i used $config['upload_path'] = './assets/images/store/category'; i got success message but uploaded file is not showing in category folder.
Following is my code.
Controller
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
echo "Tst";
$this->load->view('Upload_form', array('error' => ' ' ));
}
public function Test_do_upload() {
$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);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('Upload_form', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
echo '<pre>';
print_r($data);
// $this->load->view('upload_success', $data);
}
}
}
?>
View ( form)
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/Test_do_upload');?>
<input type = "file" name = "userfile" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
<?= form_close(); ?>
</body>
</html>
View of success upload
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?phpforeach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?phpendforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
Code is working in my local system but on server side file not move to folder. I also given permission.
I can see there are two form tag could fire this problem. So revise it
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<form action ="<?php echo site_url('upload/Test_do_upload');?>" method="post" enctype="multipart/form-data">
<input type = "file" name = "userfile" size = "20" />
<br /><br />
<input type = "submit" value = "upload" />
</form>
</body>
</html>
May be try this code and try to create directory if its not created. Else print $error variable to find out what kind of upload error. May be this will be helpful. And remove form tag from your view its repeated.
$dir_path= 'assets/admin/uploads/course_images/';
if (!is_dir($dir_path)) {
mkdir($dir_path, 0777, TRUE);
}
$new_name = time().$this->input->post('Course_code');
//move_uploaded_file($_FILES['file']['tmp_name'], $dir_path);
$config['upload_path'] = $dir_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = FALSE;
$config['max_size'] = '1000';
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('file'))
{
// no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
/*Print Error*/
}else{
/*Insert Code*/
}
Problem is When upload a image its shows same error every time "The upload path does not appear to be valid". Tried so much solution but still same problem with it..Please help.
Controller:
public function updateprofile()
{
/***get current user data***/
$this->load->model('Admin_main','userinfo');
$data=$this->userinfo->getuserdata();
$this->load->model('Admin_main','updateprofile');
$result=$this->updateprofile->updatemyprofile();
if($data->identity=='admin')
{
redirect('admin/myprofile');
}
else
{
redirect('campuser/myprofile');
}
}
Model:
if($_FILES['fileToUpload']['name']!='')
{
$config["upload_path"] = './uploads/';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('fileToUpload')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$mypic = array('profile_pic'=>$picture);
$this->db->set($mypic);
$this->db->where('id', $userid);
$uppic=$this->db->update('mokhayam_users');
}
else
{
echo $this->upload->display_errors();
}
}
View:
<form class="form-horizontal" action="admin/updateprofile" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label">Profile Picture</label>
<div class="col-sm-10">
<label for="fileToUpload"><img src="assets/admin/dist/img/gul.jpg" id="blah1" class="profile-user-img img-responsive img-circle"/></label><input type="file" name="fileToUpload" id="fileToUpload" onchange="$('#blah1')[0].src = window.URL.createObjectURL(this.files[0])">
</div>
</div>
<input type="hidden" name="userid" value="2">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Thanks in advance..
If your codeigniter version is 3.x please change library file (system/library/upload.php) version codeigniter 3.x to 2.x
and then try
$config["upload_path"] = APPPATH.'/uploads/';
......
Without changing anything in your library, the solution is to initialize the config just after loading the upload library in your controller, follow this code
// Upload Image
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '7048';
$config['max_width'] = '7000';
$config['max_height'] = '7000';
// Create folder (Uploads) if not exists
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path']);
}
// Load library Upload
$this->load->library('upload', $config);
// Initialize the Upload library with current $config
$this->upload->initialize($config);
$this->upload->do_upload();
if ($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
$page_image = $_FILES['userfile']['name'];
} else {
$errors = array('error' => $this->upload->display_errors());
$page_image = 'noimage.jpg';
}
// Set message
$this->session->set_flashdata('page_created', 'Your File(s) has been uploaded');
I am trying to upload photo using codeigniter but that is not working. I don't get file name it display only error.The helper files also available in my system folder but I do not upload photo.
Controller: upload.php
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url','file'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$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);
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);
}
}
}
?>
view: upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
upload_success.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
</body>
</html>
I always specify the input field name when calling do_upload() method. This is a fragment from working example, follow the pattern and it should work.
$field_name = 'userfile';
if (isset($_FILES[$field_name]) && is_uploaded_file($_FILES[$field_name]['tmp_name'])) {
$this->load->library('upload', $config);
if (!$this->upload->do_upload($field_name)) {
//upload failed
} else {
//upload passed
}
}
Also, make sure your upload directory is writable.
Here is my code and i dont know why its not working i already have mimes.php of csv file
I don't know what is wrong i already use this code in other function its working but in here it's not working.
Controller:
function convert_csv(){
$config['upload_path'] = './files/contracts/';
$config['allowed_types'] = 'pdf|csv';
$config['max_size'] = '4096';
$this->load->library('upload', $config);
$this->upload->display_errors('', '');
if ( !$this->upload->do_upload("csvfile")){
$error = array('error' => $this->upload->display_errors('<span>','</span>'));
$this->session->set_flashdata("upload_message", $error);
} else{
$upload_result = $this->upload->data();
}
}
View:
<?php $form_options = array( 'id' => 'contract_items',
'class' => 'form-horizontal' ); ?>
<?=$this->formbuilder->open( $controller.'/convert_csv/'.$c->id, TRUE, $form_options );?>
<?php echo form_open_multipart('upload/do_upload');?>
Select File To Upload:<br />
<input type="file" name="csvfile" value="csvfile" text="csvfile" />
<br /><br />
<input type="submit" value="Submit" />
function convert_csv(){
$config['upload_path'] = './files/contracts/';
$config['allowed_types'] = 'pdf|csv';
$config['max_size'] = '4096';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload("csvfile")){
echo $this->upload->display_errors();
} else{
$upload_result = $this->upload->data();
var_dump($upload_result);
}
}
Try this. At least you'll find what error going on there