How to delete files from Laravel app directory (not the public directory but the one in the root)?
I tried :
\Storage::Delete('App\file.php');
\Storage::Delete('\App\file.php');
\File::Delete('App\file.php');
\File::Delete('\App\file.php');
unlink ('\App\file.php');
Nothing works.
UPDATE:
The file is in something like \App\folder\folder\file.db
When not specifying an absolute path then what is used is the current working directory (the value that getcwd() would return). Usually, that's the /public directory and not the actual project root.
You need to specific an absolute path using the Laravel helpers:
\File::delete(app_path('file.php'));
Note: You can't use the \Storage helpers because they are limited to only work within the app storage folder.
Related
I have laravel installed on my server and I want to add a folder called 'projects' in laravel's root folder and access the projects folder from URL.
What should I do to solve this issue? I want to upload some PHP code demos and allow people to view them.
I have tried playing with the .htaccess but nothing seems to work for me.
public_path(); // Path of public/
base_path(); // Path of application root
app_path(); // Path of app/
I am trying to define the wordpress upload folder outside the installation directory. My directory structure is:
/path/to/my/base/folder
|-> wordpress (the wordpress installation)
|-> uploads (the required upload folder)
I have tried defining the 'UPLOADS' in my wp-config.php like:
define('UPLOADS', '../uploads');
Wordpress creates the folder and the images are uploaded there, but cannot use it when required. The image URL looks like :
www.myhost.dev/../uploads/image-file-name-format.jpg
But if I use absolute path to define the 'UPLOADS' like
define('UPLOADS', '/path/to/my/base/folder/uploads');
this works.
But the problem is, then the image url looks something like:
www.myhost.dev//path/to/my/base/folder/uploads/image-file-name-format.jpg
This tells the location of the file and this can be a security issue.
So, is there a better way to define the relative path for the uploads folder which will point to my required folder?
Thanks in advance.
I have done it at last by making a symlink with proper permissions. In my case, when I tried to make a symlink, the symlink did not have enough permission to write into the folder. This was because of my system (I am using Macbook). So what I did is:
$ cd wp-content
$ mv uploads /path/to/my/base/folder/
$ ln -s ../../uploads uploads
$ chmod -h 777 uploads
Check the last line where I have changed the permission for the symlink. Normally in most of the cases, you don't need to change the permission of the symlink, as they inherit the permission of the folder.
A couple of notes that may help people:
define( 'UPLOADS' , 'blah') is typically RELATIVE to the WordPress core root directory. The default is 'wp-content/uploads'. If you try to put the uploads outside of the WordPress directory with something like '../uploads/' as the define the upload URL will typically not work causing problems.
If you set the path with the define() inside your wp-config.php WordPress will NOT read the upload_path setting, if you have used it.
I found that setting the upload_path option in WordPress is better for storing media files outside the WordPress install path. Login to your WP Admin and go to the url /wp-admin/options.php to see the "raw" options list for WordPress.
Look for the upload_path setting. Put in your FULL PATH on the server like /usr/wpsite/uploads/ where /usr/wpsite/public_html would be where your WordPress core install typically ends up. Save the settings.
Your uploads will now be placed OUTSIDE the WordPress install directory. Make sure the web server has read/write permissions to the directory.
Also note that WordPress will put ALL FILES in that directory. It will not separate them into year/month subfolders as is the default for a typical WordPress install. If someone knows how to retain that feature please comment.
If you take a look at this link http://www.wpbeginner.com/wp-tutorials/how-to-change-the-default-media-upload-location-in-wordpress-3-5/
You need to use a path like so
define( 'UPLOADS', ''.'uploads' );
I've been using laravel for nearly all my projects and I've came across a problem when trying to list all files in a directory.
Here is my code below:
$directory = ("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);
So as you can see First line is the directory which is in Upload followed by the username of the account.
The second line is meant to scan this directory and list only files. Sadly it always gives me back an empty array. Using Scandir() here works... But it gives me folders which I'm not wanting.
So... Any tips? I checked the API and I really can't see what is wrong here :(
The reason you're getting no results when using the Storage::allFiles('directory') method is this method only looks inside of your application's storage folder, usually storage/app. Hence why modifying the storage path in configuration works as mentioned by #yassine (you shouldn't do this).
If you wish to use files outside of the storage folder, simply use the File facade instead. e.g. File::allFiles('directory')
Me too i had the same issue for local storage. I fixed it by :
project/config/filesystems.php
line 44
changed root folder to from storage_path('app') to changed to
base_path()
then I provided a related path to the root folder in Storage::files() or Storage::allfiles().
Like Storage::allfiles("app") if I want to fetch files withing the sub folder "app" relative to the base path given in the config file.
Good luck
$direcotry has to be an absolute path from your laravel root folder.
When your uploaded folder is in the root folder try this
$directory = base_path("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);
I work in responsivefilemanager and in config file I Have two line:
$upload_dir = '/user/uploads/files/'; // path from base_url to base of upload folder (with start and final /)
$current_path = '../../../../uploads/files/'; // relative path from filemanager folder to upload folder (with final /)
This wroked In xammp system (with sub folder. /user/ is sub folder). if i move in real server i need to edit this two line and remove sub folder and one ../ from two line.
Now, I need to auto detect url path. My mean is : if i install in sub folder Or root folder this two line worked in my script without manual editing.
How do can i create this?
There is no particular way to just autodetect what directory to upload to. The easiest is to solve this by just checking if the directory exists and use the other directory if it was not found.
However, I'd suggest having the same relative structure on both systems is the best solution. With the same relative structure this should work across all servers and systems without having to change anything.
Is it possible to include file from the ftp's root in a script in a subdomain ?
This doesn't work for me:
include($_SERVER['DOCUMENT_ROOT'].'/joomla.php');
Can someone help me ?
I think first you need to determine where is your subdomain files are placed and echo the $_SERVER['DOCUMENT_ROOT'] command to see the full path then you can combine two paths and make necessary adjustments.
Usually but not always, subdomain files are placed in a folder one level up then the main site's root folder so you may need to replace the folder name according to your needs in the output of server document folder function.
To give an example if your domain files are in a path like
/var/www/vhosts/example.com/httpdocs/
and if your subdomain path is like
/var/www/vhosts/example.com/subdomain/
then you will need to replace httpdocs with subdomain (or the other way around) in the output where you call $_SERVER['DOCUMENT_ROOT']