create sub folders

Hi I found this script and it allows me to upload a picture to the server and stores it in images.

Is it possible to have it create a new subfolder for each user that uploads pictures? I’m extremely new to PHP and just found this script. Here is the code

<?php //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","100"); //This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '

Unknown extension!

'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '

You have exceeded the size limit!

'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=time().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '

Copy unsuccessfull!

'; $errors=1; }}}} //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "

File Uploaded Successfully! Try again!

"; } ?>

yes it is possible!here is a sample code from a job I am currently working on:
[php]
$id=mysql_insert_id();
if(!is_dir("…/…/uploads/".$id."/before")){
mkdir("…/uploads/".$id."/before", 0777, true);
chmod("…/uploads/".$id."/before", 0777);
mkdir("…/uploads/".$id."/during", 0777, true);
chmod("…/uploads/".$id."/during", 0777);
mkdir("…/uploads/".$id."/after", 0777, true);
chmod("…/uploads/".$id."/after", 0777);
}
echo"Job has been emailed & started successfully";
}
[/php]first create a uploads folder then you could use this script (altered to fit of course) and mysql_insert_id() just after you insert the users info on registration to your site. then use mkdir() to create the directory, then use chmod to set permissions on the directory so that you can upload images into it later on. now there will be a folder for every user you have on the site. That is one way of course, but I think you should only use that if you plan on not having a ton of people on your site, because it might start getting ridiculous if you have say 10,000 people sign up then you will have 10,000 folders in your uploads section. another idea would be to create a database and when a user inserts images store the location of the image in a table associated with that user so you could make a table call it “userimages” then give it a few fields(id, user, image) Now everytime an image is uploaded save the image location, and the users id in the database. so when you want to call for it all you would have to do is:
$userid=$_SESSION[‘id’];
$foo=mysql_query(“select * from userimages where user=$userid”);
while($link=mysql_fetch_assoc($foo)){
echo"<img src={$link[‘image’]}
";
}
Just an idea :wink:

Tks Plintu

I like this idea but I have a problem. I have a register script that works. Then they can login which is good but for the bottom I’m not sure how I to bring the id to the script so it knows who is uploading the images. Also with the below suggestion it wouldn’t matter if 2 users had the same name for an image?

idea would be to create a database and when a user inserts images store the location of the image in a table associated with that user so you could make a table call it “userimages” then give it a few fields(id, user, image) Now everytime an image is uploaded save the image location, and the users id in

Sponsor our Newsletter | Privacy Policy | Terms of Service