PHP ZipArchive list only one level of files/folders - php

I have wp.zip and would like to list only one level of files/folders. My current code:
$zip = new \ZipArchive();
$zip->open('wp.zip'), \ZipArchive::RDONLY);
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
echo $stat['name'] . ' ';
}
This code spits out entire list of files recursively.
I need only first level, like this:
wp-admin/
wp-content/
index.php
config.php
<...>
What's the best approach to achieve my goal?

Yes, you can only parse the name and get the level of the file accordingly.
<?php
$zip = new \ZipArchive();
$zip->open('wp.zip');
function getEntryList($zip, $level = 1){
$res = [];
for($i = 0; $i < $zip->numFiles; ++$i){
$name = explode("/", trim($zip->statIndex($i)['name'],"/"));
if(count($name) == $level + 1){
$res[] = end($name);
}
}
return $res;
}
print_r(getEntryList($zip));

Related

php How to include the first line in a file txt

There is a problem when the search does not include the first line. What is the method to include the first line?
$name = trim($_GET['name']);
$file = file('data.txt');
for($i = 0; $i < count($file); $i++){
$read = explode('|', $file[$i]);
if($name == $read[0]){
$found = 1;
$t1 = $read[0];
$t2 = $read[1];
Using the following data as sample (data30nov.txt) - I have added a few lines to demonstrate that it can search line 1 and other lines
a|125|hh#hh.com
g|653|uu#uu.com
vb|603|uu#uu.com
a|1244|hh#hh4444.com
d|124|hh#hh2.com
Assuming that you are searching the string "a"
Your code should be able to search the 1st line
See the code below
<?php
//$name = trim($_GET['name']);
$name = "a";
$file = file('data30nov.txt');
for($i = 0; $i < count($file); $i++){
$read = explode('|', $file[$i]);
if ($name == $read[0]){
$found = 1;
echo $file[$i]. "<br>";
}
}
?>
The output can be found here:
http://www.createchhk.com/SO/testSO30Nov2021.php
(It shows clearly that the program can search the 1st line)

Why this PHP code does not run - Code fitter Mail

I have code
require_once('verify.php');
$file = 'mail.txt';
$allfile = file($file);
for ($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i];
$dep = 'admin#ngocuong.net';
$kq = verifyEmail($cuong, $dep);
if($kq == '2') {
echo 'OK<br />';
} else {
echo 'Nope<br />';
}
}
echo 'Done!!!';
with $cuong = 'cuong.fl310#gmail.com'; code run. Return is OK.
But i put all my list email into file mail.txt. And run code read line in file txt. $cuong = $allfile[$i] code not run.
I had show
for($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i]
echo $cuong;
}
it print list email in file txt. With $i= 0; $cuong = 'cuong.fl310#gmail.com' But code not run. Return enqual: Nope !!!
Plz help me fix it.
Thanks for readding!!!

php- code make it small

I am trying to make this code small using for or while loop . It is working with the following code,I just want this to be small. 'ufile' is the input name.
if (!$_FILES['ufile']['name'][0])
{
echo "Upload 1st file";
}
else
{
// can this be in a for loop???
$path1= "../uploads/".$_FILES['ufile']['name'][0];
$path2= "../uploads/".$_FILES['ufile']['name'][1];
$path3= "../uploads/".$_FILES['ufile']['name'][2];
$path4= "../uploads/".$_FILES['ufile']['name'][3];
$path5= "../uploads/".$_FILES['ufile']['name'][4];
$path6= "../uploads/".$_FILES['ufile']['name'][5];
$path7= "../uploads/".$_FILES['ufile']['name'][6];
$path8= "../uploads/".$_FILES['ufile']['name'][7];
}
$path = array();
for($i=0;$i<=7;++$i)
$path[$i]="../uploads/".$_FILES['ufile']['name'][$i];
I would advise against your current coding style. Life would be simpler if you just stored the paths in an array, e.g.
$paths[1] = "../uploads/" . $_FILES['ufile']['name'][0];
$paths[2] = "../uploads/" . $_FILES['ufile']['name'][1];
Then you could do something like this:
$paths = array();
for ($i = 0; $i <= 7; $i++) {
$paths[$i + 1] = $_FILES['ufile']['name'][$i];
}
But to answer your question, you can do something like this instead, which is very similar:
$paths = array();
for ($i = 0; $i <= 7; $i++) {
$paths['path' . ($i + 1)] = $_FILES['ufile']['name'][$i];
}
extract($paths);
See the extract doc page for more info about what's going on here
You can use variable variables as well :
foreach(range(0,7) as $index){
$varname = "path".$index;
$$varname = "../uploads/".$_FILES['ufile']['name'][$index];
}
Not sure what you want to do with those paths afterwards but here is my go at it. I would use the length of the array, assuming it doesn't always contain the same amount of file names.
$paths = array();
for($i = 0; $i < count($_FILES['ufile']['name']); $i++)
{
$paths[] = "../uploads/".$_FILES['ufile']['name'][$i];
}

breadcrumbs for multi-level url

I want to create multi-level breadcrumbs, like if user go to Home->Step1->step2->step3 page then goes to Step4 : i want to see my breadcrumbs Home:->step1->step2-step3->->step4.
Here is my code :
<?php
$path = parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH);
$parts = explode('/',$path);
$stepname='Step'.$step->step_code;
if (count($parts) < 2)
{
echo("home");
}
else
{
for ($i = 2; $i < count($parts); $i++)
{
if (!strstr($parts[$i],"."))
{
echo("<a href=\"");
for ($j = 0; $j <= $i; $j++)
{
echo $parts[$j]."/";
};
echo("\">". str_replace('-', ' ', $parts[$i])."</a> » ");
}
};
};
?>
but its show only Home->step3>step4 ,its not show step2 and step3.
Actually I want multilevel breadcrumbs.
I recommend using Yii built in class CBreadcrumbs.
Besides that, you have logic error in your if statement. Home will show only if there are no other items.
Move if after echo home:
echo("home");
if (count($parts) > 2)
{
...
<?php
# Rewritten Ajmal PraveeN
$path = parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH);
$parts = explode('/',$path);
$stepname='Step'.$step->step_code;
if (count($parts) < 2)
{
echo("home");
}
else
{
for ($i = 0; $i < count($parts); $i++)
{
if (!strstr($parts[$i],"."))
{
echo("<a href=\"");
for ($j = 0; $j <= $i; $j++)
{
echo $parts[$j]."/";
};
echo("\">". str_replace('-', ' ', $parts[$i])."</a> » ");
}
}
}
?>
The above code works fine with slash directories
ex: If php explodes a path is /home/website/public or Infinite path levels, the script generates what you are looking for ;)
Changes I made are $i = 2 to $i = 0
and A suggestion instead of str_replace use CSS styling for the »
Thank you..!

Display contents of static files across multiple pages in PHP

I'm making an incredibly simple PHP blog which takes txt files from a directory and displays them, one after another, on a PHP page. This is fine, but I'm planning to have 365 of these files so it'd be nice to show them on multiple pages, Wordpress-style. What would the PHP be to generate these pages on the fly, say, to display 20 files per page?
The PHP I have to display the files is as follows:
function printFile($file) {
$handle = fopen("$file", "r");
while (!feof($handle)) {
echo fgets($handle) . "<br>";
}
echo "<hr>";
fclose($handle);
}
$files = scandir($directory);
$number = count($files, 0);
while ($number > 2) {
$number--;
$FileToPrint = $directory . $files["$number"];
echo $FileToPrint . "<br>";
printFile("$FileToPrint");
}
This should give you an idea:
// initialize some settings
$perPage = 20;
$page = intval($_GET['page']);
// getting the files (just *.txt)
$files = glob('directory/*.txt');
$numFiles = count($files);
// displaying the files for this page
$offset = $page * $perPage;
for($i = $offset; $i < ($offset + $perPage); $i++){
// just print the filename for now
echo $files[$i]. "<br>";
}
// page browser
$numPages = ceil($numFiles / $perPage);
for($i = 0; $i < $numPages; $i++){
echo ''.$i.'';
}

Categories