将file_put_content放入文件夹中

时间:2015-10-06 09:36:44

标签: php

我正在尝试将我的文件放在file_put_contents的文件夹中,有人可以帮助我。

$invoegen_titel=$_POST['titel_form'];
$invoegen_datum=$_POST['datum'];
$invoegen_tekst=$_POST['tekst'];

$html_tekst= $invoegen_titel."</h1>"."<br>"."<p>".$invoegen_datum."</p>"."<br>"."<p>".$invoegen_tekst."</p>";
$previous = $_SERVER['HTTP_REFERER'];
$folder='blog';
var_dump(file_put_contents($folder."/".time().".html","<h1>".$invoegen_titel."</h1>"."<br>"."<p>".$invoegen_datum."</p>"."<br>"."<p>".$invoegen_tekst."</p>"));

3 个答案:

答案 0 :(得分:0)

使用此:

$lifeTime = 5; // life time, seconds

  $cached = TRUE;

  $config = array(
    'group'=>'default',  // dir
    'id' => '1',         // id cache
    'echo' => TRUE,      // echo or return
    'log'=>false,        // echo log, or not
    'ext' => 'js'        // extention cache file, default .html
  );

  $CacheFile = new CacheFile('/usr/cache/', $cached);
  $CacheFile->config($config, $lifeTime);
  if (!$CacheFile->start()){
        echo $invoegen_titel."</h1>"."<br>"."<p>".$invoegen_datum."</p>"."<br>"."<p>".$invoegen_tekst."</p>";

        $CacheFile->end();
  }

PHP类:

class CacheFile{

    private $cacheDir = '';
    private $cacheSubDir = '';
    private $fileName = '';
    private $cache = false;
    private $lifeTime = 0;
    private $echo = true;
    private $group = true;
    private $log = true;
    private $fileExt = 'html';


    public function __construct($cacheDir, $cache){
        $this->cacheDir = $cacheDir;
        $this->cache = $cache;
    }

    private function createdPatch(){
        if ($this->cache){
            if (!is_dir($this->cacheSubDir)){
                mkdir($this->cacheSubDir, 0777, true);
            }
            chmod($this->cacheSubDir, 0755);
        }
    }
    private function getSubDir($keyType, $keyValue){
        return (($keyType != '')?($keyType.'/'):'').mb_substr($keyValue, 0, 1)."/".mb_substr($keyValue, 1, 1)."/".mb_substr($keyValue, 2, 1)."/";
    }

    private function getCacheName($key){
        return md5($key).".".$this->fileExt;
    }

    public function config($conf = array('group'=>'post', 'id' => '0', 'echo' => TRUE), $time = 31536000){
        $this->group = $conf['group'];
        $this->cacheSubDir = $this->cacheDir.$this->getSubDir($conf['group'], md5($conf['id']));
        $this->fileName = $this->getCacheName($conf['group'].$conf['id']);
        $this->lifeTime = $time;
        if (isset($conf['echo']))
            $this->echo = $conf['echo'];
        if (isset($conf['log']))
            $this->log = $conf['log'];
        if (isset($conf['ext']))
            $this->fileExt = $conf['ext'];


    }

    public function start(){
        if ($data = $this->get()) {
            if ($this->echo){
                echo $data;
                return true;
            }else{
                return $data;
            }
        }
        ob_start();
        ob_implicit_flush(false);
        return false;
    }

    function end(){
        $data = ob_get_contents();
        ob_end_clean();
        if ($this->cache){
            $this->save($data.(($this->log)?'<!-- c:'.$this->group.':('.date("Y-m-d H:i:s", (time()+$this->lifeTime)).'/'.date("Y-m-d H:i:s").') -->':""));
        }
        $return = $data.(($this->log)?'<!-- g:'.$this->group.':('.date("Y-m-d H:i:s", (time()+$this->lifeTime)).'/'.date("Y-m-d H:i:s").') -->':"");
        if ($this->echo){
            echo $return;
            return true;
        }
        return $return;
    }

    public function get(){
        if (!file_exists($this->cacheSubDir.$this->fileName))
            return false;

        if (time() >= filemtime($this->cacheSubDir.$this->fileName) + $this->lifeTime){
            unlink($this->cacheSubDir.$this->fileName);
            return false;
        }

        if ($this->cache && file_exists($this->cacheSubDir.$this->fileName))
            if ($data = file_get_contents($this->cacheSubDir.$this->fileName, false))
                return $data;
        return false;
    }

    public function remove(){

        if (file_exists($this->cacheSubDir.$this->fileName)){
            echo unlink($this->cacheSubDir.$this->fileName);
            return true;
        }
        return false;

    }

    public function save($data){
        $this->createdPatch();
        if (file_put_contents($this->cacheSubDir.$this->fileName, $data, LOCK_EX))
            return true;
        return false;
    }

}

答案 1 :(得分:0)

  1. 确保目录存在。如果目录不存在,file_put_contents不会创建该目录。

  2. 请说明您在代码中遇到的问题。

  3. 更改这些部分

    $folder = time(); //make sure that time() returns string $folder = "blog/".$folder.'.html'; file_put_contents($folder, $html_tekst); var_dump(file_get_contents($folder));

答案 2 :(得分:0)

我修好了

<?php $list = file_get_contents('list.json'); $list = json_decode($list, true); $selector = $_POST['selector']; $d_or_t = $_POST['d_or_t']; if (isset($selector) && isset($d_or_t)) { // overwrite the selected domain of the list with the new value if they are not empty if ($d_or_t == "domain") { $list[$selector]['domain'] = $_POST['new']; } if ($d_or_t == "template") { $list[$selector]['template'] = $_POST['new']; } /*else { echo '<script type="text/javascript">alert("U bent vergeten een veld in te voelen!");</script>'; }*/ // store the new json } ?>
<!DOCTYPE html>
<html>
    <head>
        <title>Json values veranderen</title>
    </head>
    <body>
        <h2>Domain of Template veranderen met PHP script</h2>
        <form action="test.php" id="form" method="post">
            <select name="selector">
            <?php foreach ($list AS $key => $value) : ?>
                <option value="<?php echo $key; ?>">
            <?php echo $key; ?>
                </option>
            <?php endforeach; ?>
            </select>

            <select name="d_or_t">
            <option>domain</option>
            <option>template</option>
            </select>

            <input type="text" name="new" placeholder="Nieuw">
            <input type="submit" value="Veranderen">
        </form>
</body>
</html>

<?php  
echo "<ul>";
        foreach ($list as $key => $value) 
            {
                echo "<li>".$key."<ul>";
                foreach ($value as $key1 => $value1) 
                {
                    echo "<li>".$key1.": ".$value1."</li>"; }
                echo "</ul>"."</li>";
            }
    echo "</ul>";
     file_put_contents('list.json', json_encode($list));
?>