Yunus Aslam
Yunus Aslam

Reputation: 2466

Silverstripe upload file to root folder

I am using Silverstripe 3.0 X .. I have created a new upload field which is working fine and uploading the files to Uploads folder inside assets.

I want to set the upload path to the root directory that is inside the public_html/ I know that we can set the path by using

 $uploadField->setFolderName('customfolder');
 // This will create a new folder inside assets/Uploads/customfolder

But I need to make some changes so that I can upload the file directly to the root folder. How can I achieve that?

Upvotes: 2

Views: 1045

Answers (2)

Ryan Cotter
Ryan Cotter

Reputation: 1

When you upload to assets, your apache user takes ownership of the /assets path you're uploading to. This is handy in Silverstripe because you can use the interface to upload/download files and you typically don't physically edit the image's code itself, meaning nobody needs write or execute privileges except Silverstripe itself.

However, you often do want to edit php classes, css or config files in the root directory and below. This can become tedious if you are constantly forced to chmod stuff or log in as the apache user.

If you need to upload to root, I would suggest that you upload to a folder within assets and perhaps keep the actual folder name it's stored in as a config variable on the class. That way, you can just check the class config() and use Director or Folder to get the assets URL.

You can also save the file in the database as a File object.

Upvotes: 0

Selay
Selay

Reputation: 6464

Well, although you could, probably set as '/', but the simple answer to your question is why you need that? It is never needed and can't be done because of permission issues.

You can't really allow a write permission for the root folder, right? Your root folder contains important files and should be web-visible only (not writable).

You need uploaded files to go to assets or any other custome folder which you are happy to give higher permission.

If you really need to add a file to the root folder, it should be directly via FTP or direct copy-paste.

You can upload your files to assets or other directory but still set .htaccess to publicly show as /myimage.jpg or something. But this is another issue.

Upvotes: 2

Related Questions