How to recursively link javascript files to HTML - javascript

I have the following file structure:
root - dir1 - js_dir - file.js
- dir2 - inner_dir - header.php
The directory js_dir contains my javascript files. I'm trying to recursively add them to my header in header.php as follows:
header.php
<head>
<?php
$dir = '../../dir1';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );
foreach ( $iterator as $path ) {
if ($path->isDir()) {
print('<div> DIR : ' . $path . PHP_EOL . "</div>");
} else {
$path_parts = pathinfo($path);
$file_parts = $path_parts['extension'];
if ($file_parts == "js")
{
$array = explode("/opt/lampp/htdocs", $path);
unset($array[0]);
echo $path = implode("/", $array);
print("<div>http://localhost$path" . PHP_EOL . "</div>");
echo("<script src='http://localhost/$path'></script>");
}
}
}
?>
</head>
I, however, keep getting the error:
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(../js): failed to open dir: No such file or directory in /opt/lampp/htdocs/root
How can I go about getting it to work?
Thank you all in advance.

Related

PHP - Create a directory listing of specified folder

I've got small piece of code which I'm using to scan a certain directory on my server for all the files and folders inside. But there's a catch... It can only scan the specified folder, and no sub-directories. Let's say share/ is the root directory for this scan, if there are files in any sub-directory like share/folder/file.png they won't be listed, where the entire purpose of my project is to create an HTML file explorer.
This is the code I'm using to receive the list of files from the server to later display it in the browser using JavaScript:
<?php
$listFiles = array();
if ($handle = opendir('./share')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$listFiles[] = $entry;
}
}
closedir($handle);
}
$FS_files = json_encode($listFiles);
?>
And then I "push" it rude to my actual JavaScript code like this:
let FS_files = <?php echo $FS_files ?>
The code I above basically generates an array of file names from scanned directory. What I wanna know is how can I get an entire JSON tree of files in this directory and all folders inside it, or possibly change my code so it works that way?
<?php
function scanFolder($dir) {
$result = array();
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value,array(".",".."))) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
$result[$value] = scanFolder($dir . DIRECTORY_SEPARATOR . $value);
} else {
$result[] = $value;
}
}
}
return $result;
}
$FS_files = json_encode(scanFolder("./share"));
?>
The function scans the folder and checks each entry with is_dir. If its true, this folder is scanned with the same function.

How to link Javascript files to HTML recursively

I'm trying to recursively link some Javascript files in a directory into a HTML page:
<?php
$dir = '/opt/lampp/htdocs/my_project/js';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );
foreach ( $iterator as $path ) {
if ($path->isDir()) {
print('dir >>> ' . $path->__toString() . PHP_EOL . '\n');
} else {
print($path->__toString() . PHP_EOL . '\n\n\n');
echo("<script src='$path->__toString() . PHP_EOL>\</script>");
}
}
?>
This, however, doesn't seem to work. How can I go about adding all the files in the directory and sub-directories recursively? Any working recursive approach will do. It doesn't have to be the way I'm trying it above.
Thank you all in advance.
Try changing your foreach block to below code and see if that helps?
foreach ( $iterator as $path ) {
if ($path->isDir()) {
print('dir >>> ' . $path . PHP_EOL);
} else {
print($path . PHP_EOL);
echo("<script src='$path'></script>".PHP_EOL);
}
}
The issue was with the syntax of "<script src='$path->__toString() . PHP_EOL>\</script>" changing this line to "<script src='$path'></script>" will help.

$ is not defined during a Magento installation through cPanel

I am trying to install Magento community edition 1.9.2.1 into cPanel through Godaddy. I have so far extracted the tar file into the file manager, moved all the items in the Magento folder into root, and given folders proper permissions to run.
When I go into my website to open up the installation wizard I see this
I cannot click the continue button it doesn't work. When I inspect the page I get these errors.
I think its a jQuery problem. Looks like the website doesn't load any JavaScript. I tried adding a jQuery CDN link to the head but no avail. I have saved a jQuery CDN into my file system and called it through head still nothing.
I don't know what's the problem. JavaScript is enabled in my browser, so it should work.
i think this should be permission issue.i have attached a code,copy that in a new file(eg. magento-cleanup.php ) and upload to your magento root and run it using url(http://youdomain/magento-cleanup.php). it helps you to fix permission issue.
<?php
## Function to set file permissions to 0644 and folder permissions to 0755
function AllDirChmod( $dir = "./", $dirModes = 0755, $fileModes = 0644 ){
$d = new RecursiveDirectoryIterator( $dir );
foreach( new RecursiveIteratorIterator( $d, 1 ) as $path ){
if( $path->isDir() ) chmod( $path, $dirModes );
else if( is_file( $path ) ) chmod( $path, $fileModes );
}
}
## Function to clean out the contents of specified directory
function cleandir($dir) {
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..' && is_file($dir.'/'.$file)) {
if (unlink($dir.'/'.$file)) { }
else { echo $dir . '/' . $file . ' (file) NOT deleted!<br />'; }
}
else if ($file != '.' && $file != '..' && is_dir($dir.'/'.$file)) {
cleandir($dir.'/'.$file);
if (rmdir($dir.'/'.$file)) { }
else { echo $dir . '/' . $file . ' (directory) NOT deleted!<br />'; }
}
}
closedir($handle);
}
}
function isDirEmpty($dir){
return (($files = #scandir($dir)) && count($files) <= 2);
}
echo "----------------------- CLEANUP START -------------------------<br/>";
$start = (float) array_sum(explode(' ',microtime()));
echo "<br/>*************** SETTING PERMISSIONS ***************<br/>";
echo "Setting all folder permissions to 755<br/>";
echo "Setting all file permissions to 644<br/>";
AllDirChmod( "." );
echo "Setting pear permissions to 550<br/>";
chmod("pear", 550);
echo "<br/>****************** CLEARING CACHE ******************<br/>";
if (file_exists("var/cache")) {
echo "Clearing var/cache<br/>";
cleandir("var/cache");
}
if (file_exists("var/session")) {
echo "Clearing var/session<br/>";
cleandir("var/session");
}
if (file_exists("var/minifycache")) {
echo "Clearing var/minifycache<br/>";
cleandir("var/minifycache");
}
if (file_exists("downloader/pearlib/cache")) {
echo "Clearing downloader/pearlib/cache<br/>";
cleandir("downloader/pearlib/cache");
}
if (file_exists("downloader/pearlib/download")) {
echo "Clearing downloader/pearlib/download<br/>";
cleandir("downloader/pearlib/download");
}
if (file_exists("downloader/pearlib/pear.ini")) {
echo "Removing downloader/pearlib/pear.ini<br/>";
unlink ("downloader/pearlib/pear.ini");
}
echo "<br/>************** CHECKING FOR EXTENSIONS ***********<br/>";
If (!isDirEmpty("app/code/local/")) {
echo "-= WARNING =- Overrides or extensions exist in the app/code/local folder<br/>";
}
If (!isDirEmpty("app/code/community/")) {
echo "-= WARNING =- Overrides or extensions exist in the app/code/community folder<br/>";
}
$end = (float) array_sum(explode(' ',microtime()));
echo "<br/>------------------- CLEANUP COMPLETED in:". sprintf("%.4f", ($end-$start))." seconds ------------------<br/>";
?>

pass new file name back to jquery

I am using some jquery to help upload a file to a php script. Everything is working fine and the file does in fact get uploaded. But during the upload, I have made it so the file gets resized to our needs, with a new unique file name. I'd like to pass that new unique file name back to the jquery and have it display on the page. Right now, it just displays the original image (which is not resized)
Here's the jquery code:
$(function(){
var btnUpload=$('#upload');
var status=$('#status');
new AjaxUpload(btnUpload, {
action: 'upload-file.php',
name: 'uploadfile',
onSubmit: function(file, ext){
if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
// extension is not allowed
status.text('Only JPG, PNG or GIF files are allowed');
return false;
}
status.text('Uploading...');
},
onComplete: function(file, response){
//On completion clear the status
status.text('');
//Add uploaded file to list
if(response==="success"){
$('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
} else{
$('<li></li>').appendTo('#files').text(file).addClass('error');
}
}
});
});
And then my upload php file looks like this:
$uploaddir = 'uploads';
$file = $uploaddir . basename($_FILES['uploadfile']['name']);
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
$path = realpath(dirname(__FILE__));
include $path . '/uploads/phmagick.php';
$temp_file = explode(".", $_FILES['uploadfile']['name']);
$time = time();
$new_file = $time . '.' . $temp_file[1];
$p = new phmagick($path . '/uploads/' . $_FILES['uploadfile']['name'], $path . '/uploads/' . $new_file);
$p->convert();
$phMagick = new phMagick($path . '/uploads/' . $new_file, $path . '/uploads/' . $new_file);
$phMagick->debug=true;
$phMagick->resize(414,414,true);
echo "success";
} else {
echo "error";
}
Any thoughts on how I can get the new unique file name back, which would be something like: 1397413326.jpg?
Thank you
Echo the filename back instead of the word "success".

dropzone.js: How can I rename file?

I want to rename a file uploaded with dropzone.js. I have read the other question of rename file with dropzone, but I don't understand.
I've tried this:
this.on("sending", function(file, xhr, formData) {
var abdocument.getElementById("a").value
var nick = document.getElementById("b").value;
formData.append("fileName", a+ " - " + b);
});
}
};
but then how can I use this "fileName"? I've to use in php function? This is mine:
<?php
$upload_folder = 'uploads';
if (!empty($_FILES)) {
$temp_file = $_FILES['file']['tmp_name'];
$target_path = dirname( __FILE__ ) . '/' . $upload_folder . '/';
$target_file = $target_path . $_FILES['file']['name'];
if( file_exists( $target_path ) ) {
move_uploaded_file($temp_file, $target_file);
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
}
?>
or can I rename via javascript?
Thanks to all
Ok, I have to do this:
$_FILES['file']['name'] = ($_POST["fileName"]);
simply set it at $_FILE superglobal as follow:
$_FILES['file']['name']="your_new_file_name";

Categories