sayedsohail Posted September 26, 2007 Share Posted September 26, 2007 Hi everyone, I just saw an interesting thread, for uploading files - but my problem is just to add a directory for each user, the user name are stored in session variable sess_uname? I am not sure how do i check if the directory is already exists for the logged user. if directory exists than i i just want to move the file to that directory otherwise create directory with the name as sess_uname and store the file. Can anyone please help in this regard. Thanks <?php //This is the directory where images will be saved $path = '../images/'; //This gets all the other information from the form $name=$_POST['name']; $suburb=$_POST['suburb']; $price=$_POST['price']; $content=$_POST['content']; $uploadFile0=($_FILES['uploadFile0']['name']); $uploadFile1=($_FILES['uploadFile1']['name']); $uploadFile2=($_FILES['uploadFile2']['name']); $uploadFile3=($_FILES['uploadFile3']['name']); $uploadFile4=($_FILES['uploadFile4']['name']); $uploadFile5=($_FILES['uploadFile5']['name']); $uploadFile6=($_FILES['uploadFile6']['name']); $uploadFile7=($_FILES['uploadFile7']['name']); $uploadFile8=($_FILES['uploadFile8']['name']); // Connects to your Database mysql_connect("localhost", "root", "10000") or die(mysql_error()) ; mysql_select_db("docs") or die(mysql_error()) ; // Check the extension is valid if (preg_match('/\.(jpg|jpeg|gif|png)$/i', $_FILES['uploadFile'. $x]['name'])){ //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$uploadFile0', '$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; $uploadNeed = $_POST['uploadNeed']; // start for loop for($x=0;$x<$uploadNeed;$x++){ $file_name = $_FILES['uploadFile'. $x]['name']; // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); } else { // Add your error code here echo "Sorry, file must be of type .jpg .jpeg .gif or .png\n"; } // check if successfully copied if($copy){ print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; } else{ echo "$file_name | could not be uploaded!<br>"; } } // end of loop ?> Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/ Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 Hi everyone, I just saw an interesting thread, for uploading files - but my problem is just to add a directory for each user, the user name are stored in session variable sess_uname? I am not sure how do i check if the directory is already exists for the logged user. if directory exists than i i just want to move the file to that directory otherwise create directory with the name as sess_uname and store the file. Check the manual for the following functions: file_exists() move_uploaded_file() mkdir() These should be the main ones you'll need. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355634 Share on other sites More sharing options...
sayedsohail Posted September 26, 2007 Author Share Posted September 26, 2007 is there anything such as directory exists?, can you please help with a little bit of code. Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355636 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 You can use file_exists() for directories, that's why I suggested it. Look at the link I provided. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355643 Share on other sites More sharing options...
sayedsohail Posted September 26, 2007 Author Share Posted September 26, 2007 This is simple code, but its not working? no errors, although i removed the session in my second try. ? <?php //session_start(); //$uname = $_SESSION['sess_uname']; $uname = "tmp1"; $path= 'pdf/'.$uname.'/' $filename = '/'.$path.'/doc1.doc'; if (file_exists($path)) { echo " The Directory $path exists"; } else { echo "The directory $path does not exist"; } if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355651 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 OK, and what's the output you're getting? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355660 Share on other sites More sharing options...
sayedsohail Posted September 26, 2007 Author Share Posted September 26, 2007 nothing just blank page. Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355681 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 You're missing a semi-colon off of the path... Try this: <?php $uname = "tmp1"; $path = 'pdf/'.$uname.'/'; $filename = '/' . $path . 'doc1.doc'; if (file_exists($path)) { echo " The Directory $path exists"; } else { echo "The directory $path does not exist"; } if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> This will noly work from your document root though as you're putting a forward slash in front of the path. Is that what you want? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355688 Share on other sites More sharing options...
sayedsohail Posted September 26, 2007 Author Share Posted September 26, 2007 I slightly changed the first three lines and it worked? $uname = "tmp1"; $path = 'pdf/'.$uname.'/'; $filename = "$path" ."doc1.doc"; Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355699 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 I slightly changed the first three lines and it worked? Why the question mark. Did it work? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355702 Share on other sites More sharing options...
sayedsohail Posted September 26, 2007 Author Share Posted September 26, 2007 yes i am trying to add this code now, not sure this is the right way. $mode = 0777; if (!is_dir($path) && !mkdir($path, $mode)) {mkdir( "$uname" , 0777); } Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355730 Share on other sites More sharing options...
sayedsohail Posted September 26, 2007 Author Share Posted September 26, 2007 I was told using umask(0), is better before making any directories or writting files, can someone please explain little bit about umask(0), please. Thanks, $oldumask = umask(0); $mode = 0777; if (!is_dir($path) && !mkdir($path, $mode)) {mkdir( "$uname" , 0777); } umask($oldumask); Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-355859 Share on other sites More sharing options...
HuggieBear Posted September 27, 2007 Share Posted September 27, 2007 Before we go any further with permissions, is your server running Windows or Unix/Linux flavoured box? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-356236 Share on other sites More sharing options...
sayedsohail Posted September 27, 2007 Author Share Posted September 27, 2007 windows 2003? Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-356322 Share on other sites More sharing options...
HuggieBear Posted September 27, 2007 Share Posted September 27, 2007 In that case you can pretty much ignore all the permissions stuff I believe as Windows doesn't implement it properly. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-356326 Share on other sites More sharing options...
sayedsohail Posted September 27, 2007 Author Share Posted September 27, 2007 million thanks, The directory i am creating is basically the name of the client i.e, ABC Ltd or XYZBCDD Co. or Cat's Co., here i wish to abstract only the alphabets and only the first bit i.e., ABC and XYZBCDD and Cats replacing the other characters exists inside the first bit as you notice Cats in place of Cat's Co. I am not sure if str_replace could do all that work. $file_name=$_GET['client_name']; $file_name = str_replace("'","",$file_name); or substr($file_name,strpos($file_name,$pattern)+strlen($pattern)); or $str = preg_replace('/.'*?/', $file_name); Can you please suggest something here. Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-356332 Share on other sites More sharing options...
MadTechie Posted September 27, 2007 Share Posted September 27, 2007 basic filename filter $file_name = preg_replace('/[^a-z0-9]/si', '', $file_name); Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-356361 Share on other sites More sharing options...
sayedsohail Posted September 27, 2007 Author Share Posted September 27, 2007 Thanks madtechie, can you please explain how it works, please. specially the /si bit is something new for me. and how do i take only the first occurance of letters from the full company ie., cat's co ltd to $file_name='cats'; $file_name="Cat's Company Ltd."; tosomethign like this using preg_replace or preg_split, i am not sure could i achieve this. $file_name ="cats"; Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-356370 Share on other sites More sharing options...
sayedsohail Posted September 28, 2007 Author Share Posted September 28, 2007 ??? Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-357315 Share on other sites More sharing options...
MadTechie Posted September 28, 2007 Share Posted September 28, 2007 try this **untested** <?php $file_name="Cat's Company Ltd."; //replaces everything except a-z and 0-9 and spaces with nothing (the /i is case insensitive) $file_name = preg_replace('/([^a-z0-9 ])/i', '', $file_name); //So $file_name is "Cats Company Ltd" //Now get the first word if (preg_match('/(\w+)/si', $file_name, $regs)) { $file_name = $regs[1]; } echo $file_name; //should be Cats ?> Quote Link to comment https://forums.phpfreaks.com/topic/70739-solved-upload-file-and-create-directory-for-each-user/#findComment-357365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.