Ridgejp
asked on
Export Data from MySql Using PHP
Hi,
How do I export data to a CSV from MySql using PHP?
J
How do I export data to a CSV from MySql using PHP?
J
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why not just use something like PHPMyAdmin or Adminer to export the data?
ASKER
Hi Ray,
Just having a play now ... and getting the following error: -
I presume that this is because the directory html is owned by root not myself. Is the answer to simply change the ownership of the directory using:
Or does need to originate from further down on VAR and are there any implications security wise for this?
J
Just having a play now ... and getting the following error: -
Warning: fopen(storage/EXPORT_20170427021410_productCode.csv): failed to open stream: Permission denied in /var/www/html/export.php on line 24
I presume that this is because the directory html is owned by root not myself. Is the answer to simply change the ownership of the directory using:
sudo chown -R ridgejp html
Or does need to originate from further down on VAR and are there any implications security wise for this?
J
I think the directory you need to address is storage/ and it needs to exist before the export.
My permissions are rwx r-x r-x, or hex 7-5-5
My permissions are rwx r-x r-x, or hex 7-5-5
ASKER
Done that.
var/www/html/storage exists and the ownership is me - it looks like this: -
var/www/html/storage exists and the ownership is me - it looks like this: -
drwxr-xr-x 2 ridgejp ridgejp 4096 Apr 27 14:11 storage
It might need to be owned by "nobody" -- that is often the identity of PHP. Whatever identity PHP uses should be the owner. I'm not exactly sure how to find out who PHP is running as, but I'm fairly sure you can use mkdir() to create a directory. Then if you check the owner of the new directory, you may be able to see who PHP is.
ASKER
When I use mkdir() in terminal to create a directory I use the sudo command so it shows as root.
Php pages that I have created show as ridgejp.
Php pages that I have created show as ridgejp.
Hmm... Try something like this - just put the script into the web root directory and run the script from a regular web browser and see what you get.
<?php
/**
*
*/
error_reporting(E_ALL);
// A DIRECTORY TO STORE OUR FILE
$stor = 'whatever_name_you_want_here';
if (!is_dir($stor)) mkdir($stor);
ASKER
On it ...
ASKER
Got this ...
Warning: mkdir(): Permission denied in /var/www/html/test.php on line 9
Warning: mkdir(): Permission denied in /var/www/html/test.php on line 9
Then apparently PHP does not have permission to make directories. I couldn't really function without that permission!
ASKER
Is it a setting I need to change?
ASKER
Ok, thanks for checking I'll post a separate question in the meantime.
ASKER
Thanks Ray!
ASKER