yii2 make uploaded files private - yii2

Currently in my Yii2 app, files are uploaded to the public path web. But I need to make those files private. I know it is possible to prevent direct folder access with .htaccess but it is still accessible if we type the full url in the browser. Is there any way in Yii2 to make the files private so that it can be accessible via only controller paths so that we can apply the Access control and RBAC etc.?

You can do it this way:
Create uploads folder in Frontend directory:
root/uploads
Controller action:
Yii2 Basic App
#app: Your application root directory
Yii2 Advanced
#frontend: Alias for your frontend root folder on your root app install directory
Source: Yii 2 list of path aliases available with default basic and advanced app.
//Basic App create path to folder that can contain user files
$path = \Yii::getAlias('#app/uploads/');
//set UploadedFile $object to property
$model->file = $object;
$model->upload($path);
Now we can store any files out from web directory;
Upload model:
Save method:
$this->file->saveAs($path . $this->file->baseName . '.' . $this->file->extension);
After that you can use same $path var to access thous files from any controller. Also make sure, that you have a configured .htaccess file in the root, which points all frontend requests to the web directory.
Hope it helps you.

Related

I can't load a html file in public folder from my Firebase function

I know the path directory is the right path, it's just that I always get the error 'Error: ENOENT: no such file or directory, stat '/public/resetp.html'
My paths are as follows:
index.js: firebase/functions/index.js
reset password.html: firebase/public/resetp.html
When I go to this endpoint on my function on my website it runs this code:
response.sendFile(path.join(__dirname, '/./public/resetp.html'));
I need to use the sendFile() function so that I can just render the html file and not change the URL since in the html file that I'm trying to load, it gets the parameters of the URL (like the action code) and gets the users input to reset the users password.
Thanks, Nathan
You can achieve this by making your public directory inside your functions directory:
{
"hosting": {"public": "functions/public"}
}
All of the files in your functions directory are deployed with Cloud Functions, and no files outside of it are available.
Warning: The files that will be available will be whatever files are in the public directory at the time you deploy Cloud Functions. It is up to you to make sure Hosting and functions deploys are kept in sync.

Deploy and host .net core 2.0 and angular 6 app

Intro:
Currently, I have two separate projects.
One solution is for .NET core Web API 2.0 and another project is Angular 6 app.
I have a problem deploying this to my hosting provider ( asphostsportal ).
It consists of one root folder httpdocs where when I publish my API I copy contents of publishing folder.
After transferring files via FTP I confirm that my API works by sending a request and getting the response.
Now I need to publish my angular project.
The simplest way to do so is with ng build and I get some folder in dist.
Problem:
If I transfer contents of the folder inside dist to my root folder httpdocs on server It can't find any page but API still works.
If I transfer the whole folder inside httpdocs and set server Host setting to define document root to that folder:
It will find the index.html and show me my landing page.
However then API won't work.
If I leave it as is and change the document root back to httpdocs ( where the API files are ) but push the index.html from httpdocs/website to httpdocs ( in this case I also edit index.html to point to website folder for resources etc ) in the hope it will find it ( server IIS setting )
It doesn't. After that my API starts working again. However, no landing page to be found.
Just FYI I do not have any extra startup.cs lines of code that have anything to do with static files nor did my publish command on API project create wwwroot folder ( like mentioned here)
Edit (Solution):
Okay, so I got it to work.
1) I added 2 lines of code in Configure method of Startup.cs in my API
app.UseDefaultFiles();
app.UseStaticFiles(); // For the wwwroot folder
Where according to MSDN
"With UseDefaultFiles, requests to a folder search for: index.html"
2) I manually added wwwroot folder which for some reason was not generating upon publishing command for my API project.
3) In that folder I copied all contents of my dist/website angular publish files
And now it works, cheers!

Access Laravel stored files from android app (ionic).

I'm working on an android app with Ionic 3 connected with Laravel to control my data through an api, and i'm trying to display a bunch of images on my ionic app, the problem is i can't.
I was trying to access laravel's storage folder but i can't, i don't know if it's possible to convert a blob image to json ...
Any ideas ? Thanks !
By default, laravel's storage folder can't be seen to the public web.
When you access your laravel app, you are actually accessing the 'public' folder of the root structure. Therefore, only the application has access to the storage folder.
Run the following in your command line in root folder
php artisan storage:link
This will create a symbolic link from public/storage to app/storage/public.
Now, you'll have to put your images inside the app/storage/public folder, and access them trough http://[yoursitehere].com/storage/image1.png
Looks like you need to link your storage directory to your public folder so it's accessible.
Link the storage folder:
php artisan storage:link
Get the image URL:
echo asset('storage/image.jpg');
Check the documentation here https://laravel.com/docs/5.5/filesystem#configuration

Save Images in advanced application of yii2

i have a field called photo in model members. I want to access the photo
from both frontend and backend. So i created two folder called images in backend\web\uploads and frontend\web\uploads and my idea is to save the photo in both backend and frontend at the same time.
If i have to save an image in frontend\backend i would have used this code
if($model->photo != null)
{
$model->photo->saveAs(\Yii::$app->basePath . '/web/uploads/photo/'.$model->id."_".$model->photo);
}
but i can't use this code for my purpose because i have to save it in both frontend and backend. How can i do this?
you can make a symbolyc links so you don't have to save the file in both directories.
in linux just run ln command from a console, example you have backend\web\uploads directory and then you make frontend\web\uploads as a symbolic link to backend directory.
in linux just run:
ln -s backend/web/uploads frontend/web/uploads
in windows, open command promt, go to your yii2 directory and run:
mklink /D frontend\web\uploads backend\web\uploads
notice that mklink inputs is in reverse order than the ln command in linux
In ideal you should create static subdomain like static.yourdomain.com and save images to only one place.
In your case you should duplicate save code to frontend and backend folders.
For example:
You can have the uploads folder inside the common folder. Your save code should now reference the folder:
$model->photo->saveAs('#common/uploads/photo/'.$model->id."_".$model->photo);
You can then save the base image url in your application's param configuration and use that in your views.

AS3 - URLload local files with absoute path

I am trying to open local hard drive files inside AS3. I'm getting a security error with this code:
var soundFile:URLRequest = new URLRequest("c:\slushy.mp3");
Is it possible to URLload an absolute path on the hard drive?
Edit: so this appears to be a sandboxing issue. Drat. Is it possible to load the local file via PHP and send to flash?
PHP is a server language, so the final protocol is http.
The you must to acces by file:/// to the local file, but if you want to share the resources over Internet, you must upload your files to folder in the root of site.
By example: http://www.mysite.com/music
Then you can load the file:
var soundFile:URLRequest = new URLRequest("http://www.mysite.com/music/slushy.mp3");
Requisite: you must to create the directory "music" in server web application directory and upload the file.