TGWSE_GY Posted April 24, 2009 Share Posted April 24, 2009 Hi Guys, What I am attempting to do is test if a folder exists, I thought the following code would work but it doesn't create the directory based off the usersname. <?php session_start(); if (isset($_SESSION['Usr'])) { $UploadedFile = $_POST['formUploadFile']; $User = $_SESSION['Usr']; echo $User; die(); //Testing if there is a file in perm_images = to the name in $User if (file_exists('perm_images/$User')) { //If TRUE // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //Since the IF failed we are now going to create the dir in perm_images = to the name in $User mkdir("perm_images/$User"); if (file_exists('perm_images/$User')) { // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } } } else { echo ("Error"); //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } if (move_uploaded_file($_FILES['formUploadFile']['tmp_name'], $target_path)) { echo "The file". basename($_FILES['formUploadFile']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?> Any ideas guys, thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/ Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 Does "perm_images" have write permissions (0775 or 0777) if not, you will have to be able to write to that directory to create one in it. Also do you have display_errors turned on and error_reporting set to E_ALL, if not turn it on as that will give you an error which could tell you why it is not working/creating the directory. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818254 Share on other sites More sharing options...
TGWSE_GY Posted April 24, 2009 Author Share Posted April 24, 2009 Okay this is the error that I get Warning: mkdir() [function.mkdir]: File exists in /home/.deceasing/thegayestever/thegayestcommunityever.com/dev/php/includes/content/members_home/user_images/upload.php on line 18 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpWnldIa' to '' in /home/.deceasing/thegayestever/thegayestcommunityever.com/dev/php/includes/content/members_home/user_images/upload.php on line 37 It is as if it is failing on the first if and going to the else, which doesnt make sense cause the file exists so it should continue on. Here is my revised code <?php session_start(); if (isset($_SESSION['Usr'])) { $UploadedFile = $_POST['formUploadFile']; $User = $_SESSION['Usr']; //Testing if there is a file in perm_images = to the name in $User if (file_exists('perm_images/$User')) { //If TRUE // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //Since the IF failed we are now going to create the dir in perm_images = to the name in $User mkdir("perm_images/$User"); if (file_exists('perm_images/$User')) { // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } } } else { echo ("Error"); //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } if (move_uploaded_file($_FILES['formUploadFile']['tmp_name'], $target_path)) { echo "The file". basename($_FILES['formUploadFile']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?> Thanks Guys Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818287 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 Change the first if to be is_dir Since you are checking DIR and not for a FILE, that would be the right one to use for that if. //Testing if there is a file in perm_images = to the name in $User if (is_dir('perm_images/$User')) { Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818290 Share on other sites More sharing options...
TGWSE_GY Posted April 24, 2009 Author Share Posted April 24, 2009 Okay so now I am getting this error: Warning: mkdir() [function.mkdir]: File exists in /home/.deceasing/thegayestever/thegayestcommunityever.com/dev/php/includes/content/members_home/user_images/upload.php on line 18 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpjf1bRL' to '' in /home/.deceasing/thegayestever/thegayestcommunityever.com/dev/php/includes/content/members_home/user_images/upload.php on line 37 <?php session_start(); if (isset($_SESSION['Usr'])) { $UploadedFile = $_POST['formUploadFile']; $User = $_SESSION['Usr']; //Testing if there is a file in perm_images = to the name in $User if (is_dir('perm_images/$User')) { //If TRUE // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //Since the IF failed we are now going to create the dir in perm_images = to the name in $User mkdir("perm_images/$User"); <------ This is line 18 if (is_dir('perm_images/$User')) { // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } } } else { echo ("Error"); //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } if (move_uploaded_file($_FILES['formUploadFile']['tmp_name'], $target_path)) { <------ This is line 37 echo "The file". basename($_FILES['formUploadFile']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?> Thanks again guys. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818343 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 Print out $User, I have a vague feeling that it is not being populated. As inside the 2nd is_dir if that is going to the else since when you are moving the file $target_path contains an empty value. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818350 Share on other sites More sharing options...
TGWSE_GY Posted April 24, 2009 Author Share Posted April 24, 2009 I have echoed $User and $Target and it is holding the values fine. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818352 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 Do you have a file in that directory that is just the username? If you put this in: if (is_dir('perm_images/$User')) { // Where the file is going o be placed $target_path = "perm_images/$User"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { die("The is_dir returned false on perm_images/{$User} again, so it is not a DIR...but mkdir will not make the dir."); //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } My bet is that die statement echo's out. It would seem to me either a file is there with the $User as the name, or something else is going on, what I do not know. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818359 Share on other sites More sharing options...
TGWSE_GY Posted April 24, 2009 Author Share Posted April 24, 2009 Premiso you are correct, it immediately went to die, The is_dir returned false on perm_images/thegayestever again, so it is not a DIR...but mkdir will not make the dir. However the directory does appear on my server. Any ideas. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818367 Share on other sites More sharing options...
premiso Posted April 24, 2009 Share Posted April 24, 2009 Maybe use the full absolute path instead of just that. You can try using $_SERVER['DOCUMENT_ROOT'] to get that and see if that helps you make the directory or give an accurate test if that directory exists. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818377 Share on other sites More sharing options...
TGWSE_GY Posted April 24, 2009 Author Share Posted April 24, 2009 Nope Premiso its giving me the same error, I'm stumped I didn't think this would be so hard :-\ Thanks for the help though any more thoughts guys. Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818440 Share on other sites More sharing options...
TGWSE_GY Posted April 24, 2009 Author Share Posted April 24, 2009 Okay guys, I found a fix for it. I am not completely sure how its fixed other than, I set the path in a variable and rather than doing an is_dir I did a file_exists. Here is the code so others can see how I got it to work. <?php session_start(); if (isset($_SESSION['Usr'])) { $UploadedFile = $_POST['formUploadFile']; $User = $_SESSION['Usr']; $filename = "perm_images/" . "$User" . "/"; //Testing if there is a file in perm_images = to the name in $User if (file_exists($filename)) { //If TRUE // Where the file is going o be placed $target_path = "perm_images/$User/"; echo "1"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { //Since the IF failed we are now going to create the dir in perm_images = to the name in $User mkdir("perm_images/$User/", 0777); if (file_exists($filename)) { // Where the file is going o be placed $target_path = "perm_images/$User/"; // Add the original filename to our target path Result is "perm_images/filenae.extension" $target_path = $target_path . basename($_FILES['formUploadFile']['name']); } else { echo "ERROR 3"; //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } } } else { echo ("Error"); //header ("Location: http://www.thegayestcommunityever.com/dev/index.php?section=denied"); } if (move_uploaded_file($_FILES['formUploadFile']['tmp_name'], $target_path)) { echo "The file". basename($_FILES['formUploadFile']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155504-solved-creating-a-testing-to-see-if-a-folder-exists/#findComment-818540 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.