placing variables in filepath

A

Anonymous

Guest
hi, im just wondering if anyone nows how to place variables in file paths?
im using the following code:

$upfile = 'c:\PHP\uploadtemp\{$login}'.$userfile_name;

the variable $upfile is the path of where the file is uploading to, the problem i have is the file is uploading to the uploadtemp directory but with the name {$login}and what evers in $userfile_name, I want to be able to to show the conntents of the $login variable, if any ones got any suggestions or can spot a mistake i would be gratefull cheerz...
 
Variables are only evaluated in double-quoted strings and heredocs. In this case, string concatenation is a better choice:
Code:
<?php
$upfile = 'c:\PHP\uploadtemp\' . $login . $userfile_name;
?>
 
i tried that i seem to get this error,

Parse error: parse error, unexpected T_STRING in c:\program files\apache group\apache\htdocs\system\upload.php on line 54
 
i just tried that idea too, but it gives me a new error message displaying;

Parse error: parse error, unexpected T_VARIABLE
 
Oh, right, I forgot about the slashes. Can you post the code you're using now?
 
here's all the code;

<?php
session_start();
if ( @$_SESSION['login'] != "YES")
{
session_destroy();
exit();
}
?>

<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
</body>
</html>

<?php

$login = $_SESSION['user'];

// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];

// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];

// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];

// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];

// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions

if ($userfile_error > 0)
{
echo 'Problem: ';
switch ($userfile_error)
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}



//$upfile = 'c:\\PHP\\uploadtemp\\'.$login.$userfile_name;

// put the file where we'd like it
$upfile='c:\PHP\uploadtemp\$login'.$userfile_name;

// is_uploaded_file and move_uploaded_file added at version 4.0.3
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
exit;
}
echo 'journal uploaded successfully<br /><br />';

//need a href to go back to main screen correct
?>
 
sorted the problem had to use double slashes like Alexei Kubarev said,
i previously didnt put them all the way through thats way i got the error,
had to use forward slashes all the way through and it works fine saving to the required directory;

$upfile="c:\\PHP\\uploadtemp\\$login\\$userfile_name";

:-?
thanks for the help
cheerz
ass12
 
Hi

Just a quick note. You should get out of the habit of using 'back slashes [\]' and always use 'forward slashes [/]' instead! It does not matter what system you are on, forward slashes will always work for any type of scripting language including PHP. This way you never have to guess if you need to double or triple escape because the next or proceeding character!

example

Code:
$path = 'c:\\path\\\' . $var;

the cleaner way...

$path = 'c:/path/' . $var;


jbr
 
Back
Top