tobeyt23 Posted January 4, 2007 Share Posted January 4, 2007 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 filefor ($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 More sharing options...
tobeyt23 Posted January 4, 2007 Author Share Posted January 4, 2007 Sorry left code off earlier! Link to comment https://forums.phpfreaks.com/topic/32876-need-help-reading-from-a-file/#findComment-153058 Share on other sites More sharing options...
tobeyt23 Posted January 4, 2007 Author Share Posted January 4, 2007 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 Link to comment https://forums.phpfreaks.com/topic/32876-need-help-reading-from-a-file/#findComment-153102 Share on other sites More sharing options...
ShogunWarrior Posted January 4, 2007 Share Posted January 4, 2007 [code]<?php//Connect only once$link = mysql_connect('localhost', 'admin', 'Pu22le#1') or die ('Could not connect: ' . mysql_error());//Select DB oncemysql_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 mysqlmysql_close($link);//Close filefclose($csvFile);[/code]This optimized, streamlined code should work. Link to comment https://forums.phpfreaks.com/topic/32876-need-help-reading-from-a-file/#findComment-153117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.