cream
cream

Reputation: 1129

Correct syntax for setting destination folder in PHP

I'm trying to adapt an upload script to fit on 000webhost. I keep getting errors about the destination folder not existing. This is what I have:

define('DESTINATION_FOLDER','/uploads/');

I've also tried

/public_html/uploads/

and

/subdomain/domain/com/uploads/

In examples I've seen people were using /www/ but I don't know where that goes.

What is the correct syntax to use in this case?

Upvotes: 1

Views: 326

Answers (1)

jimp
jimp

Reputation: 17487

Try this:

define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].'/uploads/');

Consider using the PHP constant DIRECTORY_SEPARATOR, if platform independence matters to you:

define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.
                            'uploads'.DIRECTORY_SEPARATOR);

http://php.net/manual/en/dir.constants.php

EDIT:
Storing uploads under the document root can be a security risk. Unless you want those files to be directly accessible by the web server, you should consider storing them outside of the document root within your webspace. If you created a folder named "uploads" along side your "public_html" folder with your host, you could access it like this:

define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.
                            '..'.DIRECTORY_SEPARATOR. // (up one level)
                            'uploads'.DIRECTORY_SEPARATOR);

Or just specify an absolute path (leading with /) directly to the folder.

Upvotes: 4

Related Questions