我想在特定目录中创建文件failure-log.log并将其写入。已经从数据库获取目录路径。路径是这样的:
D:/folder-one/folder-two/
我的PHP代码正在另一个这样的目录中执行:
C:/apache24/crawler/admin/startService.php
如何创建文件并写入文件?
答案 0 :(得分:0)
使用file_put_contents方法,如下所示:
numbers
U可以将标志发送到override fun toString() = numbers.toString()
,就像$file ="D:/folder-one/folder-two/";
$current = file_get_contents($file);
$current .= 'yourcontenthere';
file_put_contents($file, $current);
一样:
file_put_contents
在这种情况下,您不必检索旧内容,可以在上面的链接中检查和其他标志。
检查之前是否存在文件也是个好主意。
答案 1 :(得分:0)
首先使该文件夹位置可用于Web服务器。然后使用下面的代码在该位置创建文件。
$myfile = fopen("D:/folder-one/folder-two/file.log", "a") or die("Unable to open location for log file !");
$txt = "Log details goes here ...";
fwrite($myfile, $txt);
fclose($myfile);
答案 2 :(得分:0)
确保您使用绝对路径(您也可以在相对路径上使用realpath()以确保该路径)并且目录可写
然后
incl_upp = False
incl_low = False
incl_int = False
incl_sym = False
checklist = []
char_types = []
password = []
length = input('# input length of password in integers | ')
if input('# include upper case alpha? (y/n) | ') == 'y':
incl_upp = True
print("# password now includes upper case alpha")
if input('# include lower case alpha? (y/n) | ') == 'y':
incl_low = True
print("# password now includes lower case alpha")
if input('# include integers? (y/n) | ') == 'y':
incl_int = True
print("# password now includes integers")
if input('# include special characters? (y/n) | ') == 'y':
print("# password now includes special characters")
incl_sym = True
if incl_upp == True:
char_types.append(UPPER_CASE)
checklist.append(UPPER_CASE)
if incl_low == True:
char_types.append(LOWER_CASE)
checklist.append(LOWER_CASE)
if incl_int == True:
char_types.append(NUMBERS)
checklist.append(NUMBERS)
if incl_sym == True:
char_types.append(SYMBOLS)
checklist.append(SYMBOLS)
while True:
count = 0
while count != length:
case = char_types[random.randint(0,int(len(char_types)-1))]
char = case[random.randint(0,int(len(case)-1))]
password.append(str(char))
count = count + 1
# for every char in password, for every type in list, for every character in type, check if that is in password
for i in range(len(password)):
for x in range(len(checklist)):
for y in range(len(checklist[x])):
if checklist[x[y]] in password[i]:
checklist[x] = True
for i in range(len(checklist)):
if checklist[i] != True or checklist[i] != False:
checklist[i] = False
if all(checklist):
random.shuffle(password) # added security?
password = "".join(password)
return password
如果您不想每次都删除文件内容,那么我建议使用FILE_APPEND
$dir = 'your/path';
file_put_contents($dir ."/failure-log.log", $contentOfFile);
答案 3 :(得分:-1)
写入文件
$writeFile = @fopen('/path/to/save/file', 'w+');
@fwrite($writeFile, $content);
@fclose($writeFile);
使用:
w+: will create a new file if it does not exist and overwrite if it exists
a: append to file already exist
a+: append to file already exist and create a new file if it does not exist
如果从数据库加载路径目录,则可能需要创建多目录
if( !is_dir($path) ) {
mkdir($path, 0777, true);
}
使用:
$path: path you were loaded from db