CodeIgniter Forums
File upload() problems - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: File upload() problems (/showthread.php?tid=43972)

Pages: 1 2


File upload() problems - El Forum - 07-29-2011

[eluser]brucebat[/eluser]
Hi all,

I have been using the example here to try and get some file upload.

http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Trying to get it to work all day and still no luck, was wondering if you guys with a spare pair of eyes could spot what I am doing wrong.

View:

Code:
...rest of the view

<div class="upload" style="display:block; float:left;" >
                
                <h3>Upload an electronic copy?</h3>
                <select name="upload" id="upload" style="display:block;">
                   <option value="No">No</option>
                   <option value="Yes">Yes</option>
                </select>
    
<div id="uploadarea" style="width:900px;float:left;display:none">
    &lt;?php
          //File upload of record if needed by user
        echo form_upload('userfile');
        echo '<hr/>';

               //Button for submitting form
              echo form_submit('submit', 'Submit Data');
    echo form_close();
                
    ?&gt;
</div>

The Controller:

Checks if user wants to upload then calls upload function



Code:
//if the user selected from pulldown "Yes"
//Uploads electronic record e.g. pdf, word, etc
if ($this->input->post('upload') == 'Yes')
{
$this->form_validation->set_rules('upload_record','required');
$this->upload_record();                
}


function upload_record()
            {
                $config['upload_path'] = '/records/'.$this->session->userdata['name'].'/';
                $config['allowed_types'] = '';


                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());
                    echo "failed";
                    echo $error;
                }
                
                else
                {
                    $data = array('upload_data' => $this->upload->data());

                    echo $data;
                }
            }


The controller is echoing "Failed" so the upload is not working obviously. Any suggestions. I have been through the example above with a tooth comb trying to spot mistakes.



-My folder "records" is in the root directory.
-My session variable "name" is set and has a default name of "Bob" so the config folder path should be "/records/bob/"

Thanks!


File upload() problems - El Forum - 07-29-2011

[eluser]LuckyFella73[/eluser]
I guess you don't get any error message?

Change this code block to see what CI is complaining about:
Code:
if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());
                    echo "failed";
                    echo $this->upload->display_errors(); // changed here
                }



File upload() problems - El Forum - 07-29-2011

[eluser]brucebat[/eluser]
The error message it is saying is you did not specify a file to upload?

I selected a file in my form? So what could this be?


File upload() problems - El Forum - 07-29-2011

[eluser]LuckyFella73[/eluser]
I can't see your whole view file so: do you have
the multipart attribute in your form opening tag?
Code:
enctype="multipart/form-data"



File upload() problems - El Forum - 07-29-2011

[eluser]brucebat[/eluser]
Nope, I will include it now and report back to you!


Okay I tried to add the multipart but still doesn't work:

Here is a more descriptive account of what I am trying to achieve.

Basically I want in my view for submitting data and uploading a file and that this would all be processed through the one form.

1
-User enters data
-User selects an electronic copy of the data a pdf or excel file
2
+Data in form gets sent to database
+File gets uploaded to site with an id that matches the record in the database

Here is the structure of my view
Code:
echo form_open('submit/validateform');    

//Code for other sections of the form
....

//Code for uploading a file associated with the data in form
<div class="upload" style="display:block; float:left;" >

        &lt;?php echo form_open_multipart('upload/do_upload');?&gt; //The bit I added!!
                
<h3>Upload an electronic copy?</h3>

<select name="upload" id="upload" style="display:block;>

                   <option value="No">No</option>
                   <option value="Yes">Yes</option>
           </select>

                <div id="uploadarea" style="width:900px;float:left;display:none">
              
&lt;?php
                //File upload of record if needed by user
                echo form_upload('userfile', set_value('userfile'));
                echo '<hr/>';
                
?&gt;
                </div>


&lt;/form&gt;

This might be an unorthodox way to do it, but I want to achieve it all in the one form/view so its easier for my users rather than do each task seperately.


File upload() problems - El Forum - 07-29-2011

[eluser]brucebat[/eluser]
Ok so I tried it with many file and it is still giving me no file selected.


I was wondering could this be to do with file permissions?
Also how do I set my file permission on localhost computer?

Thanks


File upload() problems - El Forum - 07-29-2011

[eluser]LuckyFella73[/eluser]
I don't know if that's the problem but you can't
have this:
Code:
echo form_upload('userfile', set_value('userfile'));

Should look like:
Code:
echo form_upload('userfile');

You can't set the value of a input "type file". When
the upload fails the user has to select the file again.
Check the parsed source-code in your browser
and be sure that the name of the input/file is "userfile".

I don't think it's a permission problem otherwise you should
get an error message like "the folder ... seems not to be writable".
Just in this sense Wink

On Windows you don't have to set a permission, on a Linux server
I guess you need 777 (CI Userguide say so)

I would set a "allowed_types" value btw. You have to set
at least one type or use a wildcard:
Code:
$config['allowed_types'] = '*'



File upload() problems - El Forum - 07-30-2011

[eluser]brucebat[/eluser]
Okay I have changed my code aroun a bit so what I have is a seperate form for uploading a file.

However now I am getting an error message
Quote:The upload path does not appear to be valid.

This is the code:

Code:
function upload_record()
            {
                
                $config['upload_path'] = '/records/';
                
                $config['allowed_types'] = '*';


                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());
                    echo "failed";
                    echo $this->upload->display_errors();
                }
                
                else
                {
                    $data = array('upload_data' => $this->upload->data());

                    echo $data;
                }
            }

My records folder is in the root directory

This is my baseurl

http://localhost/midas/

File path

C:\xampp\htdocs\midas

Screenshot:

http://i.imgur.com/9iDhL.jpg


File upload() problems - El Forum - 07-30-2011

[eluser]Ivar89[/eluser]
Try this:
Code:
$config['upload_path'] = './records/';



File upload() problems - El Forum - 07-30-2011

[eluser]brucebat[/eluser]
It works!!

So what does the "." do is it similar to ".." when you do file hrefs in HTML?