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