get application path codeigniter with out contents after last slash - codeigniter

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';";

Related

I'm having troubles reading an excel file in laravel

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.

Problems about placeholders of Netlify redirects

I am trying to set up some URL rewrites in Netlify.
I want to set up a rewrite to redirect
https://example.com/blog/2019/05/15/hello.html
to
https://example.com/blog/2019/05/hello.html
I read the document - https://www.netlify.com/docs/redirects/#placeholders:
You can use placeholders in the origin and target paths:
/news/:year/:month/:date/:slug /blog/:year/:month/:date/:slug
This would redirect a URL like /news/2004/02/12/my-story to /blog/2004/02/12/my-story
And I have my _redirects file like this:
# Redirect old permalinks to new format
/blog/:year/:month/:date/:slug.html /blog/:year/:month/:slug.html 301!
It did work to redirect
https://example.com/blog/2019/05/15/hello.html
to
https://example.com/blog/2019/05/hello.html
But there is a weird problem, it also redirects
https://example.com/blog/2019/05/not-a-date/index.html
to
https://example.com/blog/2019/05/index.html
My question is, Is it able to (if yes, how can I) make the :date placeholder matching day only? not-a-date is not a date obviously, shouldn't :date match 01 to 31 only?
To my knowledge the placeholders aren't typed (allowing only numbers). So anything will match and it's not possible to define only days as a pattern to match.
https://www.healingislands.com/* http://www.healingisland.com:splat 301!
putting { /* } this at the end of the first link and putting { :splat 301! } this at the end just worked for me

laravel: get name of active log at runtime

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.

url::to(xxx/yyy) returns different results depending on context

I'm using the URL::to call to embed a link in an outgoing mail message. What I get when I do this is something like: "baseroot/public/index.php/xxx/yyy".
And yet when I do the same call, for example, within a route call, I get "baseroute/xxx/yyy".
Any idea?
The source of URL::to resides at
http://laravel.com/api/source-class-Illuminate.Routing.UrlGenerator.html#76-98
(linked to from http://laravel.com/api/class-Illuminate.Routing.UrlGenerator.html).
I suggest you add debug printing to your copy and see what values $this->getScheme() and $this->getRootPath() yield. These must be the source of the discrepancy, apparently caused by different this objects.
I had a very similar problem with URL::to('user/123') returning an incorrect value when visiting the homepage vs. another page. After some investigation, in my case it was a matter of case-sensitivity (!) in the request's url. I hope it's somehow related to your mysterious case.
More about my case: URL:to('user/123') gave me different results whether I visited http://localhost/MyApp/public/someurl or http://localhost/Myapp/public/someurl. In the former it gave the correct result http://localhost/MyApp/public/user/123, while the latter gave the wrong result of http://localhost/user/123.
.
From here, less important notes from my investigation, for future Laravel archaeologists. I hope I'm not talking all nonsense. I am new to Laravel, using a local Laravel 4 installation + WAMP on a Windows machine.
UrlGenerator's to() method uses $root = $this->getRootUrl($scheme);. The latter uses $this->request->root();, where request is \Symfony\Component\HttpFoundation\Request.
Request::root() indeed defaults to a wrong value e.g. http://localhost when visiting someurl with the incorrect case.
The culprit is Symfony\Component\HttpFoundation\Request (in vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\Request.php). Its getBaseUrl() calls prepareBaseUrl(), and there the actual logic of comparing the requestUri with the baseUrl is finally performed.
For the few archaeologists still following, in my case the $baseUrl was /MyApp/public/index.php while the $requestUri was /Myapp/public/someurl, which sadly led the code to not satisfy this conditional:
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, dirname($baseUrl))) {
return rtrim($prefix, '/');
}

System.IO.File.Exists() returns false

I have a page where I need to display an image which is stored on the server. To find that image, I use the following code:
if (System.IO.File.Exists(Server.MapPath(filepath)))
When I use this, I get a proper result as the file is present.
But when I give an absolute path like below:
if (System.IO.File.Exists("http://myserever.address/filepath"))
It returns false.
The file is physically present there, but I don't know why it's not found.
The path parameter for the System.IO.File.Exists is the path to an actual file in the file system.
The call to Server.MapPath() changes the URI into an actual file path.
So it is working as intended.
You can't use HTTP paths in File.Exists. It supports network shares and local file systems. If you want to do this in a web application on the server side. First use Server.MapPath() first to find the physical location and then use File.Exists.
Read about Server.MapPath here: http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx
Eg.
string filePath = ResolveUrl("~/filepath/something.jpg");
if (File.Exists(Server.MapPath(filePath)))
{
//Do something.
}

Resources