Jump to content

[SOLVED] Creating a testing to see if a folder exists.......


TGWSE_GY

Recommended Posts

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 8)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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')) {

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.