Laravel cannot find file in directory? - php

http://angulairapi.rohanchhabra.in/airports
This is a very simple route I have created in Laravel. It just takes a json file in the public directory, decodes it into an array and return the same json in response.
If you go the route (mentioned above), the error say "No such file or directory" but it exists in fact. It is working fine on my local machine. But when I pushed the same thing on my server, it is giving me this error.
http://gitlab.learningtechasia.com:8901/rohan0793/angulairapi.git
I have made the repository public so that everyone can have a look.

I have tested on my machine asset() is working for me and the path(public/airports.json) you have written reflect same error for me.
Laravel`s Helper function asset("file_name") generate a URL for an asset.
please check laravel`s helper function documentation for more detail
put this code in your routes.php and try again
<?php
Route::get('/airports', function(){
$airports = json_decode(file_get_contents(asset("airports.json")));
return Response::json($airports);
});
Route::get('/flights', function(){
$airports = json_decode(file_get_contents(asset("flights.json")));
return Response::json($airports);
});
EDIT
When you are working on local machine your url having word public i.e localhost/project-name/public/airports.json.
but when you deploy project on server it seems it remove public word from url, so what happing here, server finding airports.json at location http://angulairapi.rohanchhabra.in/public/airports.json but its not actually there its at location http://angulairapi.rohanchhabra.in/airports.json, so it is recommended to use laravel function(in this case asset()) to generate url/assets link.

An alternative to asset() and Anands answer is the helper function public_path(). It returns an absolute file path from the system point of view and not a URL.
$airports = json_decode(file_get_contents(public_path().DIRECTORY_SEPARATOR."airports.json")));
asset() should be used for URLs to files. URLs that you send to the client. You should work with public_path() for internal things, such as getting the content of a file.

Related

Laravel nova action download a file causes : Failed - no file

I want to create a file and download it during a Laravel Action.
I use the following code:
public function handle(ActionFields $fields, Collection $models)
{
Storage::disk('local')->put('file.txt', 'example');
$url = Storage::disk('local')->url('file.txt');
return Action::download($url, 'file.txt');
}
This causes the following error :
Failed - no file
What am I doing wrong here?
Not sure where that error message comes from, but it may be the following problem. Calling return Action::download($url, 'file.txt') laravel nova will instruct the browser to download the file at the given url. As mentioned here this url will (depending on your config) probably be the relative url /storage/file.txt. So the browser will try to download http(s)://domain/storage/file.txt which might not be there. The docs mentioned above hold a solution for that.
If you wouldn't even want to create a file which would maybe need cleanup and protection if the data is sensitive, you could as well point the url to a route and do processing in a controller.

base url not working in server while using excell reader

i doing excell file reader in my local is working fine, i giving the base url like this
"C:/xampp/htdocs/school/uploads/staff_importFiles/".$sFileName
So my local is working fine,but when ever i uopload the code in my server it is not working , in my server base url i am ging like this
"http://domain.in/school/uploads/staff_importFiles/".$sFileName
If i am giving means i am getting error like this
The file http://domain.in/school/uploads/staff_importFiles/30e97727b3dc9f2ebe493fa3165b51c7.xls was not found
inside this staff_importFiles folder my files is there , but don't know it throwing the error, i am using codeigniter,any one help me please
In codeigniter use base_url() function.
<?php echo base_url().'uploads/stuff_importFiles/'.$sfileName ;?>
I think you have subdirectory named school in your local. While it does not exist on your server. So you should change your server url to.
"http://domain.in/uploads/staff_importFiles/".$sFileName
While this is not recommended way of doing this. You should use base_url()

Laravel route resource issue

I use:
wampserver
laravel 5
bootstrap v3.3.6
I'm new in laravel, I usually use a manual route::get. Now I try to use route:resource to make things faster but somehow those two display different result even though it's should be showing the same page.
My routes code:
Route::get('create', 'PostController#create');
Route::resource('posts','PostController');
as you can see above, it's directing to the same controller
PostController#create code:
public function create()
{
$data['title'] = ' | Create Post';
return view('posts.create',$data);
}
When I go to blog.dev/create (which is from route::get). It's successfully displaying:
BUT, when I go to blog.dev/posts/create (which is from route::resource). It's showing a css-less page:
Why is that happen? any way to fix it? (I prefer not to use route::get)
Note:
I've tried different browsers, clear cookies&cache and restarting wamp
Both have exactly the same code in view page source from browser.
It seems to be issue with css file paths. Your css paths are relative.
make your css file path to be absolute.
Read this:-
https://css-tricks.com/quick-reminder-about-file-paths/
https://www.w3.org/TR/REC-CSS1/#url
http://jeffreybarke.net/2013/06/paths-and-urls-relative-and-absolute/

Path in an AJAX call keeps changing in ZF2

I have a bit of an annoying problem in Zend Framework 2. My js, which is located in my public/js folder, calls an action in the controller for my admin module. This is the call:
$.post('admin/expand', {
id: CCID
},function(data){
if(data.hasOwnProperty('info')){
expand(data.info);
} else {
console.log('Can\'t find customer info.');
}
},'json');
The call works fine normally, but then sometimes it won't be able to find the action. The console will say:
POST http://localhost/admin/admin/expand 404 (Not Found)
So I'll change the path in the AJAX to just 'expand' instead of 'admin/expand' and it'll work for a while... and then it won't until I change it back!
So it seems that sometimes it gets confused about the routing and sometimes it doesn't. Is this a namespace problem? The js file is supposed to be in my root/public/js, right? Does anyone know what the problem is here?
Try changing the url from admin/expand to /admin/expand . It is more pragmatic to use ZF2 routes.

Nicedit upload images locally fails

This is how I call the editor:
new nicEditor({
buttonList : ['bold','italic','underline','upload'],
iconsPath:'img/nicedit.png',
uploadURI : 'http://server.com/integracion/files/nicUpload.php'
}).panelInstance(textareaId);
And the .php file exists ( and I the one in the Docs, and I updated the target paths )
/* I want them here http://server.com/integracion/files/uploads/ so... */
define('NICUPLOAD_PATH', './uploads'); // Set the path (relative or absolute) to
// the directory to save image files
define('NICUPLOAD_URI', '/uploads'); // Set the URL (relative or absolute) to
// the directory defined above
But I on response when upload completes (and of corse an alert from nicedit..)
<script>
try {
top.nicUploadButton.statusCb({"error":"Invalid Upload ID"});
} catch(e) { alert(e.message); }
</script>
what am I missing?
-EDIT
I think the problem might be in the php file:
$id = $_POST['APC_UPLOAD_PROGRESS']; /* APC is installed and enabled */
if(empty($id)) {
$id = $_GET['id'];
}
FINAL EDIT:
I have managed to make this work!
Here is an working example:
http://simplestudio.rs/yard/nicedit/
Uploaded images are going to be stored here:
http://simplestudio.rs/yard/nicedit/images/
And here is the whole code, just unpack it and put on your server, mainly I needed to adjust nicEdit.js because it had some issues.
http://simplestudio.rs/yard/nicedit/nicedit.rar
Just make your code with that js file and by looking at my example, it will work :)
Also you need to have php APC installed so that this script can work:
http://php.net/manual/en/apc.installation.php
If you by any mean have some problems I am here to solve it.
I will not delete this example on my server so that everybody who have this issue can freely download it...
The code responsible for image upload is the method uploadFile, it is looking for uploadURI option parameter.
You will need to modify onUploaded event handler to parse your custom response instead of the imgur's one (sample). By default it expects at least {"upload": { "links": {"original": "http://..."}, "image": {"width": "123" } }}.
I'm sorry but I can't help with the FormData() handling server side with PHP.
For more information you can try out the demo page on the nicEdit web site using Firebug or WebInspector to snoop the network requests, and, of course, the source code.

Categories