怎么自动生成htm文件

109588550 2015-05-20 01:24:51
为安全隐患问题,

想在上传图片的同时,在图片的目录自动生成个空白的htm文件。

图片目录是根据时间定义的 所以 目录有点多。

php 怎么实现这个?
...全文
184 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
109588550 2015-05-20
  • 打赏
  • 举报
回复
我竟然写了3条 实现了 现在问题是 怎么合并代码 $file_path = 'file/'; $tm_path = 'file/'.$Y.'/'; // file/2015 $path = 'file/'.$Y.'/'.$M.'/'; // file/2015/05 file_put_contents($path . '/index.html', '111'); file_put_contents($tm_path . '/index.html', '111'); file_put_contents($file_path . '/index.html', '111');
109588550 2015-05-20
  • 打赏
  • 举报
回复
引用 6 楼 lijpwsw 的回复:

$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/生成文件
lijpwsw 2015-05-20
  • 打赏
  • 举报
回复
引用 6 楼 lijpwsw 的回复:

$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));
}
你自己再改一下,这个递归没有判断退出条件
lijpwsw 2015-05-20
  • 打赏
  • 举报
回复

$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));
}
109588550 2015-05-20
  • 打赏
  • 举报
回复
应该设置成 图片所有路径下 都自动生成个html文件 比如图片地址是 file/2015/05/20/100.gif 那应该在file目录 2015目录 05目录和20目录下 都生成个空白的html文件 不知道怎么实现啊
109588550 2015-05-20
  • 打赏
  • 举报
回复
引用 1 楼 php_Soul 的回复:
like this

if (!file_exists('index.html')){
    file_put_contents('index.html', 'hello world');
}
创建位置怎么设置
Dleno 2015-05-20
  • 打赏
  • 举报
回复
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;
}
果酱很好吃 2015-05-20
  • 打赏
  • 举报
回复
touch('index.html')
Suckor 2015-05-20
  • 打赏
  • 举报
回复
like this

if (!file_exists('index.html')){
    file_put_contents('index.html', 'hello world');
}
109588550 2015-05-20
  • 打赏
  • 举报
回复
引用 10 楼 xuzuning 的回复:
想在上传图片的同时,在图片的目录自动生成个空白的htm文件 显然图片文件已经保存了,假定保存图片是的路径在 $img_path 中 那么只需
file_put_contents(dirname($img_path) . '/index.html', '');
如果在保存图片时不能确定相应目录是否存在,那么这样就可以了
@mkdir(dirname($img_path), 0666, true);
所有路径的目录都要有html文件
xuzuning 2015-05-20
  • 打赏
  • 举报
回复
想在上传图片的同时,在图片的目录自动生成个空白的htm文件 显然图片文件已经保存了,假定保存图片是的路径在 $img_path 中 那么只需
file_put_contents(dirname($img_path) . '/index.html', '');
如果在保存图片时不能确定相应目录是否存在,那么这样就可以了
@mkdir(dirname($img_path), 0666, true);

21,892

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧