fopen failing in Kernel Command but not in controller Laravel - php

I am using fopen to generate a csv file. The function works as expected in a test controller but when I move to a Console/Commands file, it fails with 'failed to open stream: No such file or directory'.
This is the fopen line;
$fp = fopen('dataFiles/dataFile_'.date('Ymd').'.csv', 'w');
I have tried to change the url declaration to;
$fp = fopen('/dataFiles/dataFile_'.date('Ymd').'.csv', 'w');
No dice. The dataFiles folder is in my projects public dir.
What could be the issue here?
This is a 5.3 project.
Thanks!

you have to use the public_path() to specify the public folder location. otherwise the file location is taken from the existing folder. in your case it is checking if the is file existing in Console folder, in your second example it is taking the file path from the project root.
$fp = fopen(public_path('dataFiles/dataFile_'.date('Ymd').'.csv', 'w'));
I have one suggestion for you. if you are not planning to make this files public you should put them in the storage folder. That's the place for these kind of files
Path helper functions

Related

File transfer from one directory to another project directory using php

I wanna transfer a file from one project directory to another project directory. Here i tried to use copy() function but failed. Later i used move_uploaded_file. Here also failed. I guess , its pretty simple but unable to me for solving. Here is my code.
$newPath = '././project_b/uploads/images/1582353992_flowers_yellow_blossom_windy_nature_434.mp4';
$sourcePath="/public/Files/Compress_video/1582353992_flowers_yellow_blossom_windy_nature_434.mp4";
$upload = move_uploaded_file($sourcePath, $newPath);
Here $newPath is my send project directory and $sourcePath is first project.
Hope you got my point.

PhP fopen creates file with Path name

Trying to update a file file.xml, which is with folders dirA/dirB/dirC/file.xml where dirA is the current working dir. The file file.xml exists and has write permissions.
Using the following code works in local but on server it created a file by name "dirA\dirB\dirC\file.xml" outside dirA and saves into it
$file = fopen("dirA\dirB\dirC\file.xml", "w+")
fputs($file, $xmlFile);
fclose($file);
Any idea why?
Maybe because you are running another environment on your server?
Windows and Linux are a little bit itchy on their folders.
You may also check if you have to use backslashes or not!
Probably you also have to quote them:
$file = fopen("dirA\/dirB\/dirC\/file.xml", "w+");

Laravel 5.2 File Upload

When I try and upload a file as follows:
$name = $img->getClientOriginalName();
$fullPath = Product::getUploadPath(); // Returns public_path(); with custom directory appended
$uploaded = $img->move($fullPath, $name);
I get the below error:
Could not move the file
"/private/var/folders/1l/fxl7spqj2p113fpffm3k0sy00000gn/T/phpIgpWGy" to
"/dir/subdir/subsubdir/subsubdir/public/backend/images/products/pic.jpg" ()
For interestsake, I built the entire resolve path for the image, and passed that as an argument to the move() method. Memory served that it has worked previously.
$uploaded = $img->move($fullPath.'/'.$name);
No exceptions are thrown, however, the uploaded file, now becomes a directory:
"/dir/subdir/subsubdir/subsubdir/public/backend/images/products/pic.jpg/"
I'm very close to driving my fist through my screen.
Could be a permissions issue - the user running the PHP process will need to have write access to the directory where the file will be saved. If you can stomach chmod'ing the directory to 777 for testing, that would at least provide an immediate yes or no to the permissions possibility.
Also, be sure that the directory that file is to be moved to exists - Symfony's UploadedFile::move() method falls back to PHP's move_uploaded_file() function, which according to a comment on the documentation will not create missing directories when moving a file.
When it comes to uploading files, we've all been there before. Can be maddening.

PHP is_writable() to check if file can be written?

I've read that I can use is_writable() to check if a folder or a file is writable.
How do I check if a file can be written to a folder?
Do I check the folder and if the folder is writable? Then the file is allowed to be put into that folder?
What if the file has been written to the folder, how do I check if that is writable again (edited)? Do I need to? If so, do I check the file instead of the folder?
Is it a safe method (correct) to do it?
The PHP function is_writable is exactly for this purpose. If you want to check if file is still writable after you've written the file, you can use the same function.
Read the documentation as linked in the question you pointed to. is_writable() is working on files and directories.
But mind: If you have code like this:
if (is_writeable("foo.txt")) {
$fp = fopen("foo.txt", "w");
/* ...*/
}
This might still fail. For instance there might be a lock or a race condition (permissions change between the two commands). Better simply try to open and then handle the error.
$fp = #fopen("foo.txt", "w");
if (!$fp) {
report_error_in_some_way();
}

Editing files outside of the public_html folder from inside the public_html folder

Using php, how can I edit a file that is located in a folder outside of the public_html folder from a file that is located inside the public_html folder? I tried using the include function for the filename of the file outside of the public_html folder, but it runs the external file displaying contents of it (using fputs) for the current file instead of just updating the external file.
<?php
include "hits.txt";
$filename = "hits.txt";
$count = file($filename);
$count[0]++;
$file = fopen ($filename, "w") or die ("Cannot find $filename");
fputs($file, "$count[0]");
fclose($file);
?>
From what I understand, you want to simply edit a file in the parent directory? To do this, you'll need to provide the open function with either an absolute/relative path to your file. That is:
$file_relative = "../../file.txt";
$file_absolute = "/some_file/www/file.txt";
fopen($file_relative, "w");
// Edit your file as necessary
Please note, that while this is technically feasible, it may be disallowed. You may not have the appropriate permissions to edit anything above public_html.

Categories