Jump to content

Auto-Gen of .html or .php file on form submit


jstdevdarsh
 Share

Recommended Posts

Hey Everyone,

 

I'll start by saying that I am a very novice php programmer. But I am working on a file storage website, and I have it working and running properly, what you see here is on a new page called index2.php (Thought you might need to know) and full working version can be found here, http://www.jstpreview.net84.net now want I want the site to do is automatically generate a new .php or .html page once the user presses the "submit" button, I've looked through many tutorials to figure this out but anything I have tried so far didn't work. So anyways, the page generated should be named the name of the file uploaded plus the extension. I also want this page to be generated using a pre-uploaded template but if you look at the code, you will see that I havn't implemented that part yet.

 

<?php 
//Load the settings
require_once("settings.php");


$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{

$target_path = Settings::$uploadFolder;
$target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']); 

//Check the password to verify legal upload
if($_POST['password'] != Settings::$password)
{
	$message = "Invalid Password!";
}
else
{
	//Try to move the uploaded file into the designated folder
	if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
	    $message = "The file ".  basename( $_FILES['file']['name']). 
	    " has been uploaded";
	} else{
	    $message = "There was an error uploading the file, please try again!";
	}
}

//Clear the array
unset($_FILES['file']);
}



if(strlen($message) > 0)
{
$message = '<p class="error">' . $message . '</p>';
}

/** LIST UPLOADED FILES **/
$uploaded_files = "";

//Open directory for reading
$dh = opendir(Settings::$uploadFolder);

//LOOP through the files
while (($file = readdir($dh)) !== false) 
{
if($file != '.' && $file != '..')
{
	$filename = Settings::$uploadFolder . $file;
	$parts = explode("_", $file);
	$size = formatBytes(filesize($filename));
	$added = date("m/d/Y", $parts[0]);
	$origName = $parts[1];
	$filetype = getFileType(substr($file, strlen($file) - 3));
        $uploaded_files .= "<li class=\"$filetype\"><a href=\"$filename\">$origName</a> $size - $added</li>\n";
}
}
closedir($dh);

if(strlen($uploaded_files) == 0)
{
$uploaded_files = "<li><em>No files have been uploaded yet!</em></li>";
}

function getFileType($extension)
{
$images = array('jpg', 'gif', 'png', 'bmp');
$docs 	= array('txt', 'rtf', 'doc');
$apps 	= array('zip', 'rar', 'exe');

if(in_array($extension, $images)) return "Images";
if(in_array($extension, $docs)) return "Documents";
if(in_array($extension, $apps)) return "Applications";
return "";
}

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 
   
    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 
   
    $bytes /= pow(1024, $pow); 
   
    return round($bytes, $precision) . ' ' . $units[$pow]; 
} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css" media="all"> 
@import url("style/style.css");
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>JetStorm Technologies | Staff - CloudNet</title>
</head>
<body>

<div id="container">


<h1></h1>

<form method="post" action="index.php" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="15728640" />
<fieldset>
	<legend>Add a new file to CloudNet:</legend>
		<?php echo $message; ?>
		<p><label for="name">Select file:</label><br />
		<input type="file" name="files" /></p>
		<p><label for="password">Authentication for upload:</label><br />
		<input type="password" name="password" /></p>
		<p><input type="submit" name="submit" value="Start upload" /></p>


	</fieldset>
</form>
<fieldset>

<center>

<?php

if(isset($_FILES['file']));
//else $file=1;
$file = $_POST["$origName"]; // grabbed from the form
$file = fopen("filepages/" . $origName .".php","x"); //(Creates the file)
fwrite($out, "<p> Man this really does not work $origName</p>"); // Generates the contents of file

?>
</center>

</fieldset>

<fieldset>
	<legend>Previousely uploaded files</legend>
		<ul id="menu">
			<li><a href="">All files</a></li>
			<li><a href="">Documents</a></li>
			<li><a href="">Images</a></li>
			<li><a href="">Applications</a></li>
		</ul>

		<ul id="files">
			<?php echo $uploaded_files; ?>
		</ul>
</fieldset>

</div>

<script src="js/filestorage.js" />
</body>
</html>

 

I would greatly appreciate it if someone can point out and fix the errors in this code, but it would be even more appreciated if someone could provide a working source code of auto-generation of pages on form submit.

 

Thanks in advanced

Link to comment
Share on other sites

to read data from a template and write it to another file, you could use something like:


<?php
// read data from the template:
$template = file_get_contents('template.php');
// define name for new file
$newFile = 'testing'.'.php';
// create new file and write data from template
file_put_contents($newFile,$template);
?>

Link to comment
Share on other sites

to read data from a template and write it to another file, you could use something like:


<?php
// read data from the template:
$template = file_get_contents('template.php');
// define name for new file
$newFile = 'testing'.'.php';
// create new file and write data from template
file_put_contents($newFile,$template);
?>

 

Thanks you for this, It was helpful but now I have another issue, i tried replacing 'testing' to various id's but none of them work, i tried $origName and a few others. Can you please re format this code to do the task i want it too. It would be great help. I'm also not sure about where to place this php script, i keep moving it but it writes the file without any name when the end-user just opens the page.

 

Please help me out.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.