I am trying to create a PHP script that is supposed to recursively search for all files in filesystem by a name, return results and add 1 line into each selected file.
Here is what I have now: 1. Part which is looking for files, it is working fine:
$search = "test.txt";
$dir_iterator = new RecursiveDirectoryIterator("../");
$iterator = new RecursiveIteratorIterator($dir_iterator,
RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if($file->getFilename() == $search){
echo "<input type=\"checkbox\" name=\"optim[]\" value=\"$file\">
".$file."<br>";
}
}
?>
<input type="submit" value="Add to selected">
</fieldset>
</form>
This part works and returns a form with checkboxes. And here is a part which is supposed to create a backup of and add a line to selected:
$newfile = "text.txt$date";
$date = date('m-d-Y-H:i:s');
$target = "Yesterday was rainy";
$newline = "But today the sun is shining";
function update() {
foreach( $_POST as $fin ) {
if (!copy($fin, $newfile)) {
echo "failed to copy";
}
$stats = file($fin, FILE_IGNORE_NEW_LINES);
$offset = array_search($target,$stats) +1;
array_splice($stats, $offset, 0, $newline);
file_put_contents($fin, join("
", $stats));
if($newline) {
echo "All file updated";
} else {
echo "Sorry, I was unable to add :(";
}
}
But when a button is clicked and the function is called with
if($_SERVER['REQUEST_METHOD']=='POST')
{
update();
}
I get:
Warning: copy() expects parameter 1 to be a valid path, array given in itworked.php on line 51 failed to copy
Warning: file() expects parameter 1 to be a valid path, array given in itworked.php on line 54
Warning: array_search() expects parameter 2 to be array, null given in itworked.php on line 55
Warning: array_splice() expects parameter 1 to be array, null given in itworked.php on line 56
Warning: join(): Invalid arguments passed in itworked.php on line 57
Warning: file_put_contents() expects parameter 1 to be a valid path, array given in itworked.php on line 57
It looks like $fin is an array and my function can not be executed on it, but I do not understand why - shouldn't foreach work on elements of _POST?
I am a total rookie, newbie and a dummie, so any help would be greatly appreciated. Thank you.
UPD: sure, it is called with "update", and obviously not related.
UPD2: I tried changing foreach( $_POST as $fin ) with foreach( $_POST['optim'] as $fin )
and this solved most of the errors, but it looks like the function still does not recognize filenames. Here is the error:
Warning: copy(): Filename cannot be empty in itworked.php on line 50 failed to copy
And the 50 line is: if (!copy($fin, $newfile))
UPD3:
Hi. Per @quasimodos-clone suggestion I tried echoing $fin and got something like this:
../dir/test.txt
and the current working directory is
/files/testing/
So I though that it may be the cause. So I tried using $_SERVER['DOCUMENT_ROOT'] which does not include files in /files/otherfolder, only /files/testing/, but at least gives something.
This changed the output of $fin to:
/files/testing/dir/test.txt
But still when I try to add the line by submitting, I get the same error:
Warning: copy(): Filename cannot be empty in /files/testing/itworked.php
The line is:
if (!copy($fin, $newfile)) {
Have no idea what to try next :(