Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mkdir no such file or directory

13 views
Skip to first unread message

Bhushan N.N

unread,
May 14, 2013, 11:04:27 PM5/14/13
to
I want to create a directory using mkdir. But I get "No such file or directory" error.

Below is the code I am using.

$filelocation = "/uploads/" . "/" . time();

Can some one please tell me what I am doing wrong?

I also tried manually creating the uploads folder. Still the same error. Any help will be much appreciated.

Bhushan N.N

unread,
May 14, 2013, 11:06:19 PM5/14/13
to
Here is my code again

$filelocation = "/uploads/" . "/" . time();
if (mkdir($filelocation,0777,true))
{
echo("Thank you for uploading the file.");
}
else
{
echo("Oops error uploading file.");

J.O. Aho

unread,
May 15, 2013, 12:34:49 AM5/15/13
to
On 15/05/13 05:06, Bhushan N.N wrote:

NOTE: Don't top post, reply inline. If not replying to the whole post,
then remove those parts you don't reply to.


>> I want to create a directory using mkdir. But I get "No such file or directory" error.

mkdir() will only give true or false depending on if it can create the
directory or not.


>> Below is the code I am using.
>> $filelocation = "/uploads/" . "/" . time();

Usually the user as who the process is run as (web server user or a
normal user) will not have the right to make a directory in the root
file system (path: /), but we assume you have full control of the web
server and can create the directory /uploads and have already made it,
then you need to allow the web server/normal user as whom the php script
is run as to have rwx on that directory.


>> Can some one please tell me what I am doing wrong?
>> I also tried manually creating the uploads folder. Still the same error. Any help will be much appreciated.
>
> Here is my code again
>
> $filelocation = "/uploads/" . "/" . time();
> if (mkdir($filelocation,0777,true))
> {
> echo("Thank you for uploading the file.");
> }
> else
> {
> echo("Oops error uploading file.");
> }

$path = "/uploads";
$dir = time();

if(file_exists($path)) {
if(file_exists($path."/".$dir)) {
echo "directory {$path}/{$dir} already exists";
} else {
if(mkdir($path."/".$dir,0777,true)) {
echo "Created {$path}/{$dir}";
} else {
echo "Can't create {$path}/{$dir}";
}
}
} else {
echo "{$path} is missing";
}

This is a simple check with a bad way of echoing out results, this don't
check if the file really is a directory, is_dir() will tell you that if
you want to make the check more complete, I suggest you look at
Exceptions instead of just echo everything.

--

//Aho

Richard Yates

unread,
May 15, 2013, 12:37:28 AM5/15/13
to
I don't know anuything about mkdir, but isn't:

"/uploads/" . "/" . time() the same as:

"/uploads//".time();

Are those two forward slashes causing the error?

Martin Leese

unread,
May 15, 2013, 12:39:44 AM5/15/13
to
Bhushan N.N wrote:
> Here is my code again
>
> $filelocation = "/uploads/" . "/" . time();
> if (mkdir($filelocation,0777,true))
> {
> echo("Thank you for uploading the file.");
> }
> else
> {
> echo("Oops error uploading file.");
> }

When I modified your error line to:
echo("Oops, error creating directory \"$filelocation\".");

I got the output:
Oops, error creating directory "/uploads//1368592056".

Note the double "/" in the directory name.

Also, check you have permission to create a
directory in your root directory.

--
Regards,
Martin Leese
E-mail: ple...@see.Web.for.e-mail.INVALID
Web: http://members.tripod.com/martin_leese/

Arno Welzel

unread,
May 15, 2013, 6:05:33 AM5/15/13
to
Am 15.05.2013 05:06, schrieb Bhushan N.N:

> Here is my code again
>
> $filelocation = "/uploads/" . "/" . time();
> if (mkdir($filelocation,0777,true))
> {
> echo("Thank you for uploading the file.");
> }
> else
> {
> echo("Oops error uploading file.");
> }

1) Look carefully: "/uploads/" . "/" will result in "uploads//"

2) Does /uploads exist? It seems you miss the whole path the the root
directory of the webserver (/var/www/....) or you did not understand the
difference between the root URL in your browser and the physical path on
the machine where the script runs.

I assume you need a full path and not just "/uploads" and you have to
make sure, that the user which is used to execute PHP has the right
access rights within the given path.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de

The Natural Philosopher

unread,
May 15, 2013, 6:23:23 AM5/15/13
to
On 15/05/13 04:06, Bhushan N.N wrote:
> Here is my code again
>
> $filelocation = "/uploads/" . "/" . time();

that looks to me like "/uploads//12345678901234567890"

which probably isn't a reason for failure with the double slash, but is
ugly.

However, do you have permission to create a directory in the root file
system of your computer?

Try removing the leading slash to its relative to wherever PHP is
operating, or better still use the PHP defined constant that tells you
where the root file system is, as a prefix.



--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

Denis McMahon

unread,
May 15, 2013, 8:38:27 AM5/15/13
to
On Tue, 14 May 2013 20:06:19 -0700, Bhushan N.N wrote:

> Here is my code again
>
> $filelocation = "/uploads/" . "/" . time();
> if (mkdir($filelocation,0777,true))
> {
> echo("Thank you for uploading the file.");
> }
> else {
> echo("Oops error uploading file.");
> }

Does /uploads exist on your system?
If /uploads does exist, has the webserver process got sufficient access
to create subdirs in it?
If /uploads does not exist, has the webserver process got sufficient
access to create it?

Given that creating anything at / usually requires root level access, and
it's generally not recommended to run web servers as root, I suspect
access rights might be your problem.

--
Denis McMahon, denismf...@gmail.com

Robert Heller

unread,
May 15, 2013, 9:51:39 AM5/15/13
to
And: maybe the OP *really* means something like:

$filelocation = dirname(__FILE__) . "/uploads/" . "/" . time();

That is, he might really want uploads to be *relative* to files on the
website, rather than a absolute place on the server's file system. (The
'dirname(__FILE__)' is just an example, there might be other possibilities.)

>

--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments



Bhushan N.N

unread,
May 15, 2013, 11:19:02 AM5/15/13
to
Hi Robert,

I want the uploads directory where I am hosting the website files.

Bhushan N.N

unread,
May 15, 2013, 11:20:45 AM5/15/13
to
ie, if my website is called example, the structure will be

example
index.php
upload.php
search.php
uploads
12209214
somefile.txt
31231203
otherfile.txt
about.php
contact.php

Thomas 'PointedEars' Lahn

unread,
May 15, 2013, 12:37:15 PM5/15/13
to
Please learn how to post properly on Usenet. Avoid Google Groups in favor
of a locally installed newsreader application if possible.

The problem here is that you are falsely assuming that “/” refers to the
document root. Instead, it refers to the *filesystem* root, where you/the
Web server SHOULD NOT have write access. As “uploads” is a directory on the
same level as upload.php, it is probably easiest for you just to omit the
leading “/”.

However, it would be safest to refer to the document root explicitly, like

$_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $value

(As you can see, it does not make sense to concatenate '/uploads' . '/' when
you can just write '/uploads/'.)

It also does not make a lot of sense to create a directory for every second
of upload since epoch, using time(). If you need to group uploads by date
for quick access, I suggest you use year/month/date instead. That said, you
can rather easily filter an array of file information, so unless there is a
chance that on different times people would upload files with the *same*
name where they do *not* want to overwrite the old version, you do not need
that directory structure.

However, in a multi-user application another piece is missing in any case:
You need a way to differentiate between the files of users or one user could
overwrite the file of another. Using time() _cannot_ prevent that from
happening, because there is the possibility of concurrent access.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Robert Heller

unread,
May 15, 2013, 2:44:35 PM5/15/13
to
That means you don't want "/uploads"! If the mkdir code is in upload.php,
then you really do want:

$directory = dirname(__FILE__) . "/uploads/" . time();

Thomas 'PointedEars' Lahn

unread,
May 15, 2013, 3:31:05 PM5/15/13
to
Robert Heller wrote:

> "Bhushan N.N" wrote:
>> ie, if my website is called example, the structure will be
>>
>> example
>> index.php
>> upload.php
>> search.php
>> uploads
>> 12209214
>> somefile.txt
>> 31231203
>> otherfile.txt
>> about.php
>> contact.php
>
> That means you don't want "/uploads"! If the mkdir code is in upload.php,
> then you really do want:
>
> $directory = dirname(__FILE__) . "/uploads/" . time();

From PHP 5.3 on they may want to use

$directory = __DIR__ . "/uploads/" . time();

instead. However, see my other answer as to why either approach is not a
good idea.

Bhushan N.N

unread,
May 15, 2013, 7:20:56 PM5/15/13
to
Thomas,

Thanks for your feedback.

I just put that uploads/time format as an example. In the real application I will be using

uploads/<username>/time format

Please learn how to post properly? Don't really know what you mean. Thanks a lot anyway for your help. Appreciate it.

Thank you
Bhushan

Denis McMahon

unread,
May 15, 2013, 7:55:59 PM5/15/13
to

> Please learn how to post properly? Don't really know what you mean.
> Thanks a lot anyway for your help. Appreciate it.

He means that your reply should go *DOWN HERE*, _underneath the quoted
text_

0 new messages