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
Related
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.
Am trying to read a .docx file which is inside my project folder via PHP Word library. I have included autoloader like this :
include_once 'vendor/autoload.php'; in my controller.
CODE FOR FUNCTION IN CONTROLLER:
function test_phpword()
{
$a=base_url();
$path=$a."".'123.docx';
$source =$path;
echo date('H:i:s'), " Reading contents from `{$source}`";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
echo write($phpWord, basename(__FILE__, '.php'), $writers);
}
BUT GETTING ERROR LIKE BELOW:
06:18:42 Reading contents from http://localhost/myproject/123.docx
Fatal error: Uncaught Exception: Cannot find archive file. in /opt/lampp/htdocs/myproject/vendor/phpoffice/common/src/Common/XMLReader.php:51
Try this
Change
$path=$a."".'123.docx';
to
$path='123.docx';
And put 123.docx beside your php script file. make sure the two files are in the same place. Run your php script again.
If this helps and works fine you can check the file path and make proper change to your php program.
While loading the path, you have to give relative path to the doc file as shown below,
$random_name = 123.docx;
$contents = \PhpOffice\PhpWord\IOFactory::load('../uploads/'.$random_name);
I don't know whats your $base_url, but it will not work if it is absolute path like http://path/to/doc/file.
I have worked on it and tested. Hope it helps.
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.
i need to backup a site's FTP. The site is hosted on a linux server. The problem is that there is a folder with more than 5k files. Linux can't show me more than 4998 files, so i cant copy these files 'cause the server don't give me more than 4998.
I can't delete these files to see the others 'cause the site is actually online. I can't move these file in another directory for the same reason.
What can i do? i'm trying using a shell...but i don't know...i'm not sure using this method.
I got solution for my own answer
<?php
$rootPath = realpath('wp-content/uploads/2014/07');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('dio.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
You can do this through the command line, this guide shows you how. It seems mget (the ftp command) isn't recommended for recursive calls (subfolders and their content), so wget can also be used, se this.
I also like to zip such folders with many files into one, for easy oversight when up-and-downloading. Use
zip -r myfiles.zip myfiles
Here's a guide for that too.
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.