Currently if I supply no extensions to the class it allows no extensions. I would like to allow all extensions. Is there any way to do this without hacking the core?
In Codeigniter 2, you simply need to define allowed types like this :
$config['allowed_types'] = '*';
What I do is:
$ext=preg_replace("/.*\.([^.]+)$/","\\1", $_FILES['userfile']['name']);
$fileType=$_FILES['userfile']['type'];
$config['allowed_types'] = $ext.'|'.$fileType;
That makes all files in every function call automatically allowed.
The answer to your direct question: No, there's no way to do this without overriding the core
To good news is you can avoid hacking the core, per the manual
As an added bonus, CodeIgniter permits your libraries to extend
native classes if you simply need to add some functionality to
an existing library. Or you can even replace native libraries just
by placing identically named versions in your application/libraries folder.
So, to have a drop in replacement for your library, you could copy Upload.php to your
application/libraries
folder, and then add your custom logic to that Upload.php file. Code Igniter will include this file instead whenever you load the upload library.
Alternately, you could create your OWN custom uploader class that extends the original, and only refines the is_allowed_filetype function.
application/libraries/MY_Upload.php
class MY_Upload Extends CI_Upload{
function is_allowed_filetype(){
//my custom code here
}
}
You'll want to read over the changelog whenever you're upgrading, but this will allow you to keep your code and the core code in separate universes.
So far it looks like it would only be possible via a hack.
I inserted a return true on line 556 in system/libraries/Upload.php.
$config['allowed_types'] = '*';
Which Will Upload Any File Formats Like .exe or .jpegs extra...
if all not works then rearrange the allowed type order like that in first video format
$config['allowed_types'] = 'mp4|jpg|png|'
It will works in my case so I shared if possible try it.
You simply need to replace this condition:
if (! $this->is_allowed_filetype())
{
$this->set_error('upload_invalid_filetype');
return false;
}
With:
if (count($this->allowed_types) && !$this->is_allowed_filetype())
{
$this->set_error('upload_invalid_filetype');
return false;
}
$config['allowed_types'] = '*'; is possible solution, but IMHO it is not very safe. The better way is to add a file types you need to $config['allowed_types']. In case the CodeIgniter doesn't have the MIME types you need, you can add them by editing file mimes.php in "application/config" folder. Just include your mime types in array.
Example of adding epub and fb2 file types:
return array(
'epub' => 'application/epub+zip',
'fb2' => 'application/x-fictionbook+xml',
'hqx' => array('application/mac-binhex40', 'application/mac-binhex', 'application/x-binhex40', 'application/x-mac-binhex40'),
Then you'll be able to use the extensions you added in $config['allowed_types'] variable.
Related
I have created a new storage disk for the public uploads. I use it like this:
Storage::disk('uploads')->get(...);
But I am trying to figure out a way to get the path to the disk itself, I have spent some considerable time wondering between the framework files, but couldn't find what I am looking for. I want to use it in a scenario like so:
$icon = $request->file('icon')->store('icons', 'uploads');
Image::make(Storage::disk('uploads')->path($icon))->resize(...)->save();
So, how to get a storage disk's path ?
Update 2022
You can get the path from the config:
config('filesystems.disks.uploads.root')
Anyway, I keep the old answer, as it still works and is not wrong:
There is a function for this!
Storage::disk('uploads')->path('');
https://laravel.com/api/5.8/Illuminate/Filesystem/FilesystemAdapter.html#method_path
Looks like this has been there since 5.4! I never noticed...
After extra search I found that I can use the following:
$path = Storage::disk('uploads')->getAdapter()->getPathPrefix();
But still, isn't a "->path()" method is a given here or what!
We can also access the disk storage path from configuration helper directly:
config('filesystems.disks');
There are helper functions in the framework now that can get some of the commonly used paths in the app fairly easily.
https://laravel.com/docs/5.8/helpers
For example, if you wanted to access a file from the public folder you could do the below.
$image_file_path = public_path("images/my_image.jpg")
This will return you a local path for a file located in your app under the public/images folder.
Laravel Default Method
return Storage::disk('disk_name')->path('');
Get List of All Directories using disk name
Storage::disk('disk_name')->directories()
I am just wondering, I actually decided to go down a different route.
I create this file A
C:\somefolder\templates\mytemplate\joomlaoverwrites\libraries\joomla\document\html\renderer\head.php
which overwrites a joomla library file:
C:\somefolder\libraries\joomla\document\html\renderer\head.php
I use the overwrite by using
require_once(__DIR__ . '/joomlaoverwrites/libraries/joomla/document/html/renderer/head.php');
in my index.php of my template
This actually works.
What I now want to do is use my file just as a wrapper class, and sneak my changes into it before returning the answer. E.g.:
public function fetchHead($document)
{
$joomlasOriginalHead = callOriginalFunctionFetchHeadFromTheJoomlaImplementation();
//do my changes to joomlas original answer and return that
}
E.g. from that overwrite, I want to call the original file. Is that somehow possible?
Laravel 3 had a File::mime() method which made it easy to get a file's mime type from its extension:
$extension = File::extension($path);
$mime = File::mime($extension);
On upgrading to Laravel 4 I get an error:
Call to undefined method Illuminate\Filesystem\Filesystem::mime()
I also can't see any mention of mime types in the Filesystem API docs.
What's the recommended way to get a file's mime type in Laravel 4 (please note this is not a user-uploaded file)?
One solution I've found is to use the Symfony HttpFoundation File class (which is already included as a dependency in Laravel 4):
$file = new Symfony\Component\HttpFoundation\File\File($path);
$mime = $file->getMimeType();
And in fact the File class uses the Symfony MimeTypeGuesser class so this also works:
$guesser = Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser::getInstance();
echo $guesser->guess($path);
But unfortunately I'm getting unexpected results: I'm getting text/plain instead of text/css when passing a path to a css file.
IF you had just uploaded the file you can use:
Input::file('field_name')->getMimeType();
You can see more here! I hope it be for some help! :D
EDIT:
Input::file is some kind of extention from File, so you may use File::get('file')->getMimeType(); also. Didn't test, but MUST work.
After reading that:
PHP mime_content_type() is deprecated
Its replacement FileInfo is unreliable
Symfony's getMimeType() uses FileInfo (see my other answer)
I decided instead to port Laravel 3's implementation of File::mime() into a helper library within my Laravel 4 application. The Laravel 3 implementation just reads the MIME types from a config lookup array, based on file extension.
Solution:
Copied application/config/mimes.php from my L3 project to app/config/mimes.php in my L4 project
Made a FileHelper library with the File::mime() function code from the Laravel 3 File class.
It turned out that Symfony ExtensionGuesser and MimeTypeGuesser use unreliable FileInfo class. For that reason validation of mimes return unpredictable results and can not be used with files uploads in a proper way (it returns text/plain mime for js, xls, po etc.).
I've found very simple solution for this problem.
Instead of
'attachment' => 'required|mimes:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt|max:10000',
I split that into two different parts and now my validation looks like this:
private function createFileAttachmentValidator($file)
{
return Validator::make(
[
'attachment' => $file,
'extension' => \Str::lower($file->getClientOriginalExtension()),
],
[
'attachment' => 'required|max:10000',
'extension' => 'required|in:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt',
],
$this->validationMessages()
);
}
I simply try to verify that extension of the file is present and that it is listed in my in rule. That works, however, solution is not perfect.
public function mimeType($path)
{
return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
}
Ref: https://github.com/illuminate/filesystem/blob/master/Filesystem.php#L194
I want to know a clean way of defining Application Constants in Codeigniter. I don't want to change any native file of codeigniter. Hence I don't want to define it in application/config/constants.php as when I need to migrate to newer version of code-igniter I will not be able to copy the native files of codeigniter directly.
I created a file application/config/my_constants.php and defined my constants there. 'define('APP_VERSION', '1.0.0');'
I loaded it using $this->load->config('my_constants');
But I am getting a error
Your application/config/dv_constants.php file does not appear to contain a valid configuration array.
Please suggest me a clean way of defining application level constants in code-igniter.
Not using application/config/constants.php is nonsense! That is the only place you should be putting your constants. Don't change the files in system if you are worried about upgrading.
just a complete answer. (None of the answers show how to use the constants that were declared)
The process is simple:
Defining a constant. Open config/constants.php and add the following line:
define('SITE_CREATOR', 'John Doe')
use this constant in another file using:
$myVar = 'This site was created by '.SITE_CREATOR.' Check out my GitHub Profile'
Instead of using define(), your my_constants.php file should look something like this:
$config['app_version'] = '1.0.0';
Be careful with naming the array key though, you don't want to conflict with anything.
If you need to use define(), I would suggest doing it in the main index.php file, though you will still need to use APP_VERSION to get the value.
config file (system/application/config/config.php) to set configuration related variables.
Or use
constant file (system/application/config/constants.php) to store site preference constants.
=======================
DEFINE WHAT YOU WANT
=======================
$config['index_page'] = 'home';
$config['BASEPATH'] = 'PATH TO YOUR HOST';
Please refer this:
http://ellislab.com/forums/viewthread/56981/
Define variable in to constants & add value on array
$ORDER_STATUS = array('0'=>'In Progress','1'=>'On Hold','2'
=>'Awaiting Review','3'=>'Completed','4'
=>'Refund Requested','5'=>'Refunded');
You can accomplish your goal by adding constants to your own config file, such as my_config.php.
You would save this file in the application/config folder, like this:
application/config/my_config.php.
It is very common to have a separate config file for each application you write, so this would be easy to maintain and be understood by other CI programmers.
You can instruct CI to autoload this file or you can load it manually, as needed. See the CI manual on "Config class".
Let me suggest that you use composer.json to autoload your own Constants.php file, like this:
In Moodle 1.9.7, is it possible to somehow specify a white-list of the allowed extensions for the user uploaded files?
Looking at the source, there is no built in way to limit filetypes when modules use the upload_manager to process the uploaded files. There also is no use of any mimetype detection based on content of the files. The filelib libraries in moodle base their mimetype on file extension.
A neat way to do this for a module while is using the moodle upload manager object, would be to write a new class which extends the existing upload_manager class, and add some validatation based on file content.
Something like the following - you'll need to tidy this up a bit, and complete it with your own validation code.
class upload_manager_strict extends upload_manager {
var $allowed_types
function upload_manager_strict($inputname='', $deleteothers=false, $handlecollisions=false, $course=null, $recoverifmultiple=false, $modbytes=0, $silent=false, $allownull=false, $allownullmultiple=true, $allowed_types=null) {
$this->allowed_types = $allowed_types;
parent::upload_manager_strict($inputname, $deleteothers, $handlecollisions, $course, $recoverifmultiple, $modbytes, $silent, $allownull, $allownullmultiple)
}
function validate_file(&$file) {
$status = parent::validate_file(&$file);
if ($status) {
// do some mimetype checking and validation on the file $file['tmp_name']
// could use $this->allowedtypes
}
return $status;
}
}