Need a (simple) php script

wkrappen91

Power Member
Joined
Sep 9, 2010
Messages
564
Reaction score
726
Hey guys.
Im currently working on a project for my fellow BHWlers (will share it soon) but i need one more thing and i cant figure it out.
I want the visitor of my website to be able to load a txt file and then, by clicking a button display one line of it in a textfield.
when the button is clicked again, the next line should appear.
When the end is reached, it should start over at the top.

I hope you can help me:)
 
In php I would do it with a form that includes a hidden field that pass the variable for the current line number in the file.
 
PHP:
<?php
$file = file("filename.txt");//load contents into array which is split by each new line
//to print the 5th line of the file:
print $file[4];
?>
 
Thanks so far but i still cant figure it out (0 PHP skills..)
If anyone could go a little further and post the complete html/php code, i would be very very thankfull:)
 
*Bump*
Im offering a free scrapebox blast to the person who gives me the working code:)
 
:D Really?
This should take you like 5 minutes to throw down:(
 
Try this
Code:
<?php
$line_no = 0;
if(isset($_POST['line_no'])) {  $line_no = $_POST['line_no']; }
$lines = file("robots.txt");
$last_line = count($lines);
if ($line_no == $last_line) {
    $line_no = 0;
    } else {
    $line_no = $line_no +1;
    }
?> 
<form name="main_form" method="POST" action="<?php echo $PHP_SELF;?>" >
<input type="hidden" name="line_no" value="<?php echo $line_no;?>">
<?php print $lines[$line_no]; ?>
<p align="center"><input type="submit" value="Next"></p></form>
 
Hmmm, so you want everything free?
Nope. I said i will give away a free scapebox blast to the one who solves it:)
Try this
Code:
<?php
$line_no = 0;
if(isset($_POST['line_no'])) {  $line_no = $_POST['line_no']; }
$lines = file("robots.txt");
$last_line = count($lines);
if ($line_no == $last_line) {
    $line_no = 0;
    } else {
    $line_no = $line_no +1;
    }
?> 
<form name="main_form" method="POST" action="<?php echo $PHP_SELF;?>" >
<input type="hidden" name="line_no" value="<?php echo $line_no;?>">
<?php print $lines[$line_no]; ?>
<p align="center"><input type="submit" value="Next"></p></form>
Thanks man:) this works.
but im missing the ability to upload your own txt(that is where i am stuck...)
 
How can you upload a php file but not a txt file?
 
PHP:
<?
$file = @explode(".",strrev($_FILES['upload']['tmp_name']));
$target_path = getcwd(); //gets current working directory
if (strrev($file[0]) == "txt"){ // check to make sure file ends in txt
 if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) {
  //successfully uploaded
 }else{
  //error
 }
}else{
 //wrong file format
}
?>
HTML:
<form method='post' action='' enctype='multipart/form-data'>
 <center>
 <input type='file' name='upload'>
 <input type='submit' value='Upload'>
 </center>
 </form>
 
How can you upload a php file but not a txt file?

I can but i want the user to be able to do so.

hi.imandrew:
This is exactly what i need:) now i just need a combination of those two scripts:) thanks for your work sofar:)
 
PHP:
<?php
session_start();

/*
if(isset($_POST)&&is_array($_POST)&&sizeof($_POST)>0) {
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";	
}
*/

if(isset($_FILES)&&is_array($_FILES)&&isset($_FILES['uploadedfile']['tmp_name'])&&trim($_FILES['uploadedfile']['tmp_name'])!='') {
	
	
	//$dest = '/tmp/'.basename($_FILES['uploadedfile']['tmp_name']);
	
	$dest = './temp/'.basename($_FILES['uploadedfile']['tmp_name']);
	
	move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $dest);

	if(file_exists($dest)) {
		$cont = file_get_contents($dest);
		if(trim($cont)!='') {
			$bcont = explode("\n",$cont);
			$acont = array();
			
			foreach($bcont as $v) {
				if(trim($v)!='') {
					$acont[] = trim($v);
				}
			}
			
			echo 'line #1 => ' . $acont[0] . '<br><br>';
			$_SESSION['file'] = $dest;
			$_SESSION['line'] = 1;
			//echo "<pre>";
			//print_r($acont);
			//echo "</pre>";
		}
		//echo $cont;
	} else {
		unset($_SESSION['file']);
		unset($_SESSION['line']);
		echo 'file does not exists';
	}

	
	//echo "<pre>";
	//print_r($_FILES);
	//echo "</pre>";
} else

if(isset($_SESSION)&&is_array($_SESSION)&&sizeof($_SESSION)>0) {
	//echo "<pre>";
	//print_r($_SESSION);
	//echo "</pre>";
	
	if(isset($_POST['next'])&&$_POST['next']=='next'&&isset($_SESSION['line'])&&is_numeric($_SESSION['line'])) {
		$_SESSION['line']++;
	}
	
	if(file_exists($_SESSION['file'])) {
		$cont = file_get_contents($_SESSION['file']);
		if(trim($cont)!='') {
			$bcont = explode("\n",$cont);
			$acont = array();
			
			foreach($bcont as $v) {
				if(trim($v)!='') {
					$acont[] = trim($v);
				}
			}
			
			if(!isset($acont[$_SESSION['line']])) {
				$_SESSION['line'] = 1;
			}
			
			echo 'line #'.$_SESSION['line'].' => ' . $acont[$_SESSION['line']-1] . '<br><br>';

			//echo "<pre>";
			//print_r($acont);
			//echo "</pre>";
		}
		//echo $cont;
	} else {
		echo 'file does not exists';
	}
	
}


?>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" name="submit" value="Upload File" />
<?php if(isset($_SESSION['file'])&&trim($_SESSION['file'])!='') { ?><input type="submit" name="next" value="next" /><?php } ?>
</form>
 
sorry the html was not posted. here is the full code.

PHP:
<?php
session_start();

/*
if(isset($_POST)&&is_array($_POST)&&sizeof($_POST)>0) {
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";	
}
*/

if(isset($_FILES)&&is_array($_FILES)&&isset($_FILES['uploadedfile']['tmp_name'])&&trim($_FILES['uploadedfile']['tmp_name'])!='') {
	
	
	//$dest = '/tmp/'.basename($_FILES['uploadedfile']['tmp_name']);
	
	$dest = './temp/'.basename($_FILES['uploadedfile']['tmp_name']);
	
	move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $dest);

	if(file_exists($dest)) {
		$cont = file_get_contents($dest);
		if(trim($cont)!='') {
			$bcont = explode("\n",$cont);
			$acont = array();
			
			foreach($bcont as $v) {
				if(trim($v)!='') {
					$acont[] = trim($v);
				}
			}
			
			echo 'line #1 => ' . $acont[0] . '<br><br>';
			$_SESSION['file'] = $dest;
			$_SESSION['line'] = 1;
			//echo "<pre>";
			//print_r($acont);
			//echo "</pre>";
		}
		//echo $cont;
	} else {
		unset($_SESSION['file']);
		unset($_SESSION['line']);
		echo 'file does not exists';
	}

	
	//echo "<pre>";
	//print_r($_FILES);
	//echo "</pre>";
} else

if(isset($_SESSION)&&is_array($_SESSION)&&sizeof($_SESSION)>0) {
	//echo "<pre>";
	//print_r($_SESSION);
	//echo "</pre>";
	
	if(isset($_POST['next'])&&$_POST['next']=='next'&&isset($_SESSION['line'])&&is_numeric($_SESSION['line'])) {
		$_SESSION['line']++;
	}
	
	if(file_exists($_SESSION['file'])) {
		$cont = file_get_contents($_SESSION['file']);
		if(trim($cont)!='') {
			$bcont = explode("\n",$cont);
			$acont = array();
			
			foreach($bcont as $v) {
				if(trim($v)!='') {
					$acont[] = trim($v);
				}
			}
			
			if(!isset($acont[$_SESSION['line']])) {
				$_SESSION['line'] = 1;
			}
			
			echo 'line #'.$_SESSION['line'].' => ' . $acont[$_SESSION['line']-1] . '<br><br>';

			//echo "<pre>";
			//print_r($acont);
			//echo "</pre>";
		}
		//echo $cont;
	} else {
		echo 'file does not exists';
	}
	
}


echo '<form enctype="multipart/form-data" action="upload.php" method="POST">'."\n";
echo '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />'."\n"
echo 'Choose a file to upload: <input name="uploadedfile" type="file" /><br />'."\n";
echo '<input type="submit" name="submit" value="Upload File" />'."\n";

if(isset($_SESSION['file'])&&trim($_SESSION['file'])!='') { 
	echo '<input type="submit" name="next" value="next" />'."\n";
}

echo '</form>';

?>
 
Back
Top