-1

I am trying to write to a random file with PHP. PHP script is located in /var/www/html/ the random files that I am trying create resides in /var/www/html/print folder.

I am using following javascript

<button id="button1" type="button">Write to File</button>
<script type='text/javascript'>
    $('#button1').click(function() {
        $.ajax({
            type: "POST",
            url: "printed.php",
            data: "",
            success: function(msg) {
                alert(msg);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Some error occured");
            }
        });
    });
</script>

and following php to write the files.

<?php
$filename = rand(5, 15);
$path = "/print/"
@ $fp = fopen("$path""$filename", "wb");
if (!$fp) {
    echo '<p><strong>Cannot generate message file</strong></p></body></html>';
    exit;
} else {
    $outputstring  = 'Hello, i have been generated by the button';
    fwrite($fp, $outputstring);
    Echo "Message inserted";
}
?>

If I don't use the path in PHP file is created successfully but in /var/www/html folder, I want the files to be created in /var/www/html/print folder.

But if I use the file path, I get following error in the logs.

PHP Parse error: syntax error, unexpected '@' in /netboot/var/www/html/printed.php on line 4

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
user2107349
  • 191
  • 2
  • 3
  • 16

2 Answers2

2

You have forgot to add semicolon (;).

Change

$path = "/print/"
@ $fp = fopen("$path""$filename", "wb");

To this

$path = "/print/";
@ $fp = fopen("$path" . "$filename", "wb");
Vasil Rashkov
  • 1,818
  • 15
  • 27
1

You have error in syntax.

@ $fp = fopen("$path""$filename", "wb");

Correct statement.

@ $fp = fopen("$path"."$filename", "wb");

Hope this will help you.

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20