简体   繁体   中英

Using PHP and Drupal username

I'm trying to do something where PHP reads the username logged in (I'm using Drupal) and then read a simple csv file in a directory I create for that user.

I verified that, yes, php is reading and storing my username to the variable I set up ($uname). I did a test where I echoed $uname and it works, and indeed shows my username on the site. However, when I concatencate the path, I get an error saying that the directory doesn't exist and the error shows everything except the username.

Here's what I have:

<?php

global $user;
$uname= $user->name;
$fdir = "./users/";
$fname="test.csv";
$slash = "/";

$fpath = $fdir.$uname.$slash.$fname;


$file_handle = fopen($fpath, "r");

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024);

print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

}

fclose($file_handle);

?>

UPDATED:

I am able to read a csv with a username if the file is in the same directory as the PHP script. However, I can't get it to use the username to go the subdirectory which is named after the user. How do I get this to happen?

Thanks for having a look.

Hmm...well, you're going to find this odd but this code works only if you put it into an article or page you create within Drupal and set it to contain PHP code. If you run it outside of Drupal (ie, as a standalone PHP script), doesn't work and it won't pick up the username logged into the Drupal site. Meh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM