jeg legede lidt med denne
http://php.net/manual/en/function.file-put-contents.php#96217læg mærke til $hostname
$hostname = "ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . "/" . $directory . "/" . $file;
file_put_contents($hostname, $content, 0, $stream);
ændret code, jeg har lagt din function i toppen, da det er en god praksis altid at have dem i toppen, eller bedst i externe file(r) der includeres, og fået den til at lave en index file
<?php
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
// http://php.net/manual/en/function.file-put-contents.php#96217
$file = "index.html";
$content = "
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<header>
<h1> index side </h1>
</header>
<nav>
</nav>
<section>
</section>
<footer>
</footer>
</body>
</html>
";
if (isset($_POST['submit'])) {
$ftp_server = "example.com"; // virtuelt doamin
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "ftp_user"; // bruger jeg har opsat på min xampp, med rettigheder til example.com
$ftp_user_pass = "vov";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// $root="url/phptest";
$root = "www_test";
/* **************************************** */
/* create a stream context telling PHP to overwrite the file */
$options = array('ftp' => array('overwrite' => true));
$stream = stream_context_create($options);
/* **************************************** */
// check connection
echo "<center>";
if ((!$conn_id) || (!$login_result)) {
echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">';
echo "FTP connection has failed!";
echo "Attempted to connect to <b>$ftp_server</b> for user <b>$ftp_user_name</b>";
echo '</div>';
} else {
echo '<div style="background-color:green;padding:10px;color:#fff;font-size:16px">';
echo "Connected to <b>$ftp_server</b>, for user <b>$ftp_user_name</b>";
echo '</div>';
$foldername1 = random_string(10);
$foldername2 = random_string(10);
$foldername3 = random_string(10);
$directory = "$root/$foldername1";
if (ftp_mkdir($conn_id, $directory)) {
/* **************************************** */
/* and finally, put the contents */
$hostname = "ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . "/" . $directory . "/" . $file;
file_put_contents($hostname, $content, 0, $stream);
/* **************************************** */
$directory = "$root/$foldername1/$foldername2";
if (ftp_mkdir($conn_id, $directory)) {
/* **************************************** */
/* and finally, put the contents */
$hostname = "ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . "/" . $directory . "/" . $file;
file_put_contents($hostname, $content, 0, $stream);
/* **************************************** */
$directory = "$root/$foldername1/$foldername2/$foldername3";
if (ftp_mkdir($conn_id, $directory)) {
/* **************************************** */
/* and finally, put the contents */
$hostname = "ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . "/" . $directory . "/" . $file;
file_put_contents($hostname, $content, 0, $stream);
/* **************************************** */
echo "<br /><br /><b>$directory</b> created";
}
}
} else {
echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px">';
echo "Could not create directory: <b>$directory</b>";
echo '</div>';
}
}
echo "</center>";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="submit">
<input type="submit" name="submit" value="submit">
</form>
du skal selvføgelig ændre disse linjer:
$ftp_server = "example.com"; // virtuelt doamin
$conn_id = ftp_connect($ftp_server);
$ftp_user_name = "ftp_user"; // bruger jeg har opsat på min xampp, med rettigheder til example.com
$ftp_user_pass = "vov";$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// $root="url/phptest";
$root = "www_test";indlæg du også kunne læse
http://php.net/manual/en/function.file-put-contents.php#96217http://php.net/manual/en/function.file-put-contents.php#101408http://php.net/manual/en/function.file-put-contents.php#105896