繁体   English   中英

如何在服务器上创建文件?

[英]How do I create file on server?

假设我想在/css/文件夹中创建文件调用style.css。

示例:当我单击“保存”按钮脚本时,将创建包含内容的style.css

 body {background:#fff;}
 a {color:#333; text-decoration:none; }

如果服务器无法写入我想要的文件显示错误消息Please chmod 777 to /css/ folder

让我知道

$data = "body {background:#fff;}
a {color:#333; text-decoration:none; }";

if (false === file_put_contents('/css/style.css', $data))
   echo 'Please chmod 777 to /css/ folder';

您可以使用is_writable函数来检查文件是否可写。

例如:

<?php
$filename = '/path/to/css/style.css';
if (is_writable($filename)) {
    echo 'The file is writable';
} else {
    echo 'Please chmod 777 to /css/ folder';
}
?>

是您可能想要使用的功能

或使用

如果您打开文件并且操作结果为false,那么您无法写入文件(可能是权限,可能是安全模式下的UID不匹配)

file_put_contents(php5和upper)php为你调用fopen(),fwrite()和fclose(),如果id错误则返回false(你应该确定false确实是boolean值)。

打开 'w'旗帜

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>

http://php.net/manual/en/function.fwrite.php | 例1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM