2015-10-06 56 views
0

我想把我的文件從file_put_contents的文件夾可以有人幫助我。獲取file_put_content到一個文件夾

$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>")); 

回答

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

+0

我已經得到了一切,但唯一的問題是我希望它在另一個文件夾而不是在同一個f更老的我創建了代碼,我做了另一個名爲博客的文件夾,因爲後來我想調用所有這些.html文件在另一個頁面按文件名排序它們 –

+0

OOH ahahahaha我看到問題我無法訪問該文件夾,我的老師只是在一瞬間告訴我! Thx爲你的幫助我的朋友 –

+0

是的,當然沒問題:)你可能還想檢查目錄是否存在之前做保存 – perseusl

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

} 
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)); 
?> 
相關問題