Show list of directories in path php & htm [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I would like to show a list of all subdirectories in the directory on the UI Interface using php .The directories will show up as a button and the user can select which directory.
Here is my code so far
$path = /path;
$dir = glob($path.'/*/,GLOB_ONLYDIR);
Please how do I go about this? Any hint will be appreciated.

Use foreach to loop through the results of glob function. Example:
$path = '/';
$dirs = glob($path.'*', GLOB_ONLYDIR);
foreach($dirs as $dir) {
echo ''.$dir.'<br>';
}

You can start by looping the array and printing each directory:
foreach ($dir as $item) {
echo $item;
}
Depending on how you want to add that to your UI, you can dress that up with a button code:
echo "<button>$item</button>";
Then you could add some javascript to add an action to whatever should happen when the user clicks that button.

Related

I want to download multiple files from my storage folder in Laravel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hy developers, i want to download multiple files as a zip, but i keep getting errors
below is the code i have tried
public function multiple($id){
$files = documents::where('claim_id', $id)->get();
if ($files != '') {
$filepath = storage_path('app/Documents/');
foreach($files as $file) {
$zipper = new \Chumper\Zipper\Zipper;
$fi = storage_path('app/Documents/'.$file->file_name);
$zipper->make($filepath.'doc.zip');
}
return response()->download($filepath.'doc.zip');
}
else{
Alert::info('Info', 'No Documents Available');
return redirect()->back();
}
}
attached is the error i get
Looks like you didn't add any files to your Zip.
I think:
$zipper->make($filepath.'doc.zip');
Should be:
$zipper->make($filepath.'doc.zip')->add($fi)->close();
Make sure the files and zip are in the storage folder.

Copying files via system call fails on PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to copy some files from one directory on server to another, but it does not work.
Here is my code:
system('cp /var/www/site1/images/' . $row['imageUrl']. ' /var/www/site2/content/upload/content/item/mid/' . $row['imageUrl']);
$file = '/var/www/site1/images/'.$row['imageUpl'];
$newfile = '/var/www/site2/content/upload/content/item/mid/'.$row['imageUpl'];
if (copy($file, $newfile)) {
echo "success";
}
else
{
echo "failed";
}
Look at the copy() function here: http://php.net/manual/ru/function.copy.php

Checking if data equal one of my file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a folder in my website, is it possible to check if a file exist for comparing with a data query ?
I mean if $donnees["DATA"] == one of the files of the folder.
I don't know any function or something about that.
For instance if Data = 25478
In my folder :
Differents name of file :
24788
24777
25478
$listOfFiles = glob("*");
if(in_array($donnees["DATA"], $listOfFiles) {
//do stuff
}}
You can use glob("*.txt") for .txt files etc etc.
Yes, you can use glob function:
//path to directory to scan
$directory = "somefolder/";
//get all folders/files in specified directory
$files = glob($directory . "*");
//get ach directory/file name
foreach($files as $file)
{
if($donnees["DATA"]==$file){
//do something
}
}

How to assign a value to a variable from a text file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using a script to back up files from ftp. code is below.
include "recurseZip.php";
//Source file or directory to be compressed.
$src='source/images/black.png';
//Destination folder where we create Zip file.
$dst='backup';
$z=new recurseZip();
echo $z->compress($src,$dst);
Now I want to get values to $src from source/files.txt which contains a list of file names.
My .txt file:
index.php.bk-2013-12-02
index.php.bk-2013-12-07
index.php.bk-2013-12-10
index.php.bk-2013-12-20
index.php.bk-2013-12-26
function.php.bk-2013-12-20
function.php.bk-2013-12-23
contact.php.bk-2013-12-23
contact.php.bk-2013-12-30
my source/files.txt contains 10 file names those need to be assigned as values to the variable $src I am using this script http://ramui.com/articles/php-zip-files-and-directory.html
how can I do that.?
any help will be very much appreciated.
Thanks.
Okay, you want to get the file name from each line of the .txt file.
<?php
$myFile = "files.txt";
$lines = file($myFile);
foreach($lines as $line){
$file = basename($line);
echo $file;
}
?>
Answer to your old question variant
You can use the basename() function. The manual says, "given a string containing the path to a file or directory, this function will return the trailing name component".
Now, you said "I want to get file name to $src from source/files.txt", so assuming from this, you are looking to get the file name i.e. black.png. This could be achieved using the basename() function as mentioned before.
<?php
$src='source/images/black.png';
$file = basename($src);
echo $file;
?>
Output
black.png
http://ideone.com/p2b4sr

find and fopen a file with random/unknown name with php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've searched around google for a way to fopen and read a file with an unknown/random name in a specific folder with a specific extension (it will be the only file in that folder, but it gets updated every hour with a new name), but couldn't find anything. Previously the file had the same name every time unfortunately now it changes...
previously i had to only define it this way:
$file = "/var/uploads/quarter.csv";
$handle = #fopen($file, "r");
but with a random name it doesn't work anymore.
so, I've tried:
function the_file($dir = '/var/uploads/') {
$files = glob($dir . '/*.csv');
$file = $files;
}
$handle = #fopen($file, "r");
script continues...
but it doesn't work. Any help would be appreciated, thanks.
The $file variable doesn't exist outside the function scope and hence you won't be able to use it outside the function. Also, glob() returns an array -- if there is only one file, you can just get the first element of the array and return it, like so:
function the_file($dir = '/var/uploads/') {
$files = glob($dir . '/*.csv');
return $files[0]; // return the first filename
}
Now to store it in a variable, you can do:
$file = the_file(); // or specify a directory
# code ...
A possible solution can be:
If there is only one file in the directory, you can do a scandir to get the contents of the folder. (see PHP ref.)
$dir = 'your_folder_name';
$files1 = scandir($dir);
The content of the $files will be:
Array
(
[0] => .
[1] => ..
[2] => yourfile.csv
)
So you can get the full name of the file with:
$filename = $files1[2];

Categories