Problem assigning path var to $fcontents = file ($userfile);

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jdmfontz
Forum Newbie
Posts: 17
Joined: Wed Apr 15, 2009 12:38 pm

Problem assigning path var to $fcontents = file ($userfile);

Post by jdmfontz »

I seem to be having a problem assigning a path variable within file(). For some reason when I assign a path like $fcontents = file ($path/$userfile[/b]); it does not seem to work. Does anyone see a problem here (besides me being new to php)?

$path = '/usr/local/apache2/htdocs/files';

//Insert file into table

$fcontents = file ($path/$userfile);
// expects the csv file to be in the same dir as $path
$counter = 0;
$success = 1;
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode(",", $line);
$sql = "insert into $tablename values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo "<br />" . $sql . "<br />";
$counter = $counter + 1;
if(mysql_error()) {
$success = 0;
echo "WARNING, there were errors: " . mysql_error() . "<br />";
}
}

if ($success == 0) {
echo "<br />" . "Please address the errors. " . "<br />";
}
else {
echo "<br />". "Total records inserted into table " . $tablename . ": " . $counter . "<br />";
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem assigning path var to $fcontents = file ($userfile);

Post by requinix »

/ means division. $A/$B means $A divided by $B. If $A or $B aren't numbers then PHP will pretend they are anyways.

Read.
jdmfontz
Forum Newbie
Posts: 17
Joined: Wed Apr 15, 2009 12:38 pm

Re: Problem assigning path var to $fcontents = file ($userfile);

Post by jdmfontz »

Thanks. Nice reference.
I tried to escape the / with a \

in $fcontents = file ($path\/$userfile);

but that does not seem to work.

I also tried double quotes ($path = "../files") with no success.

Them tried to put the two variables defined as another variable:

$path = '../files/$userfile';
$fcontents = file ($path);

and that did not work either.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem assigning path var to $fcontents = file ($userfile);

Post by requinix »

jdmfontz wrote:I tried to escape the / with a \

in $fcontents = file ($path\/$userfile);

but that does not seem to work.
Escaping only works in strings. And anyways, / doesn't need to be.
jdmfontz wrote:I also tried double quotes ($path = "../files") with no success.
You assigned "../files" to $path, then used that as the path to look through. Completely different location.
jdmfontz wrote:Them tried to put the two variables defined as another variable:

$path = '../files/$userfile';
$fcontents = file ($path);
That page I linked you to explains why that doesn't work.
jdmfontz
Forum Newbie
Posts: 17
Joined: Wed Apr 15, 2009 12:38 pm

Re: Problem assigning path var to $fcontents = file ($userfile);

Post by jdmfontz »

Thank you. After reviewing the reference a second time, it looks like this works:

$fcontents = file($path . '/' . $userfile);

M
Post Reply