Създаване на файлове
Автор: SandPrince
Създаване на файл /файлове
Един много прост урок как със силата на php можете да си направите система за създаване на файлове.
Урока е много елементарен и не изисква особенни познания.
Урока е преведен специално за Web-Tourist
започваме с един обикновен html код който можете да си го поставите където искате. не е нужно файла ви да е с разширение .php за да работи. ето кода:
<html>
<body>
<form action="addfile.php" method="post">
<p>File name - <input type="text" name="file_name" />
<p>
<p>File Extension:
<select name="file_ext">
<option value=".php">PHP</option>
<option value=".txt">Text</option>
<option value=".html">HTML</option>
<option value=".js">JavaScript</option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Submit!" />
</p>
</body>
</html>както виждате това е обикновен код за форма за писане. можете да го кръстите примерно addfile.html
Сега обаче трябва да си съзададем и един php файл – addfile.php
<?php
if(isset($_POST['submit'])) //has the form been submitted?
{
$file_directory = "/files/"; //директорията в която се записва файла
$file_name = strip_tags($_POST['file_name']);//the file's name, stripped of any dangerous tags
$file_ext = strip_tags($_POST['file_ext']); //the file's extension, stripped of any dangerous tags
$file = $file_directory.$file_name.$file_ext; //this is the entire filename
$create_file = fopen($file, "w+"); //съзадване на нов фай;
if(!$create_file)
{
die("Грешка в създаването/отварянето на файла! Вижте дали имате правилните права!\n");
}
$chmod = chmod($file, 0755); //Задаване на права.
//attempt to set the permissions
if(!$chmod)
{
echo("Грешка в промяната на правата на файла!\n"); //error changing the file's permissions
}
//attempt to write basic content to the file
if (fwrite($create_file, "Content goes here!") === FALSE) {
echo "Error writing to file: ($file)\n";
}
fclose($create_file);
echo "Файлът е създаден успешно!\n"; //tell the user that the file has been created successfully
exit; //exit the script
}else{ //the form hasn't been submitted!
header("Location: addfile.html"); //redirect the user back to the add file page
exit; //exit the script
}
?>В комбинация с
този урок става идеална форма за създаване на файлове.
Това е!!!


