SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2014
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    import csv using pdo

    hello. im trying to figure out how to get the file path of the uploaded file. right now i have it set by default so i can only upload from the specific file.

    Code:
    <?php
    
    $message = null;
    
    $allowed_extensions = array('csv');
    
    $upload_path = 'C:\Xampp\htdocs\EMS2\plugin\csv\exercise-files\final';
    
    if (!empty($_FILES['file'])) {
    
    	if ($_FILES['file']['error'] == 0) {
    			
    		// check extension
    		$file = explode(".", $_FILES['file']['name']);
    		$extension = array_pop($file);
    		
    		if (in_array($extension, $allowed_extensions)) {
    	
    			if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) {
    		
    				if (($handle = fopen($upload_path.'/'.$_FILES['file']['name'], "r")) !== false) {
    					
    					$keys = array();
    					$out = array();
    					
    					$insert = array();
    					
    					$line = 1;
    					
    					while (($row = fgetcsv($handle, 0, ',', '"')) !== FALSE) {
    				       	
    				       	foreach($row as $key => $value) {
    				       		if ($line === 1) {
    				       			$keys[$key] = $value;
    				       		} else {
    				       			$out[$line][$key] = $value;
    				       			
    				       		}
    				       	}
    				        
    				        $line++;
    				      
    				    }
    				    
    				    fclose($handle);    
    				    
    				    if (!empty($keys) && !empty($out)) {
    				    	
    				    	require "connection.php";
    				   		$dbh->exec("SET CHARACTER SET utf8");
    				    
    				    	foreach($out as $key => $value) {
    				    	
    				    		$sql  = "INSERT INTO `books` (`";
    				    		$sql .= implode("`, `", $keys);
    				    		$sql .= "`) VALUES (";
    				    		$sql .= implode(", ", array_fill(0, count($keys), "?"));
    				    		$sql .= ")";
    				    		$statement = $dbh->prepare($sql);
    				    		$statement->execute($value);
    				    		
    				   		}
    				   		
    				   		$message = '<span class="green">File has been uploaded successfully</span>';
    				   		
    				   	}	
    				    
    				}
    				
    			}
    			
    		} else {
    			$message = '<span class="red">Only .csv file format is allowed</span>';
    		}
    		
    	} else {
    		$message = '<span class="red">There was a problem with your file</span>';
    	}
    	
    }
    
    ?>
    html
    Code:
    <form action="" method="post" enctype="multipart/form-data">
    	
    		<table cellpadding="0" cellspacing="0" border="0" class="table">
    			<tr>
    				<th><label for="file">Select file</label> <?php echo $message; ?></th>
    			</tr>
    			<tr>
    				<td><input type="file" name="file" id="file" size="30" /></td>
    			</tr>
    			<tr>
    				<td><input type="submit" id="btn" class="fl_l" value="Submit" /></td>
    			</tr>
    		</table>
    		
    	</form>

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    443
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you mean - can you expand on the problem? Do you want to get the original path (from their PC) of the file that the user uploaded? $_FILES['tmp_name'] gives you the location of the file as it arrived on the server, but you're using that in your code so you know that.

    So, not sure what you're asking.
    http://www.firenza.net - my homage to a car from the 1970s


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •