Welcome Guest, Not a member yet? Register   Sign In
Upload Class
#1

[eluser]JohnS901[/eluser]
Hello!

I have some problem using the upload class

upload.php

Code:
<?php

class Upload extends Controller {
    
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    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);
        }
    }    
}
?>

upload_succes.php

Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</h3>

<ul>
&lt;?php foreach($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

<p>&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;

upload_form.php

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo $error;?&gt;

&lt;?php echo form_open_multipart('upload/do_upload');?&gt;

&lt;input type="file" name="userfile" size="20" /&gt;

<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

First time I tried i got redirected to example.com so i changed in config to http://localhost.

When I upload image:

RESULT: 10
2

URL:http://localhost/upload/index.php/upload/do_upload

But my main URL is: http://localhost/email/index.php/upload

HUH??


What to do?
#2

[eluser]tomcode[/eluser]
It's because You point Your form to http://localhost/upload/index.php/upload/do_upload :

The form_open_multipart('upload/do_upload') resolves to Your base url setting + 'upload/do_upload'.

So I guess Your base url should be 'http://localhost/email/'.

The base url setting is always with a trailing slash.

Or did I got something wrong ?

By the way, when building a new app, You could test with a simple second controller and a link in the welcome view whether all Your base config settings are good Wink
#3

[eluser]JohnS901[/eluser]
Thank you, thank you! Confusednake:

Just a ending question: How do I just put out the link to the images? And is this script "secured"?

Oh and Is there anyway to change the error msg to like sweden?
#4

[eluser]tomcode[/eluser]
Quote:How do I just put out the link to the images?

Code:
&lt;?php
$base_url = base_url();
// or
$base_url = $this->config->item('base_url');

$image_path = $base_url .'uploads/' .$upload_data['file_name'];
?&gt;
<a href="&lt;?php echo $image_path; ?&gt;"><img src="&lt;?php echo $image_path; ?&gt;" alt="" /></a>

Quote:And is this script “secured”?
I do not know. In the image contained viruses will still be present.


Quote:Oh and Is there anyway to change the error msg to like sweden?
You can change the language system-wide to Swedish, You need to install the Swedish language from
Wiki languages, make sure that all strings are present / translated.
#5

[eluser]JohnS901[/eluser]
Nevermind - thanks.
#6

[eluser]tomcode[/eluser]
The Swedish language pack is an for old version (1.6), You'll need to compare the strings / files with the English version and add the missing strings / files. Also make sure :

1. have it copied into the right place
2. the name given in the config/config.php must be the same than the folder name containing the language pack

see User Guide, Language class




Theme © iAndrew 2016 - Forum software by © MyBB