I'm trying to read an excel file(xlsx), I defined the path using
$uri = "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx";
but I keep getting the error
File "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx"
does not exist. I'm new to this framework.
This is the controller code.
public function show($id)
{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setReadDataOnly(TRUE);
$uri = "public/storage/resultsheet/Revival Royal Academy_Primary 5B_1572753672.xlsx";
$spreadsheet = $reader->load($uri);
$worksheet = $spreadsheet->getActiveSheet();
return view('student.result', compact('worksheet'));
}
The path you're using is relative to the current directory that PHP is running in. For Laravel, that likely means you'll be running this in app/Services/MyUploadService.php or something similar.
Given your path is relative, PhpOffice will probably be trying to load `app/Services/public/storage... etc.' which is incorrect.
Laravel has helper methods that can assist you in working with paths within your app.
You could look at using base_dir() (so: base_dir('public/storage/example.xlsx').) as it will give you an absolute path, and a similar usage of public_path() would be better than that.
However, best out of all the options would be using storage_path(). So your code would instead read:
$reader->setReadDataOnly(TRUE);
$uri = storage_path("resultsheet/my.xlsx");
$spreadsheet = $reader->load($uri);
There's helpful documentation on the Path helpers in the Laravel docs.
To debug this, storing realpath('subdir/example.txt') etc. in a variable and using a debugging tool will help you figure out the relative/absolute paths in your project and server.
Related
I want to save my logs to a folder which I can access with windows explorer. For example I want to create my log in the following path
This PC\Galaxy A5 (2017)\Phone\Android\data\MyApp\files
So I tried to use Environment variables... I get such as
/data/user/...
But here i cannot see the file what I created (using code I can access the path but I want to see in the explorer).
how I can create a path like above with code?
When I tried this code
var finalPath2 = Android.OS.Environment.GetExternalStoragePublicDirectory
(Android.OS.Environment.DataDirectory.AbsolutePath);
I get the path "/storage/emulated/0/data"
and
If i use the code
var logDirectory =Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.ApplicationData),"logs");
I get the following path like:
/data/user/0/MyApp/files/.config/logs
and
var logDirectory =Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyDocuments),"logs");
"/data/user/0/IM.OneApp.Presentation.Android/files/logs"
but unfortunately I cannot access this folder by explorer....
This PC\Galaxy A5 (2017)\Phone\Android\data\MyApp\files
So how to find out this path in c# by using environments?
Update:
when I give the following path hardcoded, it creates the file where I want..
logDirectory = "/storage/emulated/0/Android/data/MyApp/files/logs";
is there any environment to create this path? I can combine 2 environments and do some string processing in order to create this path. But maybe there is an easier way?
You are looking for the root of GetExternalFilesDir, just pass a null:
Example:
var externalAppPathNoSec = GetExternalFilesDir(string.Empty).Path;
Note: This is a Context-based instance method, you can access it via the Android application context, an Activity, etc... (see the link below to the Android Context docs)
Shared storage may not always be available, since removable media can be ejected by the user. Media state can be checked using Environment.getExternalStorageState(File).
There is no security enforced with these files. For example, any application holding Manifest.permission.WRITE_EXTERNAL_STORAGE can write to these files.
re: https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)
string docFolder = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyDocuments), "logs");
string libFolder = Path.Combine(docFolder, "/storage/emulated/0/Android/data/MyApp/files/logs");
if (!Directory.Exists(libFolder))
{
Directory.CreateDirectory(libFolder);
}
string destinationDatabasePath = Path.Combine(libFolder, "temp.db3");
db.Backup( destinationDatabasePath, "main");
I need to know the name of the current log in an app Laravel 5.
Try search for this on Iluminate\Log\Writer.
Only see $path var on call to some functions, but I don't understand how to get this value.
Basically, I need to compress the file and send it if the app gets some exceptions. For this, I need to know the name of active log.
My app conf log for daily rotation and see name of logs such laravel-2016-04-29.log
I know the name using PHP, but I imagine it is possible to know using the Log class itself.
The best I've thought
use Carbon\Carbon;
...
$carbon = new Carbon();
$log = storage_path().'/logs/laravel-'.$carbon->toDateString().'.log';
I think there is a better way
I happened to dig a little bit into Laravel logging a few days ago and possibly have a solution for you.
Log::info('abc');
foreach (Log::getMonolog()->getHandlers() as $handler) {
$stream = $handler->getStream();
if ($stream) {
$meta = stream_get_meta_data($stream);
echo $meta['uri'] . "<br/>";
}
}
This will output paths of log files that have been written to.
Otherwise getStream() will return null and therefore path cannot be extracted so easily. It is stored in url property of Monolog\Handler\RotatingFileHandler, unfortunately protected, therefore you would need to extend this class to get the path.
The normal way to load a language file located in the admin app is like so:
$language = JFactory::getLanguage();
$language->load('com_yourcomponentname', JPATH_ADMINISTRATOR);
And to load a language file from the site app:
$language = JFactory::getLanguage();
$language->load('com_yourcomponentname', JPATH_SITE);
These methods load language files from /administrator/language and /language respectively.
Presently, I need to load a language file from a module that locates its language files at /modules/mod_foo/language. How would I do that?
OK, it's as simple as replacing JPATH_SITE with the full path to the module like so:
$language = JFactory::getLanguage();
$language->load('mod_foo', JPATH_SITE.'/modules/mod_foo');
This of course assumes that the language file you want to load is located at:
/modules/mod_foo/language/xx-XX/xx-XX.mod_foo.ini
I had tried this before posting the question, but it didn't work due to a silly typo.
Perhaps a version or file location difference, but in Joomla3.8.x, Mark Simpson's answered did not work for me with a component. The below did:
$lang = JFactory::getLanguage();
$extension = 'com_example';
$base_dir = JPATH_SITE;
$language_tag = 'en-GB';
$reload = true;
$lang->load($'com_example', $base_dir, $language_tag, $reload);
$language = JFactory::getLanguage();
$boolan = $language->load('filename', JPATH_SITE);
I tested it with version 3.9.14 and it works because all the languages are in the path language.
The filenames are com_name, mod_name, lib_name, or tpl_name with language tag at the first part of the filename. The language tags will be added by the load function.
So my component use two language files. com_name and com_name_special.
com_name should be loaded by system but my com_name_special needs to be loaded by the extra load function.
Since Microsoft Web API isn't MVC, you cannot do something like this:
var a = Request.MapPath("~");
nor this
var b = Server.MapPath("~");
because these are under the System.Web namespace, not the System.Web.Http namespace.
So how do you figure out the relative server path in Web API ? I used to do something like this in MVC:
var myFile = Request.MapPath("~/Content/pics/" + filename);
Which would give me the absolute path on disk:
"C:\inetpub\wwwroot\myWebFolder\Content\pics\mypic.jpg"
You can use HostingEnvironment.MapPath in any context where System.Web objects like HttpContext.Current are not available (e.g also from a static method).
var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/SomePath");
See also What is the difference between Server.MapPath and HostingEnvironment.MapPath?
string root = HttpContext.Current.Server.MapPath("~/App_Data");
As an aside to those that stumble along across this, one nice way to run test level on using the HostingEnvironment call, is if accessing say a UNC share: \example\ that is mapped to ~/example/ you could execute this to get around IIS-Express issues:
#if DEBUG
var fs = new FileStream(#"\\example\file",FileMode.Open, FileAccess.Read);
#else
var fs = new FileStream(HostingEnvironment.MapPath("~/example/file"), FileMode.Open, FileAccess.Read);
#endif
I find that helpful in case you have rights to locally test on a file, but need the env mapping once in production.
I can't tell from the context you supply, but if it's something you just need to do at app startup, you can still use Server.MapPath in WebApiHttpApplication; e.g. in Application_Start().
I'm just answering your direct question; the already-mentioned HostingEnvironment.MapPath() is probably the preferred solution.
Since Server.MapPath() does not exist within a Web Api (Soap or REST), you'll need to denote the local- relative to the web server's context- home directory. The easiest way to do so is with:
string AppContext.BaseDirectory { get;}
You can then use this to concatenate a path string to map the relative path to any file.
NOTE: string paths are \ and not / like they are in mvc.
Ex:
System.IO.File.Exists($"{**AppContext.BaseDirectory**}\\\\Content\\\\pics\\\\{filename}");
returns true- positing that this is a sound path in your example
Little bit late answering that but there we go.
I could solve this using Environment.CurrentDirectory
The selected answer did not work in my Web API application. I had to use
System.Web.HttpRuntime.AppDomainAppPath
You can try like:
var path="~/Image/test.png";
System.Web.Hosting.HostingEnvironment.MapPath( # + path)
hi i am using codeigniter in my application .
for my application i need application path only before the last slash
like this
if application path is
http://localhost/elephanti/connections/fb_connection/invite_friends
i want to get
http://localhost/elephanti/connections/fb_connection
it is for a plae like this
echo "<script type='text/javascript'>top.location.href = '"+APPPATH+"'testapp';</script>";
i tried to use '"+APPPATH+"'../testapp' but could not , please help ...................
If you know the last part will always be the 4th, you can use a "dirty way" like this:
$url = site_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3));
And then:
echo "<script type='text/javascript'>top.location.href = '".$url."'/testapp';</script>";
// here, though, I don't understan how "testapp" goes into the url..as a segment?
This will create a valid CI url using only the first 3 segments.
Alternatively, you can use the uri_string() function, wich returns only the segment part of the url. On this, you can explode/str_replace/array_pop/do whatever you need to and pass the new elaborated string on the site_url() function wichi will build the correct URL for you.
I don't understand what "testapp" is there or, do you want to substitute the last segment of your url with that? So that you have, in your example, http://localhost/elephanti/connections/fb_connection/testapp ?
Keep in mind that both uri_string() and site_url() are functions from the URL helper so you need to load it.
In Codeigniter APPPATH referes to the location on the disk and not the url of the application. What you are looking for is base_url(). base_url() return the base location of your site, which usually is the domain.
Concatenation in php is done with the dot operator: "."
echo "top.location.href = '".base_url()."'testapp';";