making a directory on submit and then upload to it

Ask about a PHP problem here.
Post Reply
damo1995
Posts: 1
Joined: Tue Nov 01, 2011 2:34 am

making a directory on submit and then upload to it

Post by damo1995 »

Hello all first time on the forums.

i've been folowing this tut http://www.youtube.com/watch?v=7iQWR76b ... ideo_title

i have managed to get this to work ok but now want to modify it a little, i now want to add in the abbility for a person to choose a directory to upload into and if it dosent already exsist it creates one.

Here is the current script i have tried and failed
<?php
if (isset($submit)) {

mkdir("bans"/$_POST['dir'], 0777);

}

if (isset($_FILES['files'])){
	foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
		move_uploaded_file($tmp_name, "bans/{$_FILES['files']['name'][$key]}");	
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head>

<body>

	<div>
    	<form action="" method="post" enctype="multipart/form-data">
        <p>
          <label for="dir">Player Name:</label>
          <input type="text" name="dir" id="dir" />
          </p>
          <p>
          <input type="file" name="files[]" multiple="multiple" min="1" max="9999" />
          <input type="submit" value="Upload" />
        </p>
        </form>
</div>
</body>
</html>
i know i havent added in the part for the img to be moved but its not creating the dir as it should do.

sorry i am a noob at PHP this is the first real thing i've tried doing with it

Thanks in advanced
Damo
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: making a directory on submit and then upload to it

Post by jacek »

Well the problem here is that when you check for $submit being set you will never get true, since it is never set as that is the first line in the script.

You probably want
if (isset($submit)) {
 
mkdir("bans"/$_POST['dir'], 0777);
 
}
to be
if (isset($_POST['submit'])) {
    mkdir("bans"/$_POST['dir'], 0777);
}
or something like that.

Also, 0777 is wrong for the permissions of the folder, this would give any user on the system access to delete the folder, you want 0700 or at most 0740.
Image
Post Reply