How to make numbered PHP docs in order if 1 already exists?


#1

Alright,

I know that title may have been confusing, but I am trying to make it so with a form, it creates a PHP document, that for now is randomly generated strings of text that are MD5’d and then created and written, but with how annoying and ugly this is, I am thinking of how to make it, so that it creates a numeric PHP document just 1 number higher than the last one created, and now is there any ideas of how to do this…?

So if the user fills in the field, it creates 1.php

Then the second person does it, and it creates 2.php

And so on and so forth.

Any ideas or help? Thanks.

<?php

?>

#2

For this all you need to do is store the names in a database table. Make sure the tsble as an id column ans its set as an int and auto increment I’d also set it as the primary key then when ever there’s a submission the id will increment to the first record would be 1 then 2 then 3 etc

When pulling the records from the table make sure you order it by the id then the’ll appear in number order.


#3

You dont need a db for that, try this :

[php]

<?php $x = 1; while($file_number = 1000){ // this woul work just for 1000 pages $x++; $filename = "pages/$x.php"; //check if any file exists if(!is_file($filename)){ echo "$filename does not exists, you could create one with this name"; die(); } } ?>

[/php]

but i don`t recommend you to use a while loop because after 100 pages it need more than 10 seconds to loading :slight_smile:

you could also use a php page with variables

[php]<?php
$php_variables_page =“variables.php”;//define variable file name

if(!file_exists($php_variables_page)){//check if variables.php exists, if don`t we will create it
$filehandle = fopen($php_variables_page, ‘w’) or die(“can’t create php file”);
fclose($filehandle);
}

include("$php_variables_page");//include php file in order to get variable out of it :slight_smile:

if(!isset($page_number)){ //check if variable is set or we will set it to 0
$page_number = 0;
$string = ‘<?php’;
$string .= ’ $page_number=’.$page_number.’; ';//the string wich should be written to variables.php
$string .= ‘?>’;
$filehandle = fopen($php_variables_page, ‘w’) or die(“can’t create php file”);//fopen($php_variables_page, ‘a’) - to add to the file ‘w’ to rewrite
if(@fwrite($filehandle, $string)){
echo "First variable was added to variables.php,
";// you`ll get this message just once :slight_smile:
fclose($filehandle);
}else{
echo “Error, cannot write the string to file”;
}
}

if(isset($page_number)){
$new_page_name = $page_number+1;
$string = ‘<?php’;
$string .= ’ $page_number=’.$new_page_name.’; ';//the string wich should be written to variables.php
$string .= ‘?>’;
$filehandle = fopen($php_variables_page, ‘w’) or die(“can’t create php file”);//fopen($php_variables_page, ‘a’) - to add to the file ‘w’ to rewrite
if(@fwrite($filehandle, $string)){
echo “Variable was updated and written to the file”;// This is it
fclose($filehandle);
}else{
echo “Error, cannot write the string to file”;
}
}
?>[/php]

it could work so, ive writted and tested this just now but i woul recommand you to use mysql :slight_smile:


#4

As good as the other one sounds, how would I do this idea? Cause I think this could be a lot more organized. And it all could be stored in the database right?


#5

thats right it would.

Here’s a script that should take care of it you might need to tweak it slightly to fit your needs.

You’ll need a database table to store the file paths in. Create a table such as:

CREATE TABLE IF NOT EXISTS `files` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `file` varchar(255) NOT NULL,
  `size` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

This table will store a title the path to the file its size and type the id will auto increment on each entry.

In this file I’ve placed a connection to a database you will need to enter the right username and passwords etc also some functions the script will use ideally these would be in a separate file I’ve placed them together for simplicity.

For this to work there would be a folder on the server called files that has write permissions.

I commented the file to try to make it as clear as possbile

[php]

<?php //connct to database define('DB_SERVER', 'localhost'); define('DB_USER', 'database username'); define('DB_PASSWORD', 'password'); define('DB_NAME', 'database name'); @$conn = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD); mysql_select_db (DB_NAME,$conn); if(!$conn){ die( "Sorry! There seems to be a problem connecting to our database. Please give us a few minutes to remedy the problem. Thank you."); } // function the script will use ideally would be in a functions.php also the database connection would really in in its own file to such as config.php function getFileType($extension) { $images = array('jpg', 'gif', 'png', 'bmp'); $docs = array('txt', 'rtf', 'doc', 'pdf'); $apps = array('zip', 'rar', 'exe', 'html'); $video = array('mpg', 'wmv', 'avi'); $audio = array('wav', 'mp3'); $db = array('sql', 'csv', 'xls'); if(in_array($extension, $images)) return "Image"; if(in_array($extension, $docs)) return "Document"; if(in_array($extension, $apps)) return "Application"; if(in_array($extension, $video)) return "Video"; if(in_array($extension, $audio)) return "Audio"; if(in_array($extension, $db)) return "Database/Spreadsheet"; return "Other"; } 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]; } function errors($error){ if (!empty($error)) { $i = 0; while ($i < count($error)){ $showError.= '
'.$error[$i].'
'; $i ++;} return $showError; }// close if empty errors } // close function //form been submissed if (isset($_POST['upfile'])){ // check feilds are not empty $title = trim($_POST['title']); if (empty($title)){ $error[] = 'Please enter a Title.'; } $sql = "SELECT id FROM files WHERE title = '$title' "; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) {//thee's a match $error[] = 'File title already exists please choose another title.'; } //if no errors carry on if (!$error){ //where to move upload file to $target = "files/" . $_FILES['uploaded']['name']; //is an uploaded file if ($_FILES["uploaded"]["type"]) { move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target); $file = $_FILES["uploaded"]["name"]; $size = $_FILES["uploaded"]["size"]; $type = $_FILES["uploaded"]["type"]; //insert into database mysql_query(" INSERT INTO files (title, file, size, type) VALUES ('$title', '$file', '$size', '$type'")or die('Error : ' . mysql_error()); //show a result echo "

".$_FILES["uploaded"]["name"]." uploaded

"; } else { echo "File not uploaded."; } }// end no error }//close if isset upfile ?>

Upload File

<?php //show any errors echo errors($error); ?> Title: File:

Documents

<?php //pull all files from databsase and loop through them $q = mysql_query("SELECT * FROM files ORDER BY id"); while($row=mysql_fetch_object($q)) { //get the size of the file $size = formatBytes(filesize("files/".$row->file)); //get file type $filetype = getFileType(substr($row->file, strlen($row->file) - 3)); echo " "; } ?>
Filename Type Size Action
".$row->id." ".$row->title." ".$filetype." ".$size." Download
[/php]