Php tmpfile only works from command line - php

When I run this:
$file = tmpfile();
$name = stream_get_meta_data($file)['uri'];
$nfile = new SplFileObject($name);
var_dump($nfile);
from the command line, it works as expected - creating a temporary file, and dumping details about it. However, when I run it through apache, $nfile is an empty object - it seems to be unable to access the file by path. Why is this? I have already confirmed that it is creating a file, and I can see the temporary file in the file system, so it's just php that can't reach it.

Related

PHP - Converting Excel to PDF (Phpspreadsheet) - Operation not permitted

I am trying to convert an Excel file to PDF (Base64).
This is the code that converts the Excel to PDF:
$spreadsheet = $this->objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load("MyExcelFile.xlsx");
$class = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class;
\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', $class);
$this->objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf');
$this->objPHPExcel->writeAllSheets();
//Save the file. (THIS IS WHERE THE ERROR OCCUR)
$this->objPHPExcel->save(storage_path() . '/app/temp_files/' . $newFileName);
Everything works locally, but whenever I try to run the same code on my Laravel Forge server, I get below error:
unlink(/tmp/imagick-3.4.0.tgz): Operation not permitted
If I trace the error, it is in this specific line:
$this->objPHPExcel->save(storage_path() . '/app/temp_files/' . $newFileName);
As said, this code runs fine locally. The temp file $newFileName is created inside my /temp_files folder.
What am I doing wrong?
Ok so the solution to this was rather tricky. I found out that it had nothing to do with Phpspreadsheet but rather Mpdf.
The problem was that the file "imagick-3.4.0.tgz" file permission was set to read only. Mening that unlink could not work on this specific file. This goes all the way back to when I first installed the imagick library.
The solution was to go to the /tmp folder and delete the imagick-3.4.0.tgz file manually. This folder should actually be deleted when doing the imagick installation.

Why can't PHP see the text file I want to fopen() in the same directory?

All I want to do is fopen() a text file in my PHP script. It's the most important part of the module that I'm building. Unfortunately, while testing the script, I found that the script cannot see the text file that I'd like to open. I have the script in the same directory as the text file, but I keep getting met with the same error. I'm posting the script code snippet and the command line prompt/response below.
Code
$membersfile = fopen('./members.txt', 'r');
print_r($membersfile);
fclose($membersfile);
Command Line
507 elersong:~$ php /Users/elersong/Desktop/SS2014/register_member.php
Warning: fopen(./members.txt): failed to open stream: No such file
or directory in
/Users/elersong/Desktop/SS2014/register_member.php on line 11
Warning: fclose() expects parameter 1 to be resource, boolean given in
/Users/elersong/Desktop/SS2014/register_member.php on line 13
I have no clue why PHP can't see the file since it most certainly exists, and it's in the same directory as the script. Please help me make sense of this.
Some PHP setups have problems with relative parts, so try this instead:
fopen(__DIR__ . '/members.txt', 'r');
__DIR__ contains the path to the folder where the currently executing PHP file is.
Your are currently doing:
$membersfile = fopen(__DIR__ .'/members.txt', 'r');
print_r($membersfile);
fclose($membersfile);
But $membersfile contains just the handle, a number so that PHP nows which opened file you are meaning. If you print that you will just see the handle number. You need to tell pHP to read the file using:
$file = __DIR__ .'/members.txt';
$membersfile = fopen($file, 'r');
echo fread($membersfile, filesize($file))
fclose($membersfile);
or use the php shorthand function:
echo file_get_contents( __DIR__ .'/members.txt');
After trying a number of the suggestions posted here, I was finally able to figure out that the PHP script isn't using a relative path to the text file based on the directory that holds the PHP script.
User #andrew suggested that I add the following line to my script in order to learn which directory the script uses as its starting point.
echo '<pre>';var_dump(glob('*'));
From the output of this line, I was able to see that the script uses the user's root folder as the starting point in fopen() on my OSX machine. I simply needed to preface the filename with the remainder of the file path, and PHP could read it. The amended script is as follows:
$membersfile = fopen('Desktop/SS2014/members.txt', 'r');
Thanks so much, #andrew and everyone else who submitted suggestions!

Why does the file not be removed, which was created by tmpfile() of PHP, after the script ended?

The PHP Doc says, file created by tmpfile(), will automatically be removed when closed or when the script ends.
I have a script listed below:
$file = tmpfile();
$tmpFileName = array_search('uri', #array_flip(stream_get_meta_data($file)));
file_put_contents($tmpFileName, 'test');
The /tmp/phpxxxx file still exists after after running the script.
If I append this line:
fclose($file);
The temp file will be removed.
Why does this happend?
Is it because the script is not finshed? Or do I have a wrong configuration?

PHP file_put_contents works with file, no file gets "failed to open stream: No such file or directory"

The PHP file_put_contents function works perfectly fine when the file exists. If the file does not exist I receive the error "failed to open stream: No such file or directory".
$file = '../templates/stuff.xml';
if (!file_exists($file)) {$file = '../'.$file;}
$var['xhtml'] = $_POST['post_xhtml'];
$file_contents = serialize($var);
file_put_contents($file,$file_contents);
I tried the same thing with fopen and fwrite using the correct flags (w, w+ and tried the others) yet still had the same problem: if the file already existed it worked just fine, otherwise it would give me the same error message.
I know the file path is correct. I'm using Windows 7 for local development.
When the file doesn't exist, you are prepending ../ to the path, thus you are trying to write to:
../../templates/stuff.xml
Are you sure that the folder ../../templates exists (and that PHP can write to it)?
Before you write to a file, you need to check that the folder exists. Try using is_dir():
if(is_dir(dirname($file))){
file_put_contents($file, $file_contents);
}

PHP Read/Write files on remote server

I'm making a utility that provides a GUI to easy edit certain values in a csv file on a remote server. My boss wants the utility in php running on the private webserver. I'm new to php, but I was able to get the GUI file modifier working locally without issues. The final piece now is rather than the local test file I need to grab a copy of the requested file off of the remote server, edit it, and then replace the old file with the edited one. My issue is uploading and downloading the file.
When I searched for a solution I found the following:
(note in each of these I am just trying to move a test file)
$source = "http://<IP REMOTE SERVER>/index.html";
$dest = $_SERVER['DOCUMENT_ROOT']."index.html";
copy($source, $dest);
This solution ran into a permissions error.
$source ="http://<IP REMOTE SERVER>/index.html";
$destination = $_SERVER['DOCUMENT_ROOT']."newfile.html";
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
This also had a permissions error
$connection = ssh2_connect('<IP REMOTE SERVER>', 22);
ssh2_auth_password($connection, 'cahenk', '<PASSWORD>');
ssh2_scp_recv($connection, '/tmp/CHenk/CHenk.csv', 'Desktop/CHenk.csv');
This solution has the error Fatal error: Call to undefined function ssh2_connect() which I have learned is because the function is not a part of the default php installation.
In closing, is there any easy way to read/write files to the remote server through php either by changing permissions, having the php extension installed, or a different way entirely that will work. Basically I'm trying to find the solution that requires the least settings changes to the server because I am not the administrator and would have to go through a round about process of getting any changes done. If something does need to be changed instructions on doing so or a link to instructions would be greatly appreciated.
Did you set the enable-url-fopen-wrapper in your php.ini?(only if your php version is older)
Please look # php remote files storing in example 2

Categories