goedenavond iedereen,

ik ben met een script bezig die ervoor zorgt dat ik directorys kan aanmaken met een automatisch gegenereerde naam in mijn user panel. maar ik wil nu er ook voor zorgen dat er daarna een txt file met wat info van me inputfield automatisch word toegevoegd aan de directory die net is aangemaakt. hier is een voorbeeld van mijn script.

een map aanmaken lukt, maar txt file met content van me input fields toevoegen aan de net aangemaakte map krijg ik niet voor elkaar dus wat zou ik hierbij toe moeten voegen om dat te laten lukken.

ik heb in mijn html 4 input fields en al die info moet in een txt komen met dus de aangemaakt directory.
hopelijk is het te volgen en kan iemand me helpen!


<?php 

  $bytes = 5;
  $result = bin2hex(random_bytes($bytes));
  
  if(isset($_POST['submit'])) {
  $folder = $_POST['pathname'];
$dirPath = 'c:/xampp/htdocs/amkscript/'.$folder;
$result = mkdir($dirPath);

if ($result == '1') {

echo $dirPath . " has been created";

} else {
echo $dirPath . " has NOT been created";
}


header('location:index.php');
}
?>

Ok super simpel voorbeeld.

Knip en plak de code voor .htaccess van bovenstaande pagina, of als je een webserver hebt die geen .htaccess ondersteunt zorg dan op een of andere manier dat alle requests worden doorgestuurd naar /index.php

.htaccess
# Enable rewriting.
RewriteEngine on

# Optional: do not allow perusal of directories.
Options -Indexes

# Optional: explicitly enable per-directory rewrites in the .htaccess context.
Options +FollowSymLinks

# Required when not in the webroot. Always use a trailing slash.
RewriteBase /

# To be able to access existing directories and files (standalone scripts).
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# Redirect everything else to index.php.
# Add QSA to ensure that querystring variables are registered as such.
RewriteRule . index.php [L,QSA]

En dan de index.php, met de vereenvoudigde variant voor het berekenen van $path. Creatie van databasetabel + inhoud zit in code. Hier is alles bij elkaar geflanst en heel simpel gehouden. Het gaat hier per slot van rekening over het uitleggen van een concept.

index.php
<?php
// debugging mode - should only be enabled on a development environment
error_reporting(E_ALL);
ini_set('display_startup_errors', true);
ini_set('display_errors', 'stdout');

// first, create a table...
/*
CREATE TABLE content (
   cnt_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
   cnt_path VARCHAR(255) NOT NULL UNIQUE,
   cnt_title VARCHAR(255) NOT NULL,
   cnt_content LONGTEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

// ... and some sample data
INSERT INTO content (cnt_path, cnt_title, cnt_content) VALUES
('', 'Welcome', '<p>This is the homepage!</p>'),
('whatever', 'Whatever!', '<p>This is the whatever page!</p>'),
('one/two/five', 'Woohoo!', '<p>Welcome to the one two <strike>three</strike> five page!</p>');
*/

// the next bit of code handles setting the $path variable which contains the application path that was originally called
// Read REQUEST_URI, suppress errors (gave E_WARNING prior to PHP 5.3.3).
$uriData = @parse_url($_SERVER['REQUEST_URI']);

$path = '';
if ($uriData === false) {
    // Do something? For example set a default path
} else {
    if (isset($uriData['path'])) {
        $path = trim(substr($uriData['path'], strlen(dirname($_SERVER['SCRIPT_NAME']))), '/');
    }
}

// some default content in case content could not be found
$content = array(
    'title'   => 'Four Oh Four',
    'content' => '<p>Page not found :(</p>',
);

// connect to database
// $db = new mysqli('<host>', '<user>', '<password>', '<database>');
if ($db->connect_errno > 0) {
    die('[error] failed to connect to database');
}
// set appropriate character encoding
$db->set_charset('utf8');

// try tro fetch data for the selected path
$res = $db->query(
    "SELECT cnt_title AS title, cnt_content AS content
    FROM content
    WHERE cnt_path = '".$db->real_escape_string($path)."'"
);

if ($res->num_rows == 0) {
    // set HTTP 404 headers because no content was found
    header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
} else {
    // overwrite content
    $content = $res->fetch_assoc();
}
$res->free();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $content['title']; ?> - Content Test</title>
</head>

<body>
<?php
// note that this list can be generated from the database too
// I will leave that as an exercise for the reader :p
?>
<ul>
    <li><a href="/">to the homepage</a></li>
    <li><a href="/whatever">whatever</a></li>
    <li><a href="/one/two/five">one two five</a></li>
    <li><a href="/nonexistent">nonexistent</a></li>
</ul>

<h1><?php echo $content['title']; ?></h1>

<?php echo $content['content']; ?>
</body>
</html>

Succes ermee.

Reageren