CodeIgniter Forums
Problem uploading picture - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Problem uploading picture (/thread-74330.html)



Problem uploading picture - mandiatutti - 09-11-2019

hi everyone! I have a profile view in which you can change the profile picture. I'd like to create this behaviour: 
- The relative pic path will be saved in the user table under the avatar column, 
- The actual picture will be saved on the server in assets/profiles folder

So i read the tutorial, and I have some problem making the folder writable apparently (I'm on mac)
PHP Code:
<?php if(is_writable(base_url('assets/profiles'))){
    echo 'writable';
}
else { 
echo  (base_url('assets/profiles'). ' ' ."is not writable");} ?>
 This code is always returning me that is not writable, but on terminal i have these permissions on the folder: drwxrwxrwx, which I obtained with chmod 777 profiles/

But let's go on... This is my form: 
PHP Code:
<div class="file btn btn-lg btn-primary">
                            Change photo
                            
<?php echo form_open_multipart('pages/do_upload');?>
                                <input type="file" name="avatar"/>
                                <input type="submit" value="upload"/>
                            </div> 

This is my controller: 
PHP Code:
public function do_upload()
        {       
                $image_path 
= realpath(APPPATH . '.uploaded');
                $config['upload_path']          = $image_path;
                $config['allowed_types']        = 'gif|jpg|jpeg|png';
                $config['max_size']             = 10000000000;
                $config['max_width']            = 1920;
                $config['max_height']           = 1080;

                $this->load->library('upload', $config);
                $this->upload->initialize($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);
                }
        

Now, I know that it's not doing what I want (yet), since I'm firstly trying to upload the picture... Later I'll think of storing the path in my table, and deleting the old profile pic, but I think I'm capable of thing it... The problem is uploading the damn picture xD

UPDATE: I keep getting a message 'The upload path does not appear to be valid.' But according to my computer I have permissions... Could it be a problem related to docker or lamp server? 


RE: Problem uploading picture - InsiteFX - 09-11-2019

Try adding the forward slash to the end of the upload path.

PHP Code:
// change this
$config['upload_path'] = './assets/images/profiles';

// to this
$config['upload_path'] = './assets/images/profiles/'

See if the works.

If not check your upload path that it is correct.


RE: Problem uploading picture - mandiatutti - 09-11-2019

(09-11-2019, 08:14 AM)InsiteFX Wrote: Try adding the forward slash to the end of the upload path.

PHP Code:
// change this
$config['upload_path'] = './assets/images/profiles';

// to this
$config['upload_path'] = './assets/images/profiles/'

See if the works.

If not check your upload path that it is correct.
I made it even simpler... I changed my path in './uploads/' (as in codeigniter tutorial) but nothing... Moreover i created that folder with the command 'mkdir 777 uploads'. Still not working tho... Can it be related to .htaccess file?


RE: Problem uploading picture - InsiteFX - 09-11-2019

Could be but I use an assets folder under my public_html and have no problems
accessing it.


RE: Problem uploading picture - includebeer - 09-12-2019

(09-11-2019, 03:21 AM)mandiatutti Wrote:
PHP Code:
<?php if(is_writable(base_url('assets/profiles'))){
    echo 'writable';
}
else { 
echo  (base_url('assets/profiles'). ' ' ."is not writable");} ?>
 This code is always returning me that is not writable, but on terminal i have these permissions on the folder: drwxrwxrwx, which I obtained with chmod 777 profiles/

is_writable check if a local file or a directory is writable but you pass an URL! Don't use base_url, pass the path to the local directory.


RE: Problem uploading picture - mandiatutti - 09-16-2019

(09-12-2019, 12:58 PM)includebeer Wrote:
(09-11-2019, 03:21 AM)mandiatutti Wrote:
PHP Code:
<?php if(is_writable(base_url('assets/profiles'))){
    echo 'writable';
}
else { 
echo  (base_url('assets/profiles'). ' ' ."is not writable");} ?>
 This code is always returning me that is not writable, but on terminal i have these permissions on the folder: drwxrwxrwx, which I obtained with chmod 777 profiles/

is_writable check if a local file or a directory is writable but you pass an URL! Don't use base_url, pass the path to the local directory.
Right, i changed it... now is 'is_wirtable('./directory')' and it says "writable" but is still not uploading the picture.


RE: Problem uploading picture - mboufos - 09-16-2019

https://codeigniter.com/user_guide/libraries/file_uploading.html
from the example can you post the error ?
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());

$this->load->view('upload_form', $error);
}

can you post here the errors ?
and also i guess the form will be closed right ?


RE: Problem uploading picture - mandiatutti - 09-16-2019

(09-16-2019, 05:53 AM)mboufos Wrote: https://codeigniter.com/user_guide/libraries/file_uploading.html
from the example can you post the error ?
if ( ! $this->upload->do_upload('userfile'))
  {
    $error = array('error' => $this->upload->display_errors());

    $this->load->view('upload_form', $error);
}

can you post here the errors ?
and also i guess the form will be closed right ?
I updated my controller code in my question. I solved the error about the path (it said that the path didn't exist... look at controller to see how i solved it). Although, now i'm getting another error: The upload destination folder does not appear to be writable.
Now, I'm running a docker container called lamp. From the container exec i created the folder in which i'd like to upload the pictures with this syntax: mkdir 777 folder_name. I assigned the permission also to my user account, but it appears is not writable...