Author Topic: How to make numbered PHP docs in order if 1 already exists?  (Read 201 times)

Improvizionz

  • Regular Member
  • **
  • Posts: 50
  • Karma: 0
    • View Profile
How to make numbered PHP docs in order if 1 already exists?
« on: March 24, 2012, 06:16:58 PM »
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.

Code: [Select]
<?php

?>


daveismyname

  • Senior Member
  • ****
  • Posts: 301
  • Karma: 6
  • PHP Helper
    • View Profile
    • PHP Help Tutorials
Re: How to make numbered PHP docs in order if 1 already exists?
« Reply #1 on: March 25, 2012, 11:06:51 AM »
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.

andrutu991

  • Regular Member
  • **
  • Posts: 55
  • Karma: 5
    • View Profile
Re: How to make numbered PHP docs in order if 1 already exists?
« Reply #2 on: March 25, 2012, 12:35:09 PM »
You dont need a db for that, try this :

PHP Code: [Select]

<?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();
}
}
?>


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

you could also use a php page with variables

PHP Code: [Select]
<?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 :)

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, <br>";// you`ll get this message just once :)
	
	
	
	
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";
	
	
	
}
}
?>


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

Improvizionz

  • Regular Member
  • **
  • Posts: 50
  • Karma: 0
    • View Profile
Re: How to make numbered PHP docs in order if 1 already exists?
« Reply #3 on: March 25, 2012, 09:49:11 PM »
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.
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?

daveismyname

  • Senior Member
  • ****
  • Posts: 301
  • Karma: 6
  • PHP Helper
    • View Profile
    • PHP Help Tutorials
Re: How to make numbered PHP docs in order if 1 already exists?
« Reply #4 on: March 26, 2012, 03:34:13 AM »
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:

Code: [Select]
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 Code: [Select]

<?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_SERVERDB_USERDB_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($bytes0); 
    
$pow floor(($bytes log($bytes) : 0) / log(1024)); 
    
$pow min($powcount($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.= '<div class="msg-error hidethis">'.$error[$i].'</div>';
	
	
$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 
"<h3 align='center'> ".$_FILES["uploaded"]["name"]." uploaded</h3>";
	
	

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

?>

<h3>Upload File</h3>

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

<form enctype="multipart/form-data" action="" method="post">
Title:<input name="title" type="text" /> File:<input name="uploaded" type="file" maxlength="20" />
<input type="submit" name="upfile" value="Upload File">
</form>

<h3>Documents</h3>
<table>
	
<tr>
           <th></th>
           <th>Filename</th>
	
   <th>Type</th>
	
   <th>Size</th>
	
   <th>Action</th>
	
   
	
</tr>
	

<?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->filestrlen($row->file) - 3));
	
	


	
echo 
"
	
  <tr>
	
	
<td>"
.$row->id."</td>
                <td>"
.$row->title."</td>
	
	
<td>"
.$filetype."</td>
	
	
<td>"
.$size."</td>
	
	
<td><a href='"
.$row->file."'>Download</a></td>
	
  </tr>"
;
}

?>

</table>
« Last Edit: March 26, 2012, 03:36:07 AM by daveismyname »