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 />";
}
Problem assigning path var to $fcontents = file ($userfile);
Moderator: General Moderators
Re: Problem assigning path var to $fcontents = file ($userfile);
/ means division. $A/$B means $A divided by $B. If $A or $B aren't numbers then PHP will pretend they are anyways.
Read.
Read.
Re: Problem assigning path var to $fcontents = file ($userfile);
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.
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);
Escaping only works in strings. And anyways, / doesn't need to be.jdmfontz wrote:I tried to escape the / with a \
in $fcontents = file ($path\/$userfile);
but that does not seem to work.
You assigned "../files" to $path, then used that as the path to look through. Completely different location.jdmfontz wrote:I also tried double quotes ($path = "../files") with no success.
That page I linked you to explains why that doesn't work.jdmfontz wrote:Them tried to put the two variables defined as another variable:
$path = '../files/$userfile';
$fcontents = file ($path);
Re: Problem assigning path var to $fcontents = file ($userfile);
Thank you. After reviewing the reference a second time, it looks like this works:
$fcontents = file($path . '/' . $userfile);
M
$fcontents = file($path . '/' . $userfile);
M