Jump to content

Recommended Posts

So i'm a php n00b and working on a script here.

 

My first problem is, i'm trying to read a txt file into an array but the file I am trying to read is not in the same directory as the php file but the only way I can get it to work is to have the file in the same directory as the php file.

 

Here is an example of what I have:

 

$fcontents = file("./Applications.txt");

 

The file I want it to read is not in that directory though but the only way I can get it to work is to drop it in that directory, if someone can give me the correct syntax I would be grateful.

 

 

My next problem is trying to take a user selected option in an html file and use it in my php file.

 

For example I have something like this for my html file:

 

<form method=post action="http://xxxxx/upload.php">

<hr>

<p>

Select the file delimiter<br>

<select name=delimiter>

        <option value="," selected>Comma

        <option value="\t" selected>Tab

        <option value=";" selected>Semi-Colon

 

</select><br><br>

 

<form enctype="multipart/form-data" action="http://xxxxxx/forms.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="1000">

Send this file: <input name="userfile" type="file">

<input type="submit" value="Send File">

</form>

</form>

 

 

Notice I am trying to have my user upload a file as well as specify the file delimiter. I don't know if this is the correct syntax to do both things but I am trying.

 

 

Now on to the PHP

 

$delimiter = $_POST['delimiter'];

echo $delimiter;

 

  for($i=1; $i<sizeof($fcontents); $i++) {

      $line = trim($fcontents[$i]);

      $arr = explode("\t", $line);

      #if your data is comma separate

      # instead of tab separated,

      # change the '\t' above to ','

 

      $sql = "insert into Application values ('".

                  implode("','", $arr) ."')";

      mysql_query($sql);

      echo $sql ."<br>\n";

      if(mysql_error()) {

        echo mysql_error() ."<br>\n";

   

 

Notice initially I am trying to take the info that the user selected from the drop down menu and read it into an array called $delimiter

 

Next I believe I would want to change this particular line in the code:

 

$arr = explode("\t", $line);

 

To read:

 

$arr = explode($delimiter, $line);

 

I believe what that would do is no matter what the user selects as the delimiter it would put their selection there and parse the file accordingly.

 

Then the next segment would write the output to a mysql database.

 

At least that is what I am understanding it should do.

 

If someone would please help a n00b out I would be greatly appreciative.

Link to comment
https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/
Share on other sites

Okay where is the text file in relation to the php file ?

ie

index.php <--file to open text file

+---Data

        +---TextFiles

                  +---Applications.txt <---File to open

 

code

$fcontents = file("Data/TextFiles/Applications.txt");

 

Next.. the forms

your closing the first form after the 2nd form this is a no no also they are going to different php files.. with only 1 submit. also a no no..

 

so let join these first

you can include the upload.php in the forms.php if you wish (it may work) depends on the coding..

 

<form enctype="multipart/form-data" action="http://xxxxxx/forms.php" method="post">
<hr>
<p>
Select the file delimiter<br>
<select name=delimiter>
        <option value="," selected>Comma
        <option value="\t" selected>Tab
        <option value=";" selected>Semi-Colon

</select><br><br>

<input type="hidden" name="MAX_FILE_SIZE" value="1000">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>

 

php code you posted looks okay (your need to test)

 

Next

Sounds okay.. you seam to have a good idea what your plaing to do (tests required)

 

Any problems please post back

Thanks for the speedy response!!

 

So you're saying include the upload.php in the forms.php

 

I'm not quite sure how that would look

 

Here is my upload.php. It was working earlier but now it keeps defaulting to my else statement >:(

 

<html><head><title>PHP Test</title></head> <body>

Upload started.<br>

<?php

if (is_uploaded_file($_FILES['userfile']['tmp_name']))

{

move_uploaded_file($_FILES['userfile']['tmp_name'], "DUMMY.txt");

print "The file " . $_FILES['userfile']['name'] . " has been uploaded.<br>";

}

else

{

    print "Possible file upload attack. Filename: " . $_FILES['userfile']['tmp_name'];

}

?>

</body></html>

 

 

you could just add, the following code (from the upload.php) to the forms.php

 

<?php
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
   move_uploaded_file($_FILES['userfile']['tmp_name'], "DUMMY.txt");
   print "The file " . $_FILES['userfile']['name'] . " has been uploaded.<br>";
}
else
{
    print "Possible file upload attack. Filename: " . $_FILES['userfile']['tmp_name'];
}
?>

 

basically if you need to have 2 forms they will post their OWN data to wherever you point them but you can't point 1 form to 2 places

 

Hows the reading the text file comming along ?

Hows the reading the text file comming along ?

 

This may the one spot where I am having a few difficulties.

 

For example, the file uploads successfully.

The variable for the delimiter is successfully saved.

 

Now here is the full code from my forms.php

 

<?php

include 'upload.php';

/* connect */
$link = mysql_connect('xxxxxxxxx', 'USER', 'PASS')
or die('Could not connect: ' . mysql_error());
mysql_select_db('DB_NAME', $link)
or die('Could not select database');

mysql_query("DROP TABLE Appication");
mysql_query("CREATE TABLE Application (Name VARCHAR(255), Genre VARCHAR(255), Price VARCHAR(10), PRIMARY KEY (Name, Genre))");

$fcontents = file("/DUMMY.txt");
  # expects the csv file to be in the same dir as this script

$delimiter = $_POST['delimiter'];


  for($i=1; $i<sizeof($fcontents); $i++) {
      $line = trim($fcontents[$i]);
      $arr = explode($delimiter, $line);
      #if your data is comma separate
      # instead of tab separated,
      # change the '\t' above to ','
    
      $sql = "insert into Application values ('".
                  implode("','", $arr) ."')";
      mysql_query($sql);
      echo $sql ."<br>\n";
      if(mysql_error()) {
         echo mysql_error() ."<br>\n";
      }
}

?>

 

Now, currently the file being uploaded is uploaded to the same directory as the PHP file.

 

The code successfully creates the table correctly.

 

From that point it seems to die as no data is entered into the table.

 

I know the $delimiter is correct because if I echo it I get the result that I selected when I uploaded the file.

Now, I fix one thing and create a different problem now on the MySQL side of things....

 

So MySQL is checking with the $delimiter array to see that the user selected

 

It should be either a comma, semi-colon or tab

 

When I change the value from $delimiter to '\t' it enters the data correctly into the database

 

When I change back to $delimiter and select tab from when I upload the file I am getting a MySQL database stating Column count doesn't match value count at row 1

 

There is a syntax error going on somewhere

Ahhhhhh Magic Quote are on.. if you can turn them off in the PHP.INI file.. if not try this

 

change

$delimiter = $_POST['delimiter'];

to

if(get_magic_quotes_gpc()) {
$delimiter  = stripslashes($_POST['delimiter']);
} else {
$delimiter   = $_POST['delimiter'];
}

One thing i should point out is that if the data being imported has a single quote (') this is messing up the insert..

 

Try this

$arr = array_map("mysql_real_escape_string",$arr); //<--Add
$sql = "insert into Application values ('".implode("','", $arr) ."')"; //tidy
mysql_query($sql) or die("$sql<br>".mysql_error()); //updated

Sorry, I never even mentioned that part

 

The data to be inserted looks like

 

Gears of War Fiction 60

 

 

There are about 15 entries similar to that. These are tab delimited.

 

When I added that last part

 

$arr = array_map("mysql_real_escape_string",$arr);

 

I am getting the error:

 

insert into Application values ('Gears of War Fiction 60')

Column count doesn't match value count at row 1

 

 

I have a death grip on my hair, the little that I do have,.....so close

I have tried using two different files, one being applications and one using an old class roster

 

NAME	GENRE	PRICE
Gears of War	Fiction	60
NBA Live 09	Sports	60
Mortal Kombat vs DC Universe	Fiction	60
Namco Museum	Classic	30
Grand Theft Auto	Fiction	40
Rock Band II	Musical-Interactive	130
Fallout 3	Fiction	60
Call of Duty: World at War	Historic	60
Halo 3	Fiction	60
Major League Basebal 2K9	Sports	60

 

STUDENT NAME	Email 	Course
Richard O	user@testdomain.net	ENG_7120
Curtis Clifford	user@testdomain.net	ENG_7120
Shanee Terese	user@testdomain.net	ENG_7120
Wanda 	user@testdomain.net	ENG_7120
Jian 	user@testdomain.net	ENG_7120
Michael G	user@testdomain.net	ENG_7120
Jeffrey Michael	user@testdomain.net	ENG_7120
Jaeho 	user@testdomain.net	ENG_7120
Yueqin 	user@testdomain.net	ENG_7120
Jerome L	user@testdomain.net	ENG_7120
Balapuwaduge J P	user@testdomain.net	ENG_7120
Justus Nyamweya	Juser@testdomain.net	ENG_7120
Uday Sankar	user@testdomain.net	ENG_7120
Gregory Demetrius	user@testdomain.net	ENG_7120
Damodar Purushottam	user@testdomain.net	ENG_7120
Kevin Bernard	user@testdomain.net	ENG_7120
Prathap 	user@testdomain.net	ENG_7120
Yun 	user@testdomain.net	ENG_7120
Yixian 	user@testdomain.net	ENG_7120
Candice Harris	user@testdomain.net	ENG_7126
Karyn Lynn	user@testdomain.net	ENG_7126
Sheldon Oscar	user@testdomain.net	ENG_7126
Bobby R	user@testdomain.net	ENG_7126

I'm pretty sure they are tabbed but I added the new option to the form and I am gettings

 

Upload started.
The file WTF.txt has been uploaded.

array(1) { [0]=> string(38) "Richard O user@testdomain.net ENG_7120" } Wait

 

and

 

Upload started.
The file Applications.txt has been uploaded.

array(1) { [0]=> string(23) "Gears of War Fiction 60" } Wait

 

Thanks!!

 

The first option didn't work but the 2nd did!

 

Last question and I think I may be done harassing you; how can we make it so that it will read and work for comma, tab as well as semi-colon depending on what the user selects?

 

Right now it will read a tab delimited file but what about if we want to read the same file but at the comma or something like that?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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