PHP fopen 路径不工作

PHP fopen path is not working

我正在尝试使用 PHP 写入一个随机文件。 PHP 脚本位于 /var/www/html/ 我尝试创建的随机文件位于 /var/www/html/print 文件夹中。

我正在使用以下 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>

并跟随 php 编写文件。

<?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";
}
?>

如果我不使用 PHP 中的路径,文件创建成功但在 /var/www/html 文件夹中,我希望文件在 /var/www/html/print 文件夹中创建。

但是如果我使用文件路径,我会在日志中收到以下错误。

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

你的语法有误。

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

正确的说法。

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

希望对您有所帮助。

您忘记添加分号 (;)。

改变

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

对此

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