Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate upload errors on CodeIgniter

I am using CodeIgniter to upload some files through a form. I know how to translate "regular" form errors (required, valid_email, etc) but I don't know how to do it with file errors (file is not allowed, file is too big, etc).

Which keys should I use in

$this->form_validation->set_message('KEY', 'TRANSLATION');

?

Thanks

like image 726
Cmorales Avatar asked Feb 24 '26 18:02

Cmorales


1 Answers

METHOD 1

By default, language files are typically stored in system/language directory. Alternately you can create a file called upload_lang.php inside your application/language folder and store them there.

$lang['upload_userfile_not_set']        = "Unable to find a post variable called userfile.";
$lang['upload_file_exceeds_limit']      = "The uploaded file exceeds the maximum allowed size in your PHP configuration file.";
$lang['upload_file_exceeds_form_limit'] = "The uploaded file exceeds the maximum size allowed by the submission form.";
$lang['upload_file_partial']            = "The file was only partially uploaded.";
$lang['upload_no_temp_directory']       = "The temporary folder is missing.";
$lang['upload_unable_to_write_file']    = "The file could not be written to disk.";
$lang['upload_stopped_by_extension']    = "The file upload was stopped by extension.";
$lang['upload_no_file_selected']        = "You did not select a file to upload.";
$lang['upload_invalid_filetype']        = "The filetype you are attempting to upload is not allowed.";
$lang['upload_invalid_filesize']        = "The file you are attempting to upload is larger than the permitted size.";
$lang['upload_invalid_dimensions']      = "The image you are attempting to upload exceedes the maximum height or width.";
$lang['upload_destination_error']       = "A problem was encountered while attempting to move the uploaded file to the final destination.";
$lang['upload_no_filepath']             = "The upload path does not appear to be valid.";
$lang['upload_no_file_types']           = "You have not specified any allowed file types.";
$lang['upload_bad_filename']            = "The file name you submitted already exists on the server.";
$lang['upload_not_writable']            = "The upload destination folder does not appear to be writable.";

METHOD 2

You can use Flashdata after upload process.

if ($this->upload->do_upload())
{
    $this->session->set_flashdata('success', 'Yep! Upload complete');
    redirect('go_back_home');
}
else
{
    $this->session->set_flashdata('error', 'Uh, upload not complete! The problem is..'); // Manually check
    redirect('go_back_form');
}

You can also check the problem with $this->upload->data(). This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype :

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)
like image 195
Wahyu Kristianto Avatar answered Mar 01 '26 20:03

Wahyu Kristianto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!