I'm attempting to modify a text file stored on the server using Larvel's File::put(filelocation,filecontents)
but I can't figure out what location this is relative to on the server. If I have a file "somestuff.json" within my LaravelApp/public folder, what location string do I use for a parameter to File::put() ?
File name for File::methods() is not relative you have to give the full file path:
File::put('/var/www/LaravelApp/public/somestuff.json', $filecontents);
But Laravel has some helpers to help you with this:
File::put(public_path().'/somestuff.json', $filecontents);
Also:
base_path(); // the base of your application LaravelApp/
app_path(); // Your LaravelApp/app
They are all in the file vendor/laravel/framework/src/Illuminate/Support/helpers.php.
Related
I have writen a Ruby script to lookup for documents with a given date and upload them to Google Drive by using the google-drive-ruby gem. I have a folder inside of the gdrive root path where I want to place the files, and I access it using collection_by_title and then uploading the file by using the .add method.
The problem is that the files are being uploaded each one two times, one to the folder I want and another one to the root path of my GDrive. Any thoughts?
This is the method where the file gets uploaded:
def upload_document(file, folder_code)
folder = #session.collection_by_title("#{folder_code}")
path = "#{#basedir}/#{folder_code}/#{file}"
folder.add(#session.upload_from_file(path, file, convert: false))
end
EDIT: Methods and variables translated to english.
Each time the method upload_document is triggered, one copy of the file gets uploaded to the folder and another copy gets uploaded to the root path of gdrive.
Example: Method upload_document gets called providing the file (454327.pdf) and the code of the folder where it has to be uploaded in gdrive ("1"). I build the folder object by using collection_by_title, I build the path where the file is located in my local network, and finally the file gets uploaded using upload_from_file. At this point, two copies of the file had been uploaded, one to the root path of gdrive (which I don't want) and another one to the right folder in gdrive.
I received an answer from the gem creator explaining what is happening and my script is finally working as I expected.
https://github.com/gimite/google-drive-ruby/issues/260
The thing is that the file is firstly uploaded to the root by default and then .add just moves the file to the selected collection, so the file needs to be removed from root after the move operation is completed.
#session.root_collection.remove(file)
While uploading image i have this path:
public function uploadImage()
{
$target_dir = 'C:\xampp\htdocs\application\public\newuploads';
...
It worked fine.
But, when i uploaded to production server i change it to:
$target_dir = 'www.mywebsite.com\public\newuploads';
It gives me error:
move_uploaded_file(www.mywebsite.com\public\newuploads/1487912832.
jpg): failed to open stream: No such file or directory
How can i write the relative path instead?
You should never hardcode the path. Use public_path() helper to get path to the public directory:
public_path('newuploads')
Never use any static path for local or live server.
Laravel provides lot of functions to do this:
Just use it like:
$target_dir = public_path()."/newuploads";
Use the same on local and live environment.
This will work.
For More Details And Laravel Helper Functions You Can Check This:
https://laravel.com/docs/5.4/helpers
Thanks
Depending on your production server, you may have a different root directory. That being said, instead of using a full folder path, you can use the public path root such as
$target_dir = '/newuploads';
That way if the underlying OS and file system changes, your site operations are intact.
we are using magmi_full_0.7.19a
we placed a csv file under var/import directory.
but still we are getting mesage as : No csv files found in magmi page
Relative paths are relative to magento base directory , absolute paths will be used as is
Put Your csv files under the magmi/var/import
but generally magmi take path in root/var/import so make sure that you have to create a imoprt folder in var
I need to insert the contents of a file into the database during a migration (Rails 3.2.13). What's the proper way to reference a file that is elsewhere in the project?
db/migrate/the_migration.rb
class ...
content = File.read("../../app/views/layours/application.html.erb")
end
The relative path doesn't seem to work - I get:
No such file or directory - ../../app/views/layouts/application.html.erb
How can I map this path to an absolute path?
You can try the code below:
class ...
path = File.expand_path('../../app/views/layouts/application.html.erb', __FILE__)
content = File.read(path)
end
assuming you are using rake to apply an active record migration. The file path will be relative to where you started rake which I'm sure will be the projects root.
The file path would be:
content = File.read("app/views/layouts/application.html.erb")
This question is related to Rails - Where I have to store data file (.dat) in my rails project - GeoIp City database . I have a rails 3.2 app. I am trying to run:
#geoip = GeoIP.new('GeoLiteCity.dat')
In one of my app's controllers. I unzipped the 'GeoLiteCity.dat' file into the /public folder. I am getting the error "No such file or directory - GeoLiteCity.dat".
I've experimented with putting it in the images assets pipeline folder and some random other places. I continue to get the same error. Not sure how to access this file. Any ideas on what I'm doing wrong or how to access it best with the assets pipeline?
Try referencing it via the full path:
#geoip = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat")
On a side note, it's probably not a big deal, but I wouldn't put the file in your public directory.