Page 1 of 1

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

Posted: Mon Apr 20, 2009 2:36 pm
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 />";
}

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

Posted: Mon Apr 20, 2009 2:46 pm
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.

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

Posted: Mon Apr 20, 2009 3:12 pm
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.

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

Posted: Mon Apr 20, 2009 3:58 pm
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.

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

Posted: Tue Apr 21, 2009 8:17 am
by jdmfontz
Thank you. After reviewing the reference a second time, it looks like this works:

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

M