Array -> file
Téma egyszerű:
Meg van adva array-ben egy header és footer, a felhasználó egy file-upload formon keresztül tölti fel az html filet.
Ez is bekerül egy array-be, majd a array_merge() funkcióval összerakom őket egy array-be.
page[] = header[] + body[] + footer[]
Namost ezt szeretném kirakni egy új fileba. Van valakinek tippje hogyan kell ezt csinálni, mert nekem nem akar menni még PHP Manuallal sem.
Itt a minta (biztos hogy nem jó):
<?
// Header array
$header[0] = "";
$header[1] = "include 'includes/pnAPI.php';";
$header[2] = "pnInit();";
$header[3] = "include 'includes/legacy.php';";
$header[4] = "include(\"header.php\");";
$header[5] = "OpenTable();";
// Footer array
$footer[0] = "CloseTable();";
$footer[1] = "include(\"footer.php\");";
$footer[2] = "if (function_exists(\'session_write_close\')) {";
$footer[3] = "session_write_close();";
$footer[4] = "}";
// Upload Folder name
$uploadfolder = "upload";
//
//// Current filedate
//
$current_date = date("y_W_m_d");
//
$filename = "./$uploadfolder/".date("y_W_m_d")."_".$_FILES['hirnok']['name'];
//
//
//if ($_FILES["hirnok"]["size"] > 100000) {
// echo "File too big!" ;
// exit;
//}
if (!file_exists($filename)) {
copy ($_FILES['hirnok']['tmp_name'],$filename) or die ("I can't copy file from temp to $uploadfolder !");
$fp = fopen($filename,"w") or die ("Oops! The PHP script cant open $filename") ;
$file = file($filename);
$contents = array_merge($header,$file,$footer);
while (list ($key,$value) = each($contents)) {
fwrite($fp,$value) or die ("Can't write to file");
}
fclose ($fp);
echo "<p>Success! $string File <b>".$filename."</b> copied to folder: $location</p>";
echo "<input type=button value=Return onclick=history.back()>";
exit;
} else {
echo "<p>Error! I can't copy <b>".$filename."</b> file to folder: $location</p>";
echo "<input type=button value=Return onclick=history.back()>";
exit;
}
?>
■ Meg van adva array-ben egy header és footer, a felhasználó egy file-upload formon keresztül tölti fel az html filet.
Ez is bekerül egy array-be, majd a array_merge() funkcióval összerakom őket egy array-be.
page[] = header[] + body[] + footer[]
Namost ezt szeretném kirakni egy új fileba. Van valakinek tippje hogyan kell ezt csinálni, mert nekem nem akar menni még PHP Manuallal sem.
Itt a minta (biztos hogy nem jó):
<?
// Header array
$header[0] = "";
$header[1] = "include 'includes/pnAPI.php';";
$header[2] = "pnInit();";
$header[3] = "include 'includes/legacy.php';";
$header[4] = "include(\"header.php\");";
$header[5] = "OpenTable();";
// Footer array
$footer[0] = "CloseTable();";
$footer[1] = "include(\"footer.php\");";
$footer[2] = "if (function_exists(\'session_write_close\')) {";
$footer[3] = "session_write_close();";
$footer[4] = "}";
// Upload Folder name
$uploadfolder = "upload";
//
//// Current filedate
//
$current_date = date("y_W_m_d");
//
$filename = "./$uploadfolder/".date("y_W_m_d")."_".$_FILES['hirnok']['name'];
//
//
//if ($_FILES["hirnok"]["size"] > 100000) {
// echo "File too big!" ;
// exit;
//}
if (!file_exists($filename)) {
copy ($_FILES['hirnok']['tmp_name'],$filename) or die ("I can't copy file from temp to $uploadfolder !");
$fp = fopen($filename,"w") or die ("Oops! The PHP script cant open $filename") ;
$file = file($filename);
$contents = array_merge($header,$file,$footer);
while (list ($key,$value) = each($contents)) {
fwrite($fp,$value) or die ("Can't write to file");
}
fclose ($fp);
echo "<p>Success! $string File <b>".$filename."</b> copied to folder: $location</p>";
echo "<input type=button value=Return onclick=history.back()>";
exit;
} else {
echo "<p>Error! I can't copy <b>".$filename."</b> file to folder: $location</p>";
echo "<input type=button value=Return onclick=history.back()>";
exit;
}
?>
Re: Array -> file
=================================
upload.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Upload</title>
</head>
<body>
<form action="process.php" method="post" enctype="multipart/form-data" name="hinrok" id="hirnok">
<input type="file" name="hirnok">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<input type="submit" name="btn_upload" value="Upload">
</form>
</body>
</html>
=======================================
upload.php
<?
// Header string
$header = "<?php
include 'includes/pnAPI.php';
pnInit();
include 'includes/legacy.php';
include(\"header.php\");
OpenTable();
?>";
// Footer string
$footer = "<?
CloseTable();
include(\"footer.php\");
if (function_exists('session_write_close')) {
session_write_close();
}
?>";
$uploadfolder = "./hirnok/";
//
// Current filedate
//
$current_date = date("y_W_m_d");
//
$filename = "./$uploadfolder/".date("y_W_m_d")."_".$_FILES['hirnok']['name'];
//
if ($_FILES["hirnok"]["size"] > 100000) {
echo "File too big!" ;
exit;
}
if (file_exists($filename)) {
unlink($filename);
}
copy ($_FILES['hirnok']['tmp_name'],$filename) or die ("I can't copy file from temp to $uploadfolder !");
$fp = fopen($filename,"r+") or die ("Oops! The PHP script cant open $filename") ;
$hirnok = file($filename) or die ("Can't read $filename into array!");
$body = implode("",$hirnok) or die ("Implode failed.");
$output = $header."\n".$body."\n".$footer;
$tmpfile = fwrite($fp, $output) or die ("Can't execute write command!");
echo $output;
fclose($fp);
echo "<p>Success! $string File <b>".$filename."</b> copied to folder: $location</p>";
echo "<input type=button value=Return onclick=history.back()>";
exit;
?>
====================================
Szerintem egyértelmű. Ha van kérdés/comment akkor nyugodtan írjatok ide