EchoFool Posted June 14, 2009 Share Posted June 14, 2009 I have an image uploader script for users to upload but am wanting to divide the folders into subfolders based on user id, here is an example: Say User ID 2 wanted to upload a image and there is no subfolder named "2" it will create it then place the image in: images/2/image1.jpg But if subfolder 2 is already in existance it will just add the image straight to the folder instead... how is this done? I have so far got this for my image upload script: <?php if(isset($_POST['action'])){ //define a maxim size for the uploaded images in Kb define ("MAX_SIZE","150"); [insert current track Signatune] + Additional Options... //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; //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, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message ?> <font size="2" color="red"><strong> Unknown extension for image! Can only have: jpg,jpeg,png or gif format! </strong></font> <?php $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) { ?> <font size="2" color="red"><strong> You have exceeded the size limit, perhaps find a photoshop expert to help lower the size limit for you on the forums unless you can do it yourself! </strong></font> <?php $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/userprofiles/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { ?> <font size="2" color="red"><strong> Copy unsuccessfull! Please try again! </strong></font> <?php $errors=1; } } } //If no errors registred, print the success message if($errors == 0) { $Update = mysql_query("INSERT INTO userimages (UserID,Image) VALUES('{$row['GameID']}','$image_name')") Or die(mysql_error()); If(mysql_affected_rows()>0){ ?> Upload successfully complete! <?php } } } ?> Is it possible to do my idea, if so how do you do it ? Hope you can help ! Link to comment https://forums.phpfreaks.com/topic/162156-solved-folder-creation-for-image-assortment/ Share on other sites More sharing options...
jxrd Posted June 14, 2009 Share Posted June 14, 2009 You'll probably want file_exists and mkdir Link to comment https://forums.phpfreaks.com/topic/162156-solved-folder-creation-for-image-assortment/#findComment-855733 Share on other sites More sharing options...
EchoFool Posted June 14, 2009 Author Share Posted June 14, 2009 Is there a way to do if folder exists also ? Link to comment https://forums.phpfreaks.com/topic/162156-solved-folder-creation-for-image-assortment/#findComment-855771 Share on other sites More sharing options...
jxrd Posted June 14, 2009 Share Posted June 14, 2009 file_exists() also checks dirs. Read the notes!! Link to comment https://forums.phpfreaks.com/topic/162156-solved-folder-creation-for-image-assortment/#findComment-855775 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.