How can I get path to file in composer package - composer-php

I want to include a default file in a composer package. I would like to copy a file that is located in the package over to project via Robo. I was thinking I can use the Composer object but had no luck because $composer->getConfig() is null.
<?php
namespace MyLibrary\MyClass;
use Robo\Tasks as Tasks;
use Composer\Composer as Composer;
/**
* Base class for Xeno robo commands.
*/
class MyClass extends Tasks {
public function pathLibrary() {
$composer = new Composer();
echo $composer->getConfig()->get('mylibrary');
}
public function setup() {
// Copy file over.
$this->_exec('cp ' . $this->pathLibrary() . '/src/Starter/myfile.yml ./');
}
}
Anyone know of a way?

If you want to copy a file from a directory, but do not know the absolute path of the file (eg. because this is installed as a library to some vendor file), you can have a look at the magic variable __DIR__. It contains the path to the current script, so if your script is placed at /what/ever/directory/script.php, this resolves to /what/ever/directory.
So, if you know the relative path between your script and the file to be copied, you can make use of it: in your case, the file to be copied might reside at the relative path src/Starter/myfile.yml and the file to run the operation at src/MyLibrary/MyClass.php (according to the namespace of the class). To copy it somewhere, you can use the path $path = __DIR__ . '/../../Starter/myfile.yml.

Related

How to include js file from 'resources' folder (Laravel 5.5)

I used the following code to include my js file:
Meta::addJs('admin_js', '/resources/assets/admin_js/admin_app.js');
The file exists but in the console I see status 404.
If I move the file to 'public' folder - all ok. But I want that this file be stored in 'resources' directory
you have to move js files to public or store into storage and make symlinks.
or you need to create symlinks the resources directory to a public directory(which is not recommended).
you need to use the most recommended and effective method of Laravel of using Laravel mix. Please use the link below to read about laravel mix a solution.
https://laravel.com/docs/5.6/mix
It will allow you to place your js assets into resources directory and make compressed js file in public which will be used by the setup.
Update for LARAVEL 7.0 :
Adding a vanilla JavaScript file to ‘resources’ :
Create the file in resources/js (called 'file.js' in this example)
Go to webpack.mix.js file (at the bottom)
Add there: mix.js('resources/js/file.js', 'public/js');
Run in terminal : npm run production
i don't know if i am late but when i needed to load js file from directories and sub directories in my view file, i did this and it worked perfectly for me.
BTW i use laravel 5.7.
first of all i wrote a function that searched for any file in any given directory with this
.
/**
* Search for file and return full path.
*
* #param string $pattern
* #return array
*/
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
the above will return a full path for each ".js" file in my "resources/view" directory.
Then i made a call to the above function to copy the content of each js to a single js file (new.js) which i created in my public/js using the below function.
/**
* Load js file from each view subdirectory into public js.
*
* #return void
*/
function load_js_function(){
//call to the previous function that returns all js files in view directory
$files = rglob(resource_path('views').'/*/*.js');
foreach($files as $file) {
copy($file, base_path('public/js/new.js'));
}
}
After this i made call to the load_js_function() in my master blade layout(where i load all my css,js etc) immediately after loading the public/js/new.js, you can see below.
<script src="{!! asset('js/new.js') !!}"></script>
<!-- load all js from each module -->
{{ load_js_function() }}
These solution updates the file in public as you update the content of the original file.
Vote up if it works for you and comment if you have an issue implementing this, i will be glad to shed more light.
cheers

Is it ok to change public storage folder name from 'public/storage' to 'public/public'?

Laravel recommends here to create symlink from public/storage to storage/app/public. I'm looking for some inputs on whether renaming public/storage to public/public would cause any issues in other configurations or packages.
I am doing this because all my filenames in db are relative to storage/app folder. I store it that way since I can then uniformly retrieve any file using storage_path() helper. For example:
Protected file: storage/app/internal/secret.doc => filename in db = internal/secret.doc
Public file: storage/app/public/common.doc => filename in db = public/common.doc
Then to retrieve them:
// get filename 'internal/secret.doc' or 'public/common.doc'
$filename = DB::table('files')->find(1)->value('name'); // find file where id=1 and get only the name field
$file = storage_path('app/'.$filename);
This way I don't have to wonder whether I need to add public/ folder while retrieving a specific file. Also when I use asset() helper to generate urls to public files, again I can simply say:
$url = asset($filename)`;
Whereas, if I continue to use public/storage as the symlinked folder then I would have to do:
$temp_filename = str_replace('public/', '', $filename);
$url = asset('storage/'.$temp_filename);
Let me know if you have a different take or spot some issues with the renaming.
Once public/storage is sym linked to storage/app/public, you don't want your filepath in asset() to reference public. Public folder is already the root of your webserver, and asset() assumes public directory base. So your asset would be in asset("storage/$filename"). That actually pulls from storage/app/public/$filename.

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

How to get public directory?

I am a beginner here so pardon me for this question am using return File::put($path , $data); to create a file in public folder on Laravel. I used this piece of code from controller I need to know the value of $path how should it be.
Use public_path()
For reference:
// Path to the project's root folder
echo base_path();
// Path to the 'app' folder
echo app_path();
// Path to the 'public' folder
echo public_path();
// Path to the 'storage' folder
echo storage_path();
// Path to the 'storage/app' folder
echo storage_path('app');
You can use base_path() to get the base of your application - and then just add your public folder to that:
$path = base_path().'/public';
return File::put($path , $data)
Note: Be very careful about allowing people to upload files into your root of public_html. If they upload their own index.php file, they will take over your site.
I know this is a little late, but if someone else comes across this looking, you can now use public_path(); in Laravel 4, it has been added to the helper.php file in the support folder see here.
The best way to retrieve your public folder path from your Laravel config is the function:
$myPublicFolder = public_path();
$savePath = $mypublicPath."enter_path_to_save";
$path = $savePath."filename.ext";
return File::put($path , $data);
There is no need to have all the variables, but this is just for a demonstrative purpose.
Hope this helps,
GRnGC
I think what you are looking for is asset(''); You can use asset('storage'), after creating your symbolic link.
asset('public')
OR
url('public')

How do I get the full local path of a file uploaded with $_FILES

I am uploaded a file through the file input. If I print_r($_FILES), I can get several pieces of info, but it doesn't give me the full LOCAL path of the file (the path on my computer, not the server). How do I get this?
I need this to use the FTP library in CodeIgniter. Documentation is here on how to use it to upload a file.
As you can see, it requires the full local path, although I'm not sure why.
As file upload path is a variable which changes from server to server, I would suggest you to use move_uploaded_file() function:
$target = "uploads/myfile.something";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
// do something
}
that way it will always work no matter what the path is even if it changes. The target can be anything you wish, it's relative to the current folder where script is being executed.
I solved this.
public function upload($file, $folder) {
$this->CI->ftp->upload($_FILES['file']['tmp_name'], '/public_html/rest/of/path/' . $_FILES['file']['name'], 'ascii', 0775);
}
The file is being uploaded, so you can access it with $FILES['file']['tmp_name']. This is from within a class, so that's why I'm using $this->CI. Otherwise, I would just use $this.

Resources