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
Related
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
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");
}
I'm using TCPDF along with FPDI to create documents in PDF based on a template using a PHP form.
I need a way to ID them. The problem is - most of these documents were already created manually, thus having ID format already specified.
It's basically Name-DD.MM.YYYY-X.pdf and it can't change.
For example, Document-01.01.2014-2, where 2 means it's a second document issued that day.
Is there a way to do that automatically? I'm using a traditional <form> with action set to the PHP TCPDF script and it works flawlessly, but how to specify that if something was already generated today then do $var = $var + 1 and then reset it at midnight?
You could try using the function file_exists(), in order to test if the base name is already generated or not.
If it is, simply create a loop and increment your $var to test iteratively your documents.
Example:
$baseName = "Name-DD.MM.YYYY";
$extension = ".pdf";
$i = 0;
while(1)
{
if($i > 0)
$testName = $baseName."-".$i.$extension;
else
$testName = $baseName.$extension;
if(!file_exists($testName))
break;
$i++;
}
if($i > 0)
$validName = $baseName."-".$i.$extension;
else
$validName = $baseName.$extension;
Hoping my answer would help you,
Venom
so the title is not full clear, my question , I'm using the code to rename the file from directory present in the server the problem is i have to use the HTML form and php to update the file name, i want to do this : there will be an option on every file for renaming it when i click on the option the box pops up and i have to type the new name for file and save it , any help will be appreciated. (before down voting think about the question.)
The code that I'm using to update the file name
<?php
include("configuration.php");
$target = $_POST['filename'];
$newName = $_POST['newfilename'];
$actfoler = $_REQUEST['folder'];
$file = "files/users/";
$new ="files/users/";
$renameResult = rename($file, $new);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $file . " is now named " . $new;
} else {
echo "Could not rename that file";
}
header("Location:".$_SERVER["HTTP_REFERER"]);
?>
Try changing these lines:
$file = "uploads/$loggedInUser->username$actfolder/$target";
$new ="uploads/$loggedInUser->username$actfolder/$newName";
To:
$file = "uploads/{$loggedInUser->username}{$actfolder}/{$target}";
$new ="uploads/{$loggedInUser->username}{$actfolder}/{$newName}";
To explain why:
You are using variables inside a string, which means you will want to tell PHP where the variable ends. Especially when referencing objects or arrays, but also when you are placing variables right next to each other. I'm guessing PHP evaluated your original line to uploads/[Object]->usernamePizza/newname
I don't think you can call object properties in a string as you do.
try replace these lines :
$file = "uploads/".$loggedInUser->username."$actfolder/$target";
$new ="uploads/".$loggedInUser->username."$actfolder/$newName";
You may think about echoing $file and $new to confirm the path is nicely built.
On a side note, I'd recommend to really check the entries because this code can obviously lead to major security issues.
As the title said i need a way to set the variable name depending of what the name of the picture is (i got over 100 different pictures)
Since i got custom classes in another php file for each picture (like tags) like for example:
$picture1 = "hillside sunset";
$picture2 = "beach cyprus";
and so on, so i need to fetch each variable for each picture
Heres the current loop where the div class is going to be each pictures name ($PICTURENAME is just to define where this code goes and is irelevant codewise):
<?php
foreach (glob("img/*.jpg") as $filename)
{
$path = $filename;
$file = basename($path);
$file = basename($path, ".jpg");
echo '<div class="'.$PICTURENAME.'" id="'.$file.'"><img src="'.$filename.'"> '.$file.' </div>';
}
?>
Don't use 100+ variables. Using a database would make far more sense, but if you don't want to get into learning that (you should, though), using a data structure would still make far more sense.
You could create one array (and use it as a map), and have the filename as the key, and the value would be the tags.
In PHP, you can address a variable using another variable:
$name = "foo";
${$name} = "bar";
echo $foo; // prints "bar"
echo ${$name}; // the same as above
However, as Kitsune already recommended, you are better off using something else, e.g., an array.