Page 1 of 1
Creating a new directory...
Posted: Wed Jul 05, 2006 7:02 pm
by JellyFish
Hello, I keep getting this error whenever in my uploading image script.
Warning: move_uploaded_file(/home/yadayada/html/pictures/test/New): failed to open stream: Not a directory in /home/yadayada/html/do_submit.php on line 78
This is because, and I know it is, of the fact that I'm taking that image file and uploading it in a directory that doesn't exist (i.e. /test). So my big thing is how do I create a directory if one doesn't exist?
Thank you for reading and all help is appreciated.

Posted: Wed Jul 05, 2006 7:03 pm
by Benjamin
Posted: Wed Jul 05, 2006 7:21 pm
by JellyFish
Weirdan | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Yes thank you for such a simple, yet brilliant reply. I've found this function as well. But my question remains partly unanswered.
[quote]
So my big thing is how do I create a directory if one doesn't exist?
[/quote]
In other words how do I check if the directory doesn't exist? And if it doesn't, create the directory. But if it does, then do not create the directory.
Would if be something like:
Code: Select all
if (!mkdir(/my/dir/) {
//make the directory with the mkdir() function
} else {
//do not do anything. else is actually not necessary.
}
Weirdan | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Wed Jul 05, 2006 7:28 pm
by Weirdan
Posted: Wed Jul 05, 2006 7:58 pm
by Benjamin
As Weirdan said..
Code: Select all
<?php
$filename = '/path/to/dirname';
if ((file_exists($filename)) and (is_dir($filename))) {
echo "Yes, the path is correct and is a folder";
} else {
echo "Oops, something isn't correct";
}
?>
Posted: Wed Jul 05, 2006 8:09 pm
by JellyFish
Yes thank you astions and Weirdan. I placed:
Code: Select all
4: //creates the users directory if it does not exists already
5: if (!file_exists("/".$_SESSION[username]."/")) {
6: mkdir ("/".$_SESSION[username]);
7: }
in my script, and now I'm getting this error:
Warning: mkdir(/test): Permission denied in /home/yadayada/html/do_submit.php on line 6
I don't understand why I'm denied permission? Is the directory false?
Posted: Wed Jul 05, 2006 8:13 pm
by feyd
/test would be a folder in the root of the file system which you don't have permission to add data to. You may want to use $_SERVER['DOCUMENT_ROOT'] as apart of the path you supply to mkdir().
As an aside, always quote named array indices unless they are a constant:
should be
Posted: Wed Jul 05, 2006 8:32 pm
by JellyFish
Okay thanks feyd. Yeah it's amazing what a little reading can do, and I'm ignorant not to read before I post. Reading the comments on the mkdir() php.net page told me to scratch the "/". in my mkdir statement.
My script now looks like:
Code: Select all
//creates the users directory if it does not exists already
if ((!file_exists("eyecons/".$_SESSION[username])) && (!is_dir("eyecons/".$_SESSION[username]))) {
mkdir("eyecons/".$_SESSION[username]);
}
works great! Not that I know what you mean when you say
As an aside, always quote named array indices unless they are a constant:
using $_SESSION[username] apposed to $_SESSION['username'] works the same with me. So I ask, what would the benefit be in using the quotes?
I don't mean to get into debate. But just curious in the reason I should be doing this, so I'd kick myself in doing it more frequently.
Thank you everyone for your efforts. You made this a fantastic thread.
Posted: Wed Jul 05, 2006 8:40 pm
by feyd
Turn error_reporting() to E_ALL.. you'll find out. .. and just because you don't see those errors doesn't mean they aren't slowing php down. Errors in scripts always fire regardless of the error_reporting settings. PHP just happens to hide ones you wish to not see.
Posted: Wed Jul 05, 2006 8:43 pm
by JellyFish
Mmm. Nice thank you.
