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.'';
}
Related
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));
In the code below, a url variable is increasing by 1 every time in a while loop. When $i is equal to 1000, the loop will end and $i will be displayed (from 1 all the way to 1000).
How do I display the value of $i after every loop, rather than waiting to the end?
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
echo $i;
}else{
break;
}
$i++;
}
You will need to flush message after each iteration. That way your browser will receive part of the information even if your request/response is still pending.
ob_implicit_flush(true);
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i) && $i < 1000)
{
echo $i;
$i++;
ob_flush();
flush();
}
ob_end_flush();
Move echo $i; outside of the if() statement:
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
// echo $i;
}else{
break;
}
echo $i;
$i++;
}
This scenario is surely better suited for for() loop instead of while()...
PHP happen on the server. To see each value for $i immediately you would need to make the server:
do it's thing -> display that thing -> reload.
you can reload with header() but that is limited to 20 reloads (I think) unless you use header("Refresh:0");
I would make it display by making a stacking condition.
if(isset($diplay)){//2+ times through
$display = $display . $i . "<br>";
}else{//first time through
$diplay = $i."<br>";
}
then put that inside your condition
if($i !== 1000) {
if(isset($diplay)){
$display = $display . $i . "<br>";
}else{
$diplay = $i."<br>";
}
}else{
break;
}
echo $display
echo $display;
Then increment your variable
$i++;
then refresh page and pass the variable at the same time.
header("Refresh:0; url=page.php?i=$i");
then you'll have to add a condition at the beginning that gets $i or assign it if not found.
if(isset($_GET['i'])){//meaning if it's found in the url like so ?i=$i
$i = $_GET['i'];
}else{//first time through
$i = 1;
}
///////////////////putting it all together////////////////////////
if(isset($_GET['i'])){
$i = $_GET['i'];
}else{
$i = 1;
}
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
if(isset($diplay)){
$display = $display . $i . "<br>";
}else{
$diplay = $i."<br>";
}
}else{
break;
}
echo $display;
$i++;
header("Refresh:0; url=page.php?i=$i");
}
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];
}
I'm really new at php just doing some work, I want to save images in a php array and then show them in the screen, but I cannot save them or display them.
<?php
$min = 1;
$max = 9;
$number1 = rand($min,$max);
for ($i=1 ; $i<=$number1 ; $i++){
$firstN [$i] = echo "<img src='index.jpg' border='0'>";
}
echo $firstN [1];
?>
This is what I got , and the last line is to test it but nothing works, I google the topic but it doesn't help.
Thanks in advance.
As long as index.jpg is in the same directory as your file, this should work:
<?php
$firstN = array();
$min = 1;
$max = 9;
$number1 = rand($min, $max);
for ($i = 0; $i < $number1; $i++){
$firstN[] = '<img src="index.jpg" border="0">';
}
echo $firstN[0];
?>
Cleaned up the code a bit. When storing information in the array, you don't use echo and, like Mister pointed out, you had a space in the echo at the bottom of the code between the array-variable and the brackets.
If I have a file called file.html how do i make 10 clones of this file via PHP such that they are renamed file1....file10?
This code makes the files but they are all blank when they should be duplicates of mypage.html (which is less than 1kb)
<?php
$text = file_get_contents('mypage.html');
for($i = 0; $i < 100; $i++) {
file_put_contents('file'.$i.'.html', $data);
}
?>
If I have a file called file.html how do i make 10 clones of this file via PHP such that they are renamed file1....file10?
This code makes the files but they are all blank when they should be duplicates of mypage.html (which is less than 1kb)
Look at your variable names:
$text = file_get_contents('mypage.html');
for($i = 0; $i < 100; $i++) {
file_put_contents('file'.$i.'.html', $data);
}
You meant $text, and 10 not 100 right ? Also make sure you have permission to write where you want those files.
$text = file_get_contents('mypage.html');
for($i = 1; $i <= 10; $i++) {
file_put_contents('file'.$i.'.html', $text);
}
It's best to specify a dump folder
$text = file_get_contents('mypage.html');
for($i = 1; $i <= 10; $i++) {
file_put_contents('cloned/file'.$i.'.html', $text);
}
In cloned folder your PHP script must have write permissions.
why not use php copy function?
php copy manual
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
HOw about just use the copy() function in a loop instead of worrying about the contents of the file?
$filename = 'mypage.html';
for ($i=1; $i < 11; $i++) {
copy($filename, 'file'.$i.'html');
}
Note the use of $i=1 and $i<11 in loop this would give values of 1-10 instead of 0-9.