making clones of an html file - php

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.

Related

PHP ZipArchive list only one level of files/folders

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));

How to sort php results alphabetically?

I just started using PHP last week. I was trying to find out how to point at a folder of images and return thumbnails with links to larger images. After doing some research I found a script that will let me do exactly what I need, almost.
Here is the code I have:
$files = glob("Bookcase/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img class="thumb" img src="'.$num.'">'." ";
}
The only thing this script doesn't do for me is sort the images alphabetically. I don't know near enough about PHP yet to do much of anything. How can I accomplish this? Any help would be appreciated.
$files = glob("images/*.*");
sort($files);
$lenth = count($files);
for ($i=0; $i<$lenth; $i++){
//your code
}
Use this code to sort your images alphabetically.
$files = glob("Bookcase/*.*");
$myarr = array();
foreach($files as $file){
$myarr[$file];
}
sort($file);
$clength = count($file);
for($i = 0; $i < $clength; $i++) {
$num = $file[$i];
echo '<img class="thumb" img src="'.$num.'">'." ";
}

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];
}

Store images in array PHP

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.

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