Question:
What's wrong with this PHP?
Alster
2009-05-05 14:54:59 UTC
mkdir("/uploads/".$_POST['username'].) or die ("Failed");

I keep getting; Parse error: syntax error, unexpected ')'

I always forget how to include variables into file paths...
Five answers:
spiller
2009-05-05 15:39:16 UTC
"/uploads/" is relative to the root of the filesystem, either the absolute root in Unix-like operating systems or the drive of the web server or interpreter in Windows. Are you sure that's what you want?



If you want it to be relative to the HTTP root you'll need to throw $_SERVER['DOCUMENT_ROOT'] into that mix too. I.e.,



mkdir($_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $username) or die('Failed');



The actual reason for the error is that the uploads directory could not be found/did not exist. Mkdir is not recursive, and will not create the entire directory tree for you. So simply create the uploads directory first.



But for goodness sake, VALIDATE THE USERNAME. If the rest of your code is like that, you're open for some horrible security attacks.
Sleeping Troll
2009-05-05 18:10:09 UTC
Avoid the "." for concatenation whenever possible, this should work just as well: mkdir("/uploads/$_POST['username']") or die ("Failed");

PHP is very good about parsing vars when inside double quotes.
anonymous
2009-05-05 15:02:18 UTC
mkdir("/uploads/".$_POST['username'].)



That dot after username'] means that another string is coming next.



mkdir("/uploads/".$_POST['username'].'1') would be valid (if you wantred to stick a 1 after the username.



BTW, if the user is entering the user name, get $_POST['username'] into a variable first, and check it for validity.
anonymous
2009-05-05 15:33:15 UTC
mkdir("/uploads/".$_POST['username'].) or die ("Failed"); is wrong



if you are concatenating something you need spaces and you need to specify a mode. Not that if you are working on a windows box with PHP etc. then it is wise to use the octal values.

0777



mkdir("/uploads/" . $_POST['username'] . ,0777)

or die ("Failed");



or try



$path_to_create = "/uploads/" . $_POST['username'];



mkdir($path_to_create,0777)

or die ("Failed");
topherG
2009-05-05 15:20:41 UTC
My guess is that the path to your uploads directory is incorrect.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...