Edit file.php from controller using CodeIgniter - codeigniter

I need to add some content to a file from a controller in CodeIgniter, in my local enviroment I do this and works fine:
$handle = fopen(APPPATH."config/file.php", "a+");
fwrite($handle, $stuff);
fclose($handle);
However the file doesn't seem to get altered when it runs in the production environment server.
At first I though it could be permission issues but chmoding 777 the directory doesn't seem to work either

It seems to me APPPATH is pointing to a different path. Make sure APPPATH is correct.

Dumb of me, I chmoded the folder containing the file but not the file itself.

Related

Can't write image data to path laravel live project

On my project the user is able to upload a profile picture, and if they don't there is a default picture. I am using this on my Laravel project, and works 100%. Now I have hosted my project online it wont allow me to upload a profile picture but picks up the default image. I'm not sure why. The error message i'm getting is:
Intervention\Image\Exception\NotWritableException Can't write image data to path (/home/n1huer/laravel/public/uploads/avatars/1525256072.png)
This is my file structure within laravel
My controller for this is
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('myaccount', array('user' => Auth::user()) );
}
The file path must be right if it is picking up the default?
Any help would be greatly appreciated, thanks
You need to make '/uploads/avatars/' writable as the error is saying that it isn't writable.
Log into the remote server (or use a control panel) and change the permissions to the folder you're trying to write to:
chmod 755 /home/n1huer/laravel/public/uploads/avatars
On a side note,
I would look further into public folders and permissions.:
Here is some good advice:
https://stackoverflow.com/a/37266353/650241
Also, check out the official docs:
https://laravel.com/docs/5.6/filesystem
You can also use the command
sudo chmod -R a+rwx /path/to/folder
to give permissions to a folder and every file and folder inside it. Omit the x from either command if you don't want the files to be executable.
feast your soul here
Happy coding...
For whom this problem occurs:
First you need to know why this problem occurs
- See this path
path (/home/n1huer/laravel/public/uploads/avatars/1525256072.png)
This path is considered this way
http://myname.com/home/n1huer/laravel/public/uploads/avatars/1525256072.png
But it is intended here to be the actual in this
http://myname.com/uploads/avatars/1525256072.png
That is why the problem with the mentioned message occurs
to solve this problem, all you have to do is complete the following
come on this part
->save( public_path('/uploads/avatars/' . $filename) )
and replace it with this code to become like this
->save('./uploads/avatars/' . $filename)
and thus the problem is solved without the need to do anything else that may leave another problem leaving it
I have the same problem while using Image intervention library. It was working well in my local machine but in aws ubunto machine its giving this error. I have tried in many ways but finally I determined this all about permission issue. I'm using below codes to save an image from $request then resize it and then save it in profile folder under public directory.
$img=Image::make($avatar)->resize(300,300);
$img->save(public_path('profile/' .$file_storage_name));
what I see in my aws my profile folder is owned and grouped by root user. So I changed the ownership using sudo chown -R 1000:1000 profile and then changed the group using sudo chgrp www-data profile then give the folder read, write and execute permission using chmod 775 profile .Then magic ,it work .It look like below image

How to read a .docx file via PHP Word sample code in Codeigniter

Am trying to read a .docx file which is inside my project folder via PHP Word library. I have included autoloader like this :
include_once 'vendor/autoload.php'; in my controller.
CODE FOR FUNCTION IN CONTROLLER:
function test_phpword()
{
$a=base_url();
$path=$a."".'123.docx';
$source =$path;
echo date('H:i:s'), " Reading contents from `{$source}`";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
echo write($phpWord, basename(__FILE__, '.php'), $writers);
}
BUT GETTING ERROR LIKE BELOW:
06:18:42 Reading contents from http://localhost/myproject/123.docx
Fatal error: Uncaught Exception: Cannot find archive file. in /opt/lampp/htdocs/myproject/vendor/phpoffice/common/src/Common/XMLReader.php:51
Try this
Change
$path=$a."".'123.docx';
to
$path='123.docx';
And put 123.docx beside your php script file. make sure the two files are in the same place. Run your php script again.
If this helps and works fine you can check the file path and make proper change to your php program.
While loading the path, you have to give relative path to the doc file as shown below,
$random_name = 123.docx;
$contents = \PhpOffice\PhpWord\IOFactory::load('../uploads/'.$random_name);
I don't know whats your $base_url, but it will not work if it is absolute path like http://path/to/doc/file.
I have worked on it and tested. Hope it helps.

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

Carabiner: cache path does not exist. Please set the cache path in config/carabiner.php

I am using code iginiter. When I try running my application from a local browser then this error is throwing up.
Carabiner: cache path does not exist. Please set the cache path in config/carabiner.php.
I have no clue what it is. Tried some online similar solutions like :
http://www.webdevelopersdiary.com/1/archives/06-2012/1.html
but for some reason nothing seems to work. Please help me to come out of this.
Create cache directory specified in config/carabiner.php, at line #50.
Default setting: $config['cache_dir'] = 'assets/cache/';
Remember that cache_dir path is relative to document root (aka FCPATH from CI ; where index.php is located). On initialization, FCPATH and cache_dir are concatenated, resulting cache_path.
Excerpt from library:
$this->cache_path = $this->fcpath.$this->cache_dir;
Update
You need to create assets/cache directory, which is located in you application root, with write permissions: mkdir -p assets/cache && chmod -R 777 assets/cache
In the end, it will look like this:
$ tree
.
|____application
|____assets
| |____cache
|____index.php
|____system
|____user_guide

How do I get the full local path of a file uploaded with $_FILES

I am uploaded a file through the file input. If I print_r($_FILES), I can get several pieces of info, but it doesn't give me the full LOCAL path of the file (the path on my computer, not the server). How do I get this?
I need this to use the FTP library in CodeIgniter. Documentation is here on how to use it to upload a file.
As you can see, it requires the full local path, although I'm not sure why.
As file upload path is a variable which changes from server to server, I would suggest you to use move_uploaded_file() function:
$target = "uploads/myfile.something";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
// do something
}
that way it will always work no matter what the path is even if it changes. The target can be anything you wish, it's relative to the current folder where script is being executed.
I solved this.
public function upload($file, $folder) {
$this->CI->ftp->upload($_FILES['file']['tmp_name'], '/public_html/rest/of/path/' . $_FILES['file']['name'], 'ascii', 0775);
}
The file is being uploaded, so you can access it with $FILES['file']['tmp_name']. This is from within a class, so that's why I'm using $this->CI. Otherwise, I would just use $this.

Resources