PHP script to create file if no exist -
i have small problem php code. please take look:
<?php $urlud = $json['url']; $content = 'content 1 if file no exist generate'; $filename = $json['id'].".html"; if (file_exists($filename)) { echo "file exists!"; } else { file_put_contents('dir1/'. $filename, $content, file_append | lock_ex); } ?> <?php $urlud = $json['url']; $content = 'content 2 if file no exist generate'; $filename = $json['id'].".html"; if (file_exists($filename)) { echo "file exists!"; } else { file_put_contents('dir2/'. $filename, $content, file_append | lock_ex); } ?>
this code generate 2 files in different directories different content. problem is:
- this code keeps replacing old file content. solution make create if file doesn't exist ?
- any other input make code simpler?
thanks.
if (file_exists('dir1/'. $filename))
and
if (file_exists('dir2/'. $filename))
you have check full path of file not file name. e.g. imagine $filename "readme.txt" file not existing in script path. , expect in dir1 or dir2.
Comments
Post a Comment