Jump to content

How to upload and read a text file


bulrush

Recommended Posts

First, my goals:

- The user selects a text file on their local PC (or their local network drive) to process.

- I want to read the file into an array and process it.

 

I'm having trouble with this. Here is my code so far:

<?php session_start(); 
$title='Import File v.10a';
?>
<!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" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <?php 
  echo '<title>'.$title.'</title>';
  ?>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
require_once('navmenu.php');
echo '<h2>'.$title.'</h2>';
?>

<!-- comment -->
<?php
// Connect to the database 
$dbc = mysqli_connect($host, $user, $password, $database);

$errcnt=0;
$models=array(); //Init array
$bullarr=array();
$model='';
$partname='';
$partsubname='';
$image='';
$ptype=''; //Product type in {type} tag.

if (isset($_POST['cmdImport'])) //Save records that were changed.
    {
    $filename=$_POST['txtFilename']; //File on user's machine.
   	$s='You are importing '.$filename;
   	crDebug($s); //DEBUG
   	if (move_uploaded_file($_FILES['txtFilename']['tmp_name'], 'upload/'))
   		{
   		$filename=$_FILES['txtFilename']['name'];
   		$s='Received '.$filename;
   		crInfomsg($s);
   		}
   	else {
   		$errmsg='File upload failed. Error is '.$_FILES['txtFilename']['error'];
       	crError($_SERVER['PHP_SELF'].' line '.__LINE__,$errmsg,true);
   		}
   		
    if (!(file_exists($filename)))
    	{
    	$errmsg='File '.$filename.' does not exist. Cannot continue.';
       	crError($_SERVER['PHP_SELF'].' line '.__LINE__,$errmsg,true);
    	}
    else {
    	}
    } //if isset($_POST['cmdImport']

?>

<!-------------------------------------------------------->
<form action="<?php echo $_SERVER['PHP_SELF'].'?'.SID; ?>" method="post">
<hr/>

<label for="cbxBrand">Pick one Brand</label>
<?php
$oldbrandarr=array(); //Init array.
    //Do query and loop here.
    //Show brands from zzbrands.
    $query = "SELECT bzz ".
    "FROM zzb ".
    "ORDER BY br ".
    ";";
    //crInfomsg($query); //DEBUG
    crCreateSelect($query, 'cbxBrand', 'brand')
?>

<p>Input file: <input type="file" name="txtFilename" id="txtFilename" value="" size="80" /> 

<br/><input type="submit" value="Import" name="cmdImport" />
</form>

</body>
</html>

 

My problem is I get an error each time "File upload failed" which is displayed above when I try to do move_uploaded_file().

 

I'm not experienced processing text files. Can someone help me or point me to a website?

 

One more thing, the subdir "upload" is located at the same level as this php file. That is where the uploaded files will go temporarily.

 

Link to comment
https://forums.phpfreaks.com/topic/209678-how-to-upload-and-read-a-text-file/
Share on other sites

Thank you. I fixed it. MY code now looks like this:

if (isset($_POST['cmdImport'])) //Save records that were changed.
    {
    $filename=$_POST['txtFilename']; //File on user's machine.
   	$b=$_FILES['txtFilename']['name'];
   	//$s='You are importing '.$_FILES['txtFilename']['tmp_name'];
   	//$s.=', basename='.$b;
   	//$s.=', txtFilename-name='.$_FILES['txtFilename']['name'];
   	//crDebug($s); //DEBUG
    $dest='upload/'.$b;
   	if (move_uploaded_file($_FILES['txtFilename']['tmp_name'], $dest))
   		{
   		$filename=$_FILES['txtFilename']['name'];
   		$s='Received '.$filename;
   		crInfomsg($s);
   		}
   	else {
   		$errmsg='File upload failed. Error is '.$_FILES['txtFilename']['error'];
       	crError($_SERVER['PHP_SELF'].' line '.__LINE__,$errmsg,true);
   		}

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.