Jump to content

Uploading .txt Files


BicoloredNote

Recommended Posts

I am very new to PHP and I have a question. I am trying to upload a .txt and then display it on the page after it uploads. I already have the Browse...Upload buttons, everything in but when I go to upload it just prints out the code.

 

<?php
$okay = true;
if($_FILES['myfile']['error'] > 0) {
	echo 'A problem was detected:<br/>';
	switch ($_FILES['myfile']['error']){
		case 1: echo '* File exceeded maximum size allowed by server.<br/>';		break;
		case 2: echo '* File exceeded maximum size allowed by application.<br/>';	break;
		case 3: echo '* File could not by fully uploaded.<br/>';					break;
		case 4: echo '* File was not uploaded.<br/>';
	}
	$okay = false;
}

......

 

It prints everything after the ">" sign in the first line, instead of what the .txt file has.

 

I understand this may be a very broad question but any pointers or any other addition information needed, please let me know.

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/99305-uploading-txt-files/
Share on other sites

uploadfile.php

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>

<head>
	<title>REAL Bank™ ATM :: Upload Accounts</title>
</head>
<body>
<h1>Welcome to REAL Bank™</h1>
<hr>
<h2>Upload Accounts</h2>
<?php
$okay = true;
if($_FILES['myfile']['error'] > 0) {
	echo 'A problem was detected:<br/>';
	switch ($_FILES['myfile']['error']){
		case 1: echo '* File exceeded maximum size allowed by server.<br/>';		break;
		case 2: echo '* File exceeded maximum size allowed by application.<br/>';	break;
		case 3: echo '* File could not by fully uploaded.<br/>';					break;
		case 4: echo '* File was not uploaded.<br/>';
	}
	$okay = false;
}

if($okay && $_FILES['myfile']['type'] != 'text/plain') {
	echo 'A problem was detected:<br/>';
	echo '* File is not a text file.<br/>';
	$okay = false;
}

$filename = 'file.txt';
if ($okay) {
	if (is_uploaded_file($_FILES['myfile']['tmp_name'])){
		if (!move_uploaded_file($_FILES['myfile']['tmp_name'], $filename)) {
			echo 'A problem was detected:</br>';
			echo '* Could not copy file to final destination.<br/>';
			$okay = false;
		}
	}
	else {
		echo 'A problem was detected:<br/>';
		echo '* File to copy is not an uploaded file.<br/>';
		$okay = false;
	}
}

if ($okay) {
	echo 'File uploaded successfully.';
	$file			=			fopen ( $filename, 'r' );
	$fileContents	= nl2br (	fread ( $file, filesize( $filename )));
								fclose ($file);
	echo " <hr/> $fileContents <hr/>";
}
?>

<form action="REALBank.php" method="post">
<input type="submit" value="Main Menu" />
</form>
<hr>
</body>
</html>

 

 

uploadaccounts.php

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
<html>
<SCRIPT language=JavaScript>
function verify( form ){
	file = form.elements["myfile"];

	if ((file.value != null) && (file.value != "")) {
		return confirm("Upload "+file.value+"?");
	}
	alert( "Please provide a filename.");
	return false;
}
</SCRIPT>
<head>
	<title>REAL Bank™ ATM :: Upload Accounts</title>
</head>
<body>
<h1>Welcome to REAL Bank™</h1>
<hr>
<h2>Upload Accounts</h2>
<form enctype="multipart/form-data" action="uploadfile.php" method="post" 
onsubmit="return verify(this);">
<input type="hidden" name="MAX_FILE_SIZE" value="1000" />
Accounts file: <input type="file" name="myfile" size="30" />
<p><center>
<input type="submit" value="Upload" /></form></p>
<form action="REALBank.php" method="post">
<input type="submit" value="Main Menu" />
</center>
<hr>
</body>
</html>

 

 

When I upload the .txt file into uploadaccounts.php, it of course goes to uploadfile.php but it doesn't print out what the .txt file says. Instead, prints out the code from after the "<" sign.

Link to comment
https://forums.phpfreaks.com/topic/99305-uploading-txt-files/#findComment-508102
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.