HTMLファイルを自動生成する方法

WBOY
リリース: 2016-06-23 13:34:13
オリジナル
997 人が閲覧しました

セキュリティ上の理由から、

画像のアップロード中に画像ディレクトリに空の HTML ファイルを自動的に生成したいと考えています。

画像ディレクトリは時間に基づいて定義されているため、多くのディレクトリがあります。

PHP でこれを実現するにはどうすればよいですか?


ディスカッションに返信(解決策)

like this

if (!file_exists('index.html')){    file_put_contents('index.html', 'hello world');}
ログイン後にコピー
ログイン後にコピー

touch('index.html')

ecmall:

function ecm_mkdir($absolute_path, $mode = 0777){    if (is_dir($absolute_path))    {        return true;    }    $root_path      = ROOT_PATH;    $relative_path  = str_replace($root_path, '', $absolute_path);    $each_path      = explode('/', $relative_path);    $cur_path       = $root_path; // 当前循环处理的路径    foreach ($each_path as $path)    {        if ($path)        {            $cur_path = $cur_path . '/' . $path;            if (!is_dir($cur_path))            {                if (@mkdir($cur_path, $mode))                {                    fclose(fopen($cur_path . '/index.htm', 'w'));                }                else                {                    return false;                }            }        }    }    return true;}
ログイン後にコピー

like this

if (!file_exists('index.html')){    file_put_contents('index.html', 'hello world');}
ログイン後にコピー
ログイン後にコピー



作成の設定方法場所

画像のすべてのパスに HTML ファイルを自動的に生成するように設定する必要があります

たとえば、画像のアドレスは file/2015/05/20/100.gif です

その後、ファイル ディレクトリに生成される必要があります、2015 ディレクトリ、05 ディレクトリ、20 ディレクトリ 空の HTML ファイルの実装方法がわかりません

$imgUrl = '/file/2015/05/20/100.gif';$path = dirname($imgUrl);createHtml($path);function createHtml($path){	if (!file_exists($path . '/index.html')){	    file_put_contents($path . '/index.html', 'hello world');	}	createHtml(dirname($path));}
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー

$imgUrl = '/file/2015/05/20/100.gif';$path = dirname($imgUrl);createHtml($path);function createHtml($path){	if (!file_exists($path . '/index.html')){	    file_put_contents($path . '/index.html', 'hello world');	}	createHtml(dirname($path));}
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー


自分で変更できます、この再帰は終了条件を判断しません

$imgUrl = '/file/2015/05/20/100.gif';$path = dirname($imgUrl);createHtml($path);function createHtml($path){	if (!file_exists($path . '/index.html')){	    file_put_contents($path . '/index.html', 'hello world');	}	createHtml(dirname($path));}
ログイン後にコピー
ログイン後にコピー
ログイン後にコピー



/file/2015/05/20/写 にファイルのみを生成する しかし、私は実際に 3 と書きましたが、問題はコードをマージする方法であるということです


$ file_path = 'file path = 'file /'.$Y.'/'.$M.'/'; // file/2015/05
file_put_contents($path . '/index.html', '111'); Index.html', '111');
file_put_contents($file_path . '/index.html', '111');


htm をアップロードするときに、画像のディレクトリに空白を自動的に生成したいです。ファイル

明らかに画像ファイルは保存されています。画像を保存するパスが $img_path にあると仮定します。

あとは

file_put_contents(dirname($img_path) . '/index.html', '');
ログイン後にコピー
ログイン後にコピー
画像を保存するときに対応するディレクトリが存在するかどうかわからない場合は、これで問題ありません
@mkdir(dirname($img_path), 0666, true);
ログイン後にコピー
ログイン後にコピー




画像のアップロード中に、画像のディレクトリに空の HTML ファイルを自動的に生成したいです

画像を保存するパスが $img_path にあると仮定すると、画像ファイルは明らかに保存されています

あとは

file_put_contents(dirname($img_path) . '/index.html', '');
ログイン後にコピー
ログイン後にコピー
画像を保存する場合 対応するディレクトリが存在するかどうか判断できない場合は、これだけで十分です
@mkdir(dirname($img_path), 0666, true);
ログイン後にコピー
ログイン後にコピー



パス内のすべてのディレクトリに HTML ファイルが必要です

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!