-3

When I write a file using PHP,

<?php
    header('Access-Control-Allow-Headers: *');
    $texttowrite = $_POST['articletext'];
    $title = $_POST['title'];
    $name = $_POST['name'];
    // Illegal characters on Unix and Linux or any *nix server
    $illegal = array(" ", "?", "/", "\\", "*", "|", "<", ">", '"');
    // Legal characters
    $legal = array("-", "_", "_", "_", "_", "_", "_", "_", "_");
    // Replace it!
    $newtitle = str_replace($illegal, $legal, $title);
    $newname = str_replace($illegal, $legal, $name);
    $filename = "content/" + $newtitle + "-" + $newname;
    $myfile = fopen($filename, "w") or die("Unable to open file!");
    fwrite($myfile, $texttowrite);
?>

note: this program is supposed to read output and a file name from POST form data and write it to a server.

it just creates a file called "0" in the root directory of the server's file system. Is there anything that's going wrong?

No errors were generated by the compiler.

no ai please
  • 732
  • 3
  • 11
  • 24
  • 1
    Debug. Print the values of `$filename` just before the `fopen`. Is that what you expect? A bunch of prints will show you the values of each variables. Since it is not a fatal error (i.e. no error in your logs), it is a logic error. – Nic3500 Apr 24 '21 at 03:42
  • 1
    There is *definitely* a duplicate question somewhere among the 1,402,389 PHP questions (probably from 2008), but it is not easy to find. Can someone find it? A similar question, but for the `+=` operator: *[How can I use the += operator in PHP for a string?](https://stackoverflow.com/questions/6476940)* – Peter Mortensen May 16 '21 at 10:00
  • 1
    A duplicate (but it is ***not*** a canonical question. It is closed, but the close target is *not* at all the canonical question): *[Plus operator in PHP](https://stackoverflow.com/questions/25398760)* – Peter Mortensen May 16 '21 at 10:08
  • 1
    For future reference, this question was discussed on Meta: [Why is a question about creating a file in PHP not about programming?](https://meta.stackoverflow.com/q/407683/12892553) – Nimantha May 16 '21 at 10:34
  • 1
    What is the (canonical) duplicate? – Peter Mortensen May 16 '21 at 11:50

1 Answers1

2

String concatenation in PHP is ., not +.

user1597430
  • 1,138
  • 1
  • 7
  • 14