Codeigniter how to load all js files from folder - javascript

Codeigniter how to load all js files from folder, without mentioning name of file.
Example- I want to get all js file from one folder. I know the path up to folder but js files are created dynamically when build is created so do not know exact name of files to load tradional way.

Maybe this solution will going to work
$js_path = rtrim(FCPATH . 'assets/js', '/'); // FCPATH = root folder of your codeigniter project
$js_files = glob("{" . $js_path . "/*.js}", GLOB_BRACE);
for($i = 0; $i < count($js_files); $i++){
echo read_file($js_files[$i]);
}
code not tested but I hope this will solve the problem

I tried following that worked for me
$folderpath = $_SERVER['DOCUMENT_ROOT'].'/resources/common/js/';
$fileName = array();
$fileName = get_filenames($folderpath);
$jsFiles = array();
foreach($fileName as $file){
$url = 'resources/common/js/'.$file;
$jsFiles[]['js'] = base_url($url);
}
$this->load->vars($jsFiles);

Related

Save image (from javascript) in a public folder (through php)

I am trying to use this package
https://vuejsexamples.com/a-beautiful-vue-component-for-image-cropping-and-uploading/
But then I am wondering how to save my image in a folder.
I am trying to do this in php so that I can use easily the route afterwards, but it could be OK with JS as well if it is easier.
public function uploadfile(Request $request){
$img = $request->img;
$newlocation = $request->newlocation;
$filename = $request->filename;
$img = str_replace('data:image/png;base64,', '', $request->img); //I tried with and without this
return file_put_contents ($newlocation . "/" . $filename , $img );
}
I actually get an image in my public folder but the content is absolutely not what I want :)
The image.jpg contains the text C:\Users\usr\AppData\Local\Temp\phpEE72.tmp
I found this link very helpful when uploading images:
https://hdtuto.com/article/laravel-57-image-upload-with-validation-example
In my controller I have something like this after validation, which allows for different image filetypes like png and jpg:
$name = $request->input('name');
$getimageName = $name.'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('filelocation'), $getimageName);
And in my filesystems.php file:
'disks' = > [
'filelocation' => [
'driver' => 'local',
'root' => public_path().'/foldername',
'url' => env('APP_URL').'/public/foldername',
'visibility' => 'public',
]
]
That's the string of the filename. Behind the scenes PHP handles all files uploaded to the server by putting in a temp directory. It's up to you to move it and process it. All you'd need to do is to read the file's contents first:
file_put_contents ($newlocation . "/" . $filename , file_get_contents($img) )
without your str_replace.
You should consider doing this in a more Laravel way as recommended in https://laravel.com/docs/5.8/filesystem#file-uploads but to answer the immediate question, all you need if file_get_contents.

Drupal's .info file doesn't load multiple js files

Although I included multiple .js files in Drupal 7's .info file as advised, only the first ones do load - up to
scripts[] = js/jquery.cycle2.min.js
but not further. Changing the order doesn't help, it seems to stop loading every time at the first cycle-js file.
What am I missing there? Or is it only possible to include max 3 .js for some reason/setting?
core = 7.x
engine = phptemplate
stylesheets[all][] = style.css
stylesheets[all][] = media.css
scripts[] = js/custom.js
scripts[] = js/jquery.cycle2.min.js
scripts[] = js/jquery.cycle2.caption2.min.js
scripts[] = js/jquery.js
You can use following function to add JS on your site:
function mytheme_preprocess_page(&$vars, $hook) {
if (true) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mytheme1.js');
$vars['scripts'] = drupal_get_js(); // necessary in D7?
}
}
Try another way to include js file .
/your_theme/template.php
function mytheme_preprocess_page(&$vars, $hook) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/custom.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/jquery.cycle2.min.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/jquery.cycle2.caption2.min.js');
drupal_add_js(drupal_get_path('theme', 'mytheme') . 'js/jquery.js');
$vars['scripts'] = drupal_get_js();
}
Thank You.

PHP user profile page

Im trying to make a website with users on it, and I'm trying to create a script so that whenever a new user registers it will automatically create a user folder and profile for them but when I try to register it doesn't create the files, could someone please help me with this, Thanks
<?php
include "inc/header.php";
$newfolder = $username;
if (!mkdir($newfolder, 0777, true)) {
die('Failed to create folders...');
}
$pagename = $username;
$newFileName = './u/'.$username.'/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
?>
To make a directory/file
if (!file_exists("parent_folder/$username")) {
//Create a file with read write execute PERMISSIONS ENABLED
//Please check : your parent folder also must have 0777 permissions to avoid any kind of read write error
mkdir("parent_folder/$username", 0777);
//now u have to create a FILE with .php
//now this file_put_contents is VERY importnant !
$pagename = $username ;
$newFileName = './parent_folder/$username/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
if (file_put_contents($newFileName, $newFileContent) !== false) {
//notify file is created
echo "File created (" . basename($newFileName) . ")";
} else {
//notify u have error
echo "Cannot create file (" . basename($newFileName) . ")";
}
//now create ur .php file in user folder
}
else {
echo "Your parent folder does not exist"
}
Now the possible error and some tips
1) Most of people do fopen("filename_with_PATH", "w")
and expect that file will be generated in PATH folder !
Some times it might fall wrong (depends on version)
fopen is meant to create a file in the directory where your php resides
2) check ur php permission in php.ini if u dont give php permission to write,remote access then u might get some errors (it will be displayed that u have error in my script)
3)For more info and tinkering file_put_contents
Hope this will be helpful for you ..

Recursively generate script tags for all JavaScript files within a directory with PHP

I'm building a complex app with lots of JavaScript files in lots of sub-directories. I know I want to include them all (it won't affect performance), but I don't want to manually create a script tag for each. Given that all of my files are children of a "/js" directory, how could I dynamically generate the script tags for each with PHP? Something like this:
// first somehow recursively get all .js files, then:
foreach($files as $file) {
echo '<script src="' . $file->path . '"></script>';
}
Most elegant way is to use SPL in my opinion.
$dirIterator = new RecursiveDirectoryIterator("/path/to/js");
$iterator = new RecursiveIteratorIterator(
$dirIterator,
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if($file->getExtension() == 'js') {
// You probably have to adjust the full path according to your DOC_ROOT
$url = $file->getPathname();
echo '<script src="' . $url . '"></script>';
}
}
Have a look at http://php.net/manual/en/class.splfileinfo.php to see what else you can do with $file .

Search for .mp3 files in main folder and subfolders

im looking to make a web player from scratch so I can control my music from my phone. I've got a listing of all files within a folder done, but I need to get it to list the files within the subfolders of the main folder.
So sort of something like \Path\ - Shows all in \Path\ and \Path\subs\ shows the contents of all subfolders.
This is the code that I have written to allow me to search a folder for .mp3 files, but I cannot pin the way to search in subfolders as well.
$.ajax({
url: "music/",
success: function(data){
$(data).find("a:contains(.mp3)").each(function(){
var files = $(this).text().replace(/ /g, '');
files = $(this).text().replace(/.mp3/,'');
$('<p></p>').html(files).appendTo('#displayer');
$('#displayer > p:nth-child(odd)').css('background-color','#DDD');
});
}
});
Many thanks in advance
This is my solution in PHP
<?php
// your mp3 dir
$dir = "dir/";
$result = array();
if(is_dir($dir)){
$iterator = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
if("mp3"==$ext){
$result[] = $file->getPathName();
}
}
// you can echo json_encode(yourMp3Path) here.
var_dump($result);
}

Categories

Resources