0

Like the question states, I would like to create a new directory in the "Users" folder on the server when a user registers their account.

PHP:

$username = $_POST['username'];//STRING
$username = strip_tags($_POST['username']);
$username = stripslashes($_POST['username']);
$username = mysqli_real_escape_string($conn,$_POST['username']);

etc.
etc.

$sql = "INSERT INTO tbl_users 
VALUES ('$username', '$password',    '$countryCode','$mobileNumber','$email','$profilePicture','$fname','$mname','$lname','$gender','$birthday','$occupation','$signupdate',0)";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully in tbl_users";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

I'm quite new to php and I don't really understand how most of the commands work.

I am making an app in java and I want the onClick to add data to a database as well as create the following file hierarchy:

Users(Already there)

  • Username(Of user registering)

    • Media

      • Pictures

      • Videos

      • Audio

Brian
  • 105
  • 2
  • 10
  • 1
    it's not right to create a folder for every user, are you sure you really have to do this? what's your goal? – F.E.A Aug 17 '16 at 19:14
  • Isn't mkdir() your friend? – greenapps Aug 17 '16 at 19:14
  • 2
    You are vulnerable to [sql injection attacks](http://bobby-tables.com), and your first three lines of code are utterly useless, because the 4th one simply trashes/ignores everything you did previously. And if you want to create a directory, then http://php.net/mkdir. Plus `query()` calls don't EVER return `true`. they return a statement handle or object, or a boolean false on failure. `true` is not something you can EVER test for equality on in this situation. your code will ALWAYS report a failure because of this – Marc B Aug 17 '16 at 19:14
  • 3
    It's a common misconception for people new to PHP that a path that looks like this `example.com/users/Henders/media` actually exists. Just because the URL looks like that it doesn't mean that folder exists. My first port of call would be to look up something about 'URL rewriting in PHP' and you'll see what I'm talking about. – Henders Aug 17 '16 at 19:16
  • `I'm quite new to php and I don't really understand how most of the commands work.` Sorry if this sounds harsh, or unfriendly, but believe me, this is a friendly advice: time for a lot of learning. Kludging code `!=` development work. Also: by creating a folder structure, what exactly do you strive to achieve? Having a folder for each user is a **deadly** pattern... The file system is not meant to have that kind of structure. Imagine: 1 million users -> 1 million subfolders in a folder? There are file systems that chuckle even by thinking about this... (Also, how does this relate to Java?) – ppeterka Aug 17 '16 at 19:20
  • Well I am trying to build a social network, java is my strong point. I am just trying to edit the php code that the database admin made in order to save profile pictures and media that the users have uploaded – Brian Aug 17 '16 at 19:25
  • You should not trust that DB admin guy then for PHP development. This is the kind of code we see from complete newbies. – ppeterka Aug 17 '16 at 19:27
  • How would you suggest that I approach this then? I need users to have a profile picture and other media, how would be the best way to say it on the server? – Brian Aug 17 '16 at 19:28
  • For anything that has social in the name, plan for scaling... Hiccups, downtimes, slowness scares away the community... I'm not an expert in this, but to store static content, I'd have the files on disk in a 2 levels deep structure: `/level1/level2/` (just put it to shared storage with the different levels later, as a primitive "sharding"), all elements having a kind of ID as the name, with max. 10000 files in each `level2` folder. This would be the key for the files: `level1ID`, `level2ID` and `fileID`. The metadata for the files would be stored in the DB. – ppeterka Aug 17 '16 at 19:43
  • Wow, I just found [this answer](http://stackoverflow.com/a/8922090/1667004) saying almost exactly what I just wrote... It sure feels good to find out that I think the same as some of those people who'd seen such stuff before. – ppeterka Aug 17 '16 at 19:49
  • @ppeterka is there any way that i could contact you off the site somehow? you seem very insightful and it would really help me a lot. – Brian Aug 17 '16 at 19:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121205/discussion-between-brian-and-ppeterka). – Brian Aug 17 '16 at 20:00

1 Answers1

0

To add a directory in "Users" folder,You can add this code:

mkdir("Users/new_folder",0755);
ppeterka
  • 20,583
  • 6
  • 63
  • 78