Jump to content

Need help reading from a file


tobeyt23

Recommended Posts

I am trying to read from a csv file that has 2 vales fname, lastname. Then take those values and insert into a database then create new directories with those values in this format fname_lname. Can get them into the database but keep blowing up on creating the directories. Below is what i have to get them into the database, can this be done differently and how do i go about creating the databases? Also my ultimate goal is to completely automate this by having the file uploaded. Is that the best way to go?

[code]
<?php
$values = "";
// Read the file into an array
$file = file('pkg6_msoutput.csv');

// Count the number of lines
$lines = count($file);

// Loop through the lines in the file
for ($i=0; $i<$lines; $i++) {

// Splits fname and lname into own useable string
$name = explode(',', $file[$i]);

// Create MySQL insert query and insert data into database
$link = mysql_connect('localhost', 'admin', 'Pu22le#1') or die ('Could not connect: ' . mysql_error());
mysql_select_db('asce') or die('Could not select database');
$query = "INSERT INTO purls (id, fname, lname) VALUES ('null', '".strip_tags(addslashes($name[0]))."', '".strip_tags(addslashes($name[1]))."')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
mysql_free_result($result);
mysql_close($link);
}
?>
[/code]

Thanks in advance for help and suggestions.
Link to comment
https://forums.phpfreaks.com/topic/32876-need-help-reading-from-a-file/
Share on other sites

Here is the code I was using for the directory creation:

[code]
$dir = "".$fname."_".$lname."";
$file = "purl_index.php";
mkdir("".$dir."", 0757);
opendir("".$dir."");
$file = 'purl_index.php';
$newfile = "".$dir."/index.php";
if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
[/code]

Again anything will be helpful!!! ;D
[code]
<?php
//Connect only once
$link = mysql_connect('localhost', 'admin', 'Pu22le#1') or die ('Could not connect: ' . mysql_error());
//Select DB once
mysql_select_db('asce') or die('Could not select database');

//Open file
$csvFile = fopen('pkg6_msoutput.csv','r');
if( $csvFile !== False )
{
        while( $values = fgetcsv($csvFile,2000) )
        {
                $fname = $values[0];
                $lname = $values[1];

                $query = "INSERT INTO purls (fname, lname) VALUES ('".strip_tags(addslashes($fname))."', '".strip_tags(addslashes($lname))."')";
                $result = mysql_query($query) or die('Query failed: ' . mysql_error());
                //Free MySQL
                mysql_free_result($result);

                //Now let's create the directores
                $dir = "./".$fname."_".$lname."";
                $file = "purl_index.php";
                mkdir("".$dir."", 0757);
                clearstatcache();
                opendir("".$dir."");
                $newfile = "".$dir."/index.php";
                if (!copy($file, $newfile)) {
                    echo "failed to copy $file...\n";
                }
        }
}
else
{
        //Error opening!!
}
//Close mysql
mysql_close($link);
//Close file
fclose($csvFile);
[/code]

This optimized, streamlined code should work.

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.