Thread View: pl.comp.lang.php
6 messages
6 total messages
Started by jivanmukta
Wed, 11 Feb 2015 17:00
MultiUpload plików o rozszerzeniu bmp (CodeIgniter)
Author: jivanmukta
Date: Wed, 11 Feb 2015 17:00
Date: Wed, 11 Feb 2015 17:00
28 lines
2780 bytes
2780 bytes
Mam problem z multi-uploadem plików o rozszerzeniu bmp. Wywala mi się błąd, że nie można wgrywać plików o takim rozszerzeniu. Nie wiem o co chodzi, bo podałem jako jedno z rozszerzeń do uploadu właśnie bmp. Np. przy rozszerzeniu jpg wszystko idzie gładko.
Używam biblioteki MY_Upload, którą ściągnąłem z Internetu:
/**
* Multi-Upload
*
* Extends CodeIgniters native Upload class to add support for multiple
* uploads.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Uploads
* @author Conveyor Group <steven@conveyorgroup.com>
* @link https://github.com/stvnthomas/CodeIgniter-2.1-Multi-Upload
*/
Mam zdefiniowane stałe:
define('IMAGE_EXTS', 'gif|jpg|jpeg|jpe|bmp|png|gif87|gif89a');
define('MOVIE_EXTS', 'mpg|mpeg|mov|avi|wmv|vob|rm|rmvb|ogg|ogm');
Używam biblioteki tak:
$init['upload_path'] = $dir;
$init['allowed_types'] = IMAGE_EXTS . '|' . MOVIE_EXTS;
$init['max_size'] = '1024'; // in kB
$init['max_width'] = '1440';
$init['max_height'] = '900';
$this->upload->initialize($init);
if (!$this->upload->do_multi_upload('property_pictures')) {
$this->common->error(ERRMSG_UPLOAD_FAILED . ' ' . $this->upload->display_errors());
}
Nie działa mi debuggowanie PHP na komputerze, więc proszę o pomoc. Może ktoś z Was ma doświadczenia w MultiUploadzie plików w CodeIgniterze.
Re: MultiUpload plików o rozszerzeniu bmp (CodeIgniter)
Author: Borys Pogoreło
Date: Wed, 11 Feb 2015 19:24
Date: Wed, 11 Feb 2015 19:24
14 lines
1413 bytes
1413 bytes
Dnia Wed, 11 Feb 2015 09:00:55 -0800 (PST), jivanmukta@poczta.onet.pl napisał(a): > Mam problem z multi-uploadem plików o rozszerzeniu bmp. Wywala mi się > błąd, że nie można wgrywać plików o takim rozszerzeniu. Nie wiem o co > chodzi, bo podałem jako jedno z rozszerzeń do uploadu właśnie bmp. Np. > przy rozszerzeniu jpg wszystko idzie gładko. Ale gdzie masz ten błąd? W przeglądarce? Wyświetlany przez CI? Jaki konkretnie? Z takim opisem problemu nikt nie zgadnie o co chodzi. -- Borys Pogoreło borys(#)leszno,edu,pl
Re: MultiUpload plików o rozszerzeniu bmp (CodeIgniter)
Author: jivanmukta
Date: Thu, 12 Feb 2015 05:12
Date: Thu, 12 Feb 2015 05:12
269 lines
9251 bytes
9251 bytes
> Ale gdzie masz ten błąd? W przeglądarce? Wyświetlany przez CI? Jaki
> konkretnie? Z takim opisem problemu nikt nie zgadnie o co chodzi.
W przeglądarce mam:
An Error Was Encountered
Nie powiodĹo siÄ wgranie obrazkĂłw.
Typ pliku ktĂłry prĂłbujesz wgraÄ jest niedozwolony.
Wydaje mi si (nie mam debuggowania), że może chodzić o instrukcję z application/libararies/MY_Upload.php:
public function do_multi_upload($field){
...
//Determine error number.
$error = (!isset($_FILES[$field]["error"][$i])) ? 4 : $_FILES[$field]["error"][$i];
//Set error.
switch($error){
//UPLOAD_ERR_INI_SIZE
case 1:
$this->set_error("upload_file_exceeds_limit");
break;
//UPLOAD_ERR_FORM_SIZE
case 2:
$this->set_error("upload_file_exceeds_form_limit");
break;
//UPLOAD_ERR_PARTIAL
case 3:
$this->set_error("upload_file_partial");
break;
//UPLOAD_ERR_NO_FILE
case 4:
$this->set_error("upload_no_file_selected");
break;
//UPLOAD_ERR_NO_TMP_DIR
case 6:
$this->set_error("upload_no_temp_directory");
break;
//UPLOAD_ERR_CANT_WRITE
case 7:
$this->set_error("upload_unable_to_write_file");
break;
//UPLOAD_ERR_EXTENSION
case 8:
$this->set_error("upload_stopped_by_extension");
break;
default:
$this->set_error("upload_no_file_selected");
break;
}
//Return failed upload.
return FALSE;
...
Funkcja ta wywołuje do_upload() z system/libraries/Upload.php.
Właściwość $this->allowed_types jest ustawiana przez wywołanie $this->upload->initialize($init);
W system/libraries/Upload.php mam:
/**
* Verify that the filetype is allowed
*
* @return bool
*/
public function is_allowed_filetype($ignore_mime = FALSE)
{
if ($this->allowed_types == '*')
{
return TRUE;
}
if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
{
$this->set_error('upload_no_file_types');
return FALSE;
}
$ext = strtolower(ltrim($this->file_ext, '.'));
if ( ! in_array($ext, $this->allowed_types))
{
return FALSE;
}
// Images get some additional checks
$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
if (in_array($ext, $image_types))
{
if (getimagesize($this->file_temp) === FALSE)
{
return FALSE;
}
}
if ($ignore_mime === TRUE)
{
return TRUE;
}
$mime = $this->mimes_types($ext);
if (is_array($mime))
{
if (in_array($this->file_type, $mime, TRUE))
{
return TRUE;
}
}
elseif ($mime == $this->file_type)
{
return TRUE;
}
return FALSE;
}
public function do_upload($field = 'userfile')
{
// Is $_FILES[$field] set? If not, no reason to continue.
if ( ! isset($_FILES[$field]))
{
$this->set_error('upload_no_file_selected');
return FALSE;
}
// Is the upload path valid?
if ( ! $this->validate_upload_path())
{
// errors will already be set by validate_upload_path() so just return FALSE
return FALSE;
}
// Was the file able to be uploaded? If not, determine the reason why.
if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
{
$error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];
switch($error)
{
case 1: // UPLOAD_ERR_INI_SIZE
$this->set_error('upload_file_exceeds_limit');
break;
case 2: // UPLOAD_ERR_FORM_SIZE
$this->set_error('upload_file_exceeds_form_limit');
break;
case 3: // UPLOAD_ERR_PARTIAL
$this->set_error('upload_file_partial');
break;
case 4: // UPLOAD_ERR_NO_FILE
$this->set_error('upload_no_file_selected');
break;
case 6: // UPLOAD_ERR_NO_TMP_DIR
$this->set_error('upload_no_temp_directory');
break;
case 7: // UPLOAD_ERR_CANT_WRITE
$this->set_error('upload_unable_to_write_file');
break;
case 8: // UPLOAD_ERR_EXTENSION
$this->set_error('upload_stopped_by_extension');
break;
default : $this->set_error('upload_no_file_selected');
break;
}
return FALSE;
}
// Set the uploaded data as class variables
$this->file_temp = $_FILES[$field]['tmp_name'];
$this->file_size = $_FILES[$field]['size'];
$this->_file_mime_type($_FILES[$field]);
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
$this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
$this->file_name = $this->_prep_filename($_FILES[$field]['name']);
$this->file_ext = $this->get_extension($this->file_name);
$this->client_name = $this->file_name;
// Is the file type allowed to be uploaded?
if ( ! $this->is_allowed_filetype())
{
$this->set_error('upload_invalid_filetype');
return FALSE;
}
// if we're overriding, let's now make sure the new name and type is allowed
if ($this->_file_name_override != '')
{
$this->file_name = $this->_prep_filename($this->_file_name_override);
// If no extension was provided in the file_name config item, use the uploaded one
if (strpos($this->_file_name_override, '.') === FALSE)
{
$this->file_name .= $this->file_ext;
}
// An extension was provided, lets have it!
else
{
$this->file_ext = $this->get_extension($this->_file_name_override);
}
if ( ! $this->is_allowed_filetype(TRUE))
{
$this->set_error('upload_invalid_filetype');
return FALSE;
}
}
// Convert the file size to kilobytes
if ($this->file_size > 0)
{
$this->file_size = round($this->file_size/1024, 2);
}
// Is the file size within the allowed maximum?
if ( ! $this->is_allowed_filesize())
{
$this->set_error('upload_invalid_filesize');
return FALSE;
}
// Are the image dimensions within the allowed size?
// Note: This can fail if the server has an open_basdir restriction.
if ( ! $this->is_allowed_dimensions())
{
$this->set_error('upload_invalid_dimensions');
return FALSE;
}
// Sanitize the file name for security
$this->file_name = $this->clean_file_name($this->file_name);
// Truncate the file name if it's too long
if ($this->max_filename > 0)
{
$this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
}
// Remove white spaces in the name
if ($this->remove_spaces == TRUE)
{
$this->file_name = preg_replace("/\s+/", "_", $this->file_name);
}
/*
* Validate the file name
* This function appends an number onto the end of
* the file if one with the same name already exists.
* If it returns false there was a problem.
*/
$this->orig_name = $this->file_name;
if ($this->overwrite == FALSE)
{
$this->file_name = $this->set_filename($this->upload_path, $this->file_name);
if ($this->file_name === FALSE)
{
return FALSE;
}
}
/*
* Run the file through the XSS hacking filter
* This helps prevent malicious code from being
* embedded within a file. Scripts can easily
* be disguised as images or other file types.
*/
if ($this->xss_clean)
{
if ($this->do_xss_clean() === FALSE)
{
$this->set_error('upload_unable_to_write_file');
return FALSE;
}
}
/*
* Move the file to the final destination
* To deal with different server configurations
* we'll attempt to use copy() first. If that fails
* we'll use move_uploaded_file(). One of the two should
* reliably work in most environments
*/
if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
{
if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
{
$this->set_error('upload_destination_error');
return FALSE;
}
}
/*
* Set the finalized image dimensions
* This sets the image width/height (assuming the
* file was an image). We use this information
* in the "data" function.
*/
$this->set_image_properties($this->upload_path.$this->file_name);
return TRUE;
}
Re: MultiUpload plików o rozszerzeniu bmp (CodeIgniter)
Author: Borys Pogoreło
Date: Thu, 12 Feb 2015 18:34
Date: Thu, 12 Feb 2015 18:34
37 lines
1971 bytes
1971 bytes
Dnia Wed, 11 Feb 2015 21:12:53 -0800 (PST), jivanmukta@poczta.onet.pl
napisał(a):
>> Ale gdzie masz ten błąd? W przeglądarce? Wyświetlany przez CI? Jaki
>> konkretnie? Z takim opisem problemu nikt nie zgadnie o co chodzi.
>
> W przeglądarce mam:
>
> An Error Was Encountered
> Nie powiodĹo siÄ wgranie obrazkĂłw.
> Typ pliku ktĂłry prĂłbujesz wgraÄ jest niedozwolony.
I to jest w całości komunikat z CI?
> Wydaje mi si (nie mam debuggowania), że może chodzić o instrukcję z application/libararies/MY_Upload.php:
Zawsze masz jakąś formę debugowania. Choćby var_dump() w kodzie.
> $mime = $this->mimes_types($ext);
> if (is_array($mime))
> {
> if (in_array($this->file_type, $mime, TRUE))
> {
> return TRUE;
> }
> }
> elseif ($mime == $this->file_type)
> {
> return TRUE;
> }
Podejrzewam, że coś tutaj nie zaskakuje dla formatu bmp. Ale to musisz
niestety sam przeanalizować.
--
Borys Pogoreło
borys(#)leszno,edu,pl
Re: MultiUpload plików o rozszerzeniu bmp (CodeIgniter)
Author: jivanmukta
Date: Fri, 13 Feb 2015 15:16
Date: Fri, 13 Feb 2015 15:16
12 lines
2002 bytes
2002 bytes
> > W przeglądarce mam:
> >
> > An Error Was Encountered
> > Nie powiodĹo siÄ wgranie obrazkĂłw.
> > Typ pliku ktĂłry prĂłbujesz wgraÄ jest niedozwolony.
>
> I to jest w całości komunikat z CI?
Nie, wyświetlam go sam:
if (!$this->upload->do_multi_upload('property_pictures')) {
$this->common->error(ERRMSG_UPLOAD_FAILED . ' ' . $this->upload->display_errors());
}
Re: MultiUpload plików o rozszerzeniu bmp (CodeIgniter)
Author: jivanmukta
Date: Sat, 14 Feb 2015 15:41
Date: Sat, 14 Feb 2015 15:41
11 lines
1968 bytes
1968 bytes
> Podejrzewam, że coś tutaj nie zaskakuje dla formatu bmp. Ale to musisz
> niestety sam przeanalizować.
Tak. W funkcji is_allowed_filetype:
$this->file_type == 'image/x-ms-bmp', bo tak zwraca finfo_file()
natomist $mimes w application/config/mimes.php:
'bmp' => array('image/bmp', 'image/x-windows-bmp'),
Dodałem 'image/x-ms-bmp' do application/config/mimes.php i teraz już działa.
Sprawdzę upload dla pozostałych rozszerzeń.
Dzięki za pomoc. Problem zamknięty.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads