Link to home
Create AccountLog in
Avatar of Ridgejp
RidgejpFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Failed to open stream: Permission denied in /var/www/html/export.php on line 24

Hi,

I'm playing around with a test script that exports a select query into a csv file. I'm getting the above error when I run the script (see below) and think it's related to the permission setting of my php user which is "www-data".

<?php // demo/db_to_csv.php
include("latch2.php"); 

$uid = access_control();
/**
 * Export a database table to a CSV file for use in Excel
 * https://www.experts-exchange.com/questions/29018496/Export-Data-from-MySql-Using-PHP.html
 */
error_reporting(E_ALL);
echo "<pre>";

// SET YOUR TABLE NAME HERE - OR MAYBE USE THE URL "GET" ARGUMENT
$table_name = 'productCode';

// OPEN THE CSV FILE - PUT YOUR FAVORITE NAME HERE
$csv
= 'storage'
. DIRECTORY_SEPARATOR
. 'EXPORT_'
. date('Ymdhis')
. "_$table_name"
. '.csv'
;
$fp  = fopen($csv, 'w');
if (!$fp) trigger_error("UNABLE TO OPEN $csv", E_USER_ERROR);

// GET THE COLUMN NAMES
$sql = "SHOW COLUMNS FROM $table_name";
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}
if ($res->num_rows == 0)
{
    trigger_error("ERROR: $table_name HAS NO COLUMNS", E_USER_ERROR);
}
else
{
    while ($show_columns = $res->fetch_object())
    {
        $my_columns[] = $show_columns->Field;
    }
}

// WRITE THE COLUMN NAMES TO THE CSV
if (!fputcsv($fp, $my_columns)) trigger_error("FAILURE WRITING COLUMN NAMES TO $csv", E_USER_ERROR);

// GET THE ROWS OF DATA
$sql = "SELECT * FROM $table_name";
if (!$res = $mysqli->query($sql))
{
    $err
    = 'QUERY FAILURE:'
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    . ' QUERY: '
    . $sql
    ;
    trigger_error($err, E_USER_ERROR);
}

// ITERATE OVER THE DATA SET
while ($row = $res->fetch_row())
{
    // WRITE THE COMMA-SEPARATED VALUES.
    if (!fputcsv($fp, $row)) trigger_error("FAILURE WRITING DATA TO $csv", E_USER_ERROR);
}

// ALL DONE
fclose($fp);

// SHOW THE CLIENT A LINK
echo "<p><a href=\"$csv\">$csv</a></p>" . PHP_EOL;

Open in new window


What settings should I adjust to allow the script to run, without compromising the security of the site?

J
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Usually this means that the folder you're trying to write into ("storage") is owned by one user and PHP is running as a different user, and the folder permissions aren't set up to allow anyone but the owner to write to it.

PHP is probably running as the web server user so you can either change the owner of the storage folder to match the web server / PHP user or you can change the permissions on the folder to allow the user to write to the file (e.g. change group owner to the web server user group and allow group members to have write permissions).
The quick fix is to change your storage folder permissions to 1777 but it's better if you can do 770 and fix group ownership of the folder.
Avatar of Ridgejp

ASKER

I used this command to change the ownership of the user:group and the script now runs ...

sudo chown -R www-data:www-data storage

Open in new window


Is that a problem?

Ps... apologies for the delay in replying been away on a short break.

J
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Ridgejp

ASKER

Thank you.