Dummy Code
Dummy Code

Reputation: 1898

PHP Script mkdir "No error, but no directory"

I am currently learning and messing around with some HTML and PHP on my personal website. I need to create a "New Directory" using PHP.

I am currently using, http://dummycode.com/projects/projectform/form.html in order to try and make this happen. It is running the PHP script but it is not creating a new directory under /projects/projectform/dir

Here is the code for the form.php file:

    <?php
$result = mkdir("http://dummycode.com/projects/projectform/dir", 0700);
if ($result == 1) {
    echo "Success creating directory!";
} else {
    echo "Error creating directory!";
}

?>

I cannot seem to find the issue because every time it says "Error creating directory!" but the PHP is not throwing any errors.

Is there something simple I am missing, or am I just being stupid?

Thanks, Henry Harris

Upvotes: 0

Views: 485

Answers (2)

paxdiablo
paxdiablo

Reputation: 881113

While PHP 5 introduced support for protocol wrappers, the support is not universal, as explained here:

5.0.0 As of PHP 5.0.0 mkdir() can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers for a listing of which wrappers support mkdir().

The HTTP protocol is not supported by mkdir, see here:

Wrapper Summary
Attribute                                   Supported
---------------------------------------     ---------
Restricted by allow_url_fopen               Yes
Allows Reading                              Yes
Allows Writing                              No
Allows Appending                            No
Allows Simultaneous Reading and Writing     N/A
Supports stat()                             No
Supports unlink()                           No
Supports rename()                           No
Supports mkdir()                            No   <----
Supports rmdir()                            No

If you want to make a directory, you'll need to do it using a proper (filesystem-based) file specification rather than a URL.

What's possibly happening is that a directory is being created, it's just that it'll be called http://dummycode.com/projects/projectform/dir and exist in whatever your default directory was when you executed the mkdir. That would explain why there's no error and why the directory doesn't appear to come into existence. I'd have a look into that.

However, that's tangential to the problem, which can be solved by not using a URL.

Upvotes: 2

Johan Snitt
Johan Snitt

Reputation: 101

Have you tried using a relative path rather then a url?

Upvotes: 1

Related Questions