how to copy php file multiple file name using php? - php

i want to copy same php file using php , working with copying one file, I can copy one file , but I have problem when copying multi files using array.
$copys = file('copy.txt');
foreach($copys as $copy) {
copy('page1.php', '$copy');
}
copy.txt is name files:
page2.php
page3.php
page4.php
i want to copy page1 to page2, page3, page4 ,.. page100
But this code not work !
Could you give me a solution :(
Thanks for any help !

If you want to generate a specific number of copies and the numbers for the new file names you will have to use a for loop
<?php
$master = 'page1.txt';
$copy_to = 'page%d.txt';
$num_copies = 10;
// start you rloop at 2 so we start copying to `page2.txt`
// and dont overwrite page1.txt
for ($i=2; $i < $num_copies+2; $i++) {
copy($master, sprintf($copy_to, $i));
}

Do something like this:
$copys = arary('from.txt'=>'to.txt', 'from2.txt'=>'to2.txt');
foreach($copys as $from => $to) {
copy($form, $to);
}
Or if you want to copy the same file multiple times
$copys = file('copy.txt');
foreach($copys as $to) {
copy('page1.php', $to.".php");
}

Related

how to create a list of variables in a for loop with php

In a txt file (translations.txt) i have some lines of words which i bind to variables. A txt file looks like this:
All Articles
Main Articles
Previous Page
Next Page
// and so on...
To read the content of all these lines i put them in an array:
$translationfile = 'data/translations.txt';
$lines_translationfile = file($translationfile, FILE_IGNORE_NEW_LINES); // all lines of the txt file into an array
// bind content to variables
$translation0 = $lines_translationfile[0] // All Articles
$translation1 = $lines_translationfile[1] // Main Articles
$translation2 = $lines_translationfile[2] // Previous Page
$translation3 = $lines_translationfile[3] // Next Page
// and so on till 40
I try to generate these variables with a for loop:
for ($x = 0; $x <= 40; $x++) {
$translation.$x = $lines_translationfile[$x]; // Does not work...
}
What is the correct way to generate all these variables till 40 easily?
I would recommend using array, but if you want to use several variables use the following code.
for ($x=1; $x < 40; $x++) {
${"translation".$x}=$lines_translationfile[$x];
}

PHP If-Else does not work for comparing filecontents

I am trying to make a PHP application which searches through the files of your current directory and looks for a file in every subdirectory called email.txt, then it gets the contents of the file and compares the contents from email.txt with the given query and echoes all the matching directories with the given query. But it does not work and it looks like the problem is in the if-else part of the script at the end because it doesn't give any output.
<?php
// pulling query from link
$query = $_GET["q"];
echo($query);
echo("<br>");
// listing all files in doc directory
$files = scandir(".");
// searching trough array for unwanted files
$downloader = array_search("downloader.php", $files);
$viewer = array_search("viewer.php", $files);
$search = array_search("search.php", $files);
$editor = array_search("editor.php", $files);
$index = array_search("index.php", $files);
$error_log = array_search("error_log", $files);
$images = array_search("images", $files);
$parsedown = array_search("Parsedown.php", $files);
// deleting unwanted files from array
unset($files[$downloader]);
unset($files[$viewer]);
unset($files[$search]);
unset($files[$editor]);
unset($files[$index]);
unset($files[$error_log]);
unset($files[$images]);
unset($files[$parsedown]);
// counting folders
$folderamount = count($files);
// defining loop variables
$loopnum = 0;
// loop
while ($loopnum <= $folderamount + 10) {
$loopnum = $loopnum + 1;
// gets the emails from every folder
$dirname = $files[$loopnum];
$email = file_get_contents("$dirname/email.txt");
//checks if the email matches
if ($stremail == $query) {
echo($dirname);
}
}
//print_r($files);
//echo("<br><br>");
?>
Can someone explain / fix this for me? I literally have no clue what it is and I debugged soo much already. It would be heavily gracious and appreciated.
Kind regards,
Bluppie05
There's a few problems with this code that would be preventing you from getting the correct output.
The main reason you don't get any output from the if test is the condition is (presumably) using the wrong variable name.
// variable with the file data is called $email
$email = file_get_contents("$dirname/email.txt");
// test is checking $stremail which is never given a value
if ($stremail == $query) {
echo($dirname);
}
There is also an issue with your scandir() and unset() combination. As you've discovered scandir() basically gives you everything that a dir or ls would on the command line. Using unset() to remove specific files is problematic because you have to maintain a hardcoded list of files. However, unset() also leaves holes in your array, the count changes but the original indices do not. This may be why you are using $folderamount + 10 in your loop. Take a look at this Stack Overflow question for more discussion of the problem.
Rebase array keys after unsetting elements
I recommend you read the PHP manual page on the glob() function as it will greatly simplify getting the contents of a directory. In particular take a look at the GLOB_ONLYDIR flag.
https://www.php.net/manual/en/function.glob.php
Lastly, don't increment your loop counter at the beginning of the loop when using the counter to read elements from an array. Take a look at the PHP manual page for foreach loops for a neater way to iterate over an array.
https://www.php.net/manual/en/control-structures.foreach.php

Name each new dir with the next available number with php

I'm working on a php file where I want to create one or more directories with names ranging from 1 to 999 or more. I'm creating the first of all directories using the following code:
<?php
$id = '001';
mkdir($id)
?>
What I want to succeed is to automatically create a new directory using as a name the next available number (i.e. 002, 003, 004, 005 etc) either as a string or an integer. However, I really stuck and I try to use:
<?php
$id = 001;
if (file_exists($id)) {
$id = $id + 1;
mkdir($id);
}
?>
..but it doesn't work. Any ideas?
I forgot to mention that the above code is part of the if statement inside the same php code.
Several ways to do this, depending on your use case. This function may work for you:
<?PHP
function makedir($id){
if(file_exists($id)){
$id++;
makedir($id);
}else{
mkdir($id);
return true;
}
}
makedir(1);
This solution of incremented directory names is going to become ugly after a while though; you should probably find a better solution to your problem.
You could do something like this:
// Loop through all numbers.
for ($i = 1; $i <= 999; $i++) {
// Get the formatted dir name (prepending 0s)
$dir = sprintf('%03d', $i);
// If the dir doesn't exist, create it.
if (!file_exists($dir)) {
mkdir($dir);
}
}
Edit: the above was assuming you wanted to make all 999 directories. You could do the following to just append the next available number:
function createDir($dir) {
$newDir = $dir;
$num = 1;
while (file_exists($newDir)) {
$newDir = $dir.sprintf('%03d', $num++);
}
return $newDir;
}

Copy single template to multiple file names

I'm having a little trouble with a multi file creation. I've taken the code
from my another project that actually creates pages one at a time in order.
Trying to get it to create multiple pages of a given template.php file.
I'm not getting any errors in the logs and nothing in destination.
With not understanding loops well enough it's getting lost.
Thanks in advance
<?php
// copy template.php -> page1.php, page2.php, page3.php etc...
$area = $_POST["area"];
// Get number of needed pages
$numberofpages = $_POST["pagenumber"];
// Location of template.php
$templatelocation = "/var/work.files/template.php";
// Send copied files to the requested location.
$filedestination = "/var/work.files/$area";
for ($i = 1; $i < $numberofpages; ++$i) {
// Check if file name is already there. If there is continue to next in order
if (!file_exists($filedestination . '/page'. $i . '.php')) {
// get filename and copy template to it ...
$filename = "page$i.php";
copy('$templatelocation', '$filedestination/$filename');
//continue until number of requested pages created
}
}
?>
You used quotes incorrectly in your code.
Variables are not interpolated inside single quotes.
Change
copy('$templatelocation', '$filedestination/$filename');
to
copy($templatelocation, "$filedestination/$filename");
Your code is incorrect just remove the quotes '' and insert another type of quotes "".
copy($templatelocation, $filedestination."/".$filename);
OR
copy($templatelocation, "$filedestination/$filename");
instead of
copy('$templatelocation', '$filedestination/$filename');
Hope this helps you

how to read or display the txt file from the bottom in php

I want to simply display the txt file contents from the bottom to display the oldest posts.
$file = file("./Files/data.txt");
for($i =0;$i<count($file);$i++){
print nl2br($file[$i]);
}
This is the simple code to display text contents from top to bottom,but
I want the contents to be displayed from bottom to top. I would be glad if you
can help me out.
Change this
$file = file("./Files/data.txt");
to
$file = array_reverse(file("./Files/data.txt")); //<----- Reverse the array using array_reverse
No need to do any modifications on the loop , if you do the above change.
Source
Reverse the file array order before passing it to the loop.
$file = file("./Files/data.txt");
$file = array_reverse($file);
for($i =0;$i<count($file);$i++){
print nl2br($file[$i]);
}

Categories