简体   繁体   English

将file_put_content放入文件夹

[英]Getting file_put_content into a folder

I am trying to put my files in a folder from the file_put_contents can someone help me with that. 我试图将我的文件从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>"));

Use this: 用这个:

$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: 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. make sure that the directory is present. 确保该目录存在。 file_put_contents doesn't create the directory if it is not present. 如果该目录不存在,则file_put_contents不会创建该目录。

  2. Please specify what problems you are encountering on your code. 请指定您在代码上遇到的问题。

  3. change these parts 更换这些零件

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

I fixed it 我修好了它

<?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));
?>

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

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