I'm new at the whole php thing, and I need to write a file in the server (my own computer where I am developing a web app) with some config info. Here is the code I'm using:
$filename = "config.conf";
$db_info = $db_user . "<-*->" . $db_passwd . "<-*->" . $db_name;
if (file_put_contents($filename,$db_info) === false){
$ret["error"] = "Can't create/write: $filename.";
mysqli_close($con);
echo json_encode($ret);
return;
}
When I test my web app I get the create/write error I wrote. I checked and the file is not created. My working directory is in /var/www/tests however I've checked tests and my user has read and write permissions and is owner of that folder. If I create a file with a ordinary file explorer I have no problems whatsoever. Since the function is so simple, I am at a loss. Please help!
does web server has read/write permissions (e.g. nginx uses www-data user)? try setting permissions to 777 for folder where you want to create that file.
Related
I face the problem with extracting the .zip archive located under different user web/ directory. Let me explain: I have 2 websites both written on pure php. First website i use to manage Second. Every client on Second website has his own folder located in root with similar code but different configs. It looks like this:
Second website
...
/var/www/clients/client0/web2/web/client1/...
/var/www/clients/client0/web2/web/client2/...
...
Like you understand owner of this files is user: web2 and group: client0
And First website location:
...
/var/www/clients/client0/web1/web/...
...
Owner of this files is user: web1 and group: client0
What I'm trying to do is to extract zip archive at Second webroot directory while working on my backend on First website when create a new client.
Here is the code i tried(this works perfect on my localhost when user: www-data and group: www-data):
...
if (!file_exists('/var/www/clients/client0/web2/web/' . $_POST['storeid'])) {
$zip = new ZipArchive;
$dir = '/var/www/clients/client0/web2/web/';
$install_dir = $dir . 'backup/';
$res = $zip->open($install_dir . 'client_install.zip');
if ($res === TRUE) {
//Try to execute
$zip->extractTo($dir . $_POST['storeid'] . '/'); //var/www/clients/client0/web2/client2
$zip->close();
//Set permitions on new store folder
chmod_r($dir . $_POST['storeid'], 0755, 0644);
echo "\nExtracted successfully to " . $dir;
die();
} else {
echo "Failed to open zip: " . $dir . "client_install.zip" . " \n";
die();
}
}
...
At the time when i run this code i get message:
Failed to open zip:
/var/www/clients/client0/web2/web/backup/client_install.zip
I know it may be related to user permissions. How can solve this considering keeping same ISPConfig environment and user permissions.
Update:
Here is the error log message:
PHP Warning: ZipArchive::open(): open_basedir restriction in effect.
File(/var/www/clients/client0/web2/web/client1/backup/client_install.zip)
is not within the allowed path(s):(/var/www/clients/client0/web1/web:/var/www/clients/client0/web1/private:/var/www/clients/client0/web1/tmp:/var/www/test.site.com/web:/srv/www/test.site.com/web:/usr/share/php5:/usr/share/php:/tmp:/usr/share/phpmyadmin:/etc/phpmyadmin:/var/lib/phpmyadmin)
in /var/www/clients/client0/web1/web/process.php on line 261.
My question may sound stupid, so I'm sorry in advance. But can a change in the entry in a particular website of open_basedir option affect the performance of the entire server, or the inability of Apache to restart. My question is because my test website is located on a server with production sites. And I would not want to make these changes without initial consultation. So I think I need to add a new path to the open_basedir site parameter. People who know how this option works specifically, please respond to this post. Thanks in advance.
So here's the story, im not very experienced with php, and recently moved to a new host and my website worked prefectly on the old host but on the new host there are errors with the code.
The error message says :
Warning: copy() [function.copy]: Unable to access http://i.imgur.com/USlH6p2.jpg in (directory)
Heres the code where it says there is an error
function upload_image_remote($image, $name) {
$upload_dir = APP_PATH . '/image.uploads';
//check for directory rights
if(!is_writable($upload_dir)) {
echo do_error(_('Folder image.uploads is not writeable'));
exit;
}
//check if there's a directory for today uploads
$today = date("d-m-Y");
if(!is_dir($upload_dir .'/' . $today)) {
if(!mkdir($upload_dir .'/' . $today, 0777)) {
echo do_error(_(sprintf('Folder <strong>image.uploads/%s</strong> could not be created. Please check permissions to be 0777.', $today)));
exit;
}
}
$upload_path = $upload_dir .'/' . $today;
(--> this is where) return copy($image, $upload_path .'/'. $name);
}
I think it may be a permissions problem since it worked perfectly on (old host) 000webhost, anyone have any ideas on what can be wrong with the code ?
Thanks
My guess would be that the copy function is trying to access the remote file and cannot due to the PHP settings.
I think most hosting companies, for security reasons, will have allow_url_fopen = 0. This means that you will not be able to read from the remote location (http://www.site.com/foo.gif). However, you will be able to read from the local file system.
From the PHP documentation:
This option enables the URL-aware fopen wrappers that enable accessing URL object like files. Default wrappers are provided for the access of remote files using the ftp or http protocol, some extensions like zlib may register additional wrappers.
To check this, view the current PHP settings on the server by creating a file with the following contents in your web root. Your looking for the allow_url_fopen setting.
<?php
echo phpinfo();
?>
Give folder permission 0755 Set correct path of temp folder for file
system.
I did a script, which has to load a file from a ftp-server then parses it. The code relies on tempname to store the temporarily store the ftp-file. On my developement server (with php 5.3.10), this works flawlessly, however on the client machine (with php 5.2.17) it does not and gives me:
Warning: ftp_rawlist() [function.ftp-rawlist]: Unable to create temporary file. Check permissions in temporary files directory.
Can someone give me a clue what i could do?
(I am a little weak on the possibiities of php)
I used this code:
define ("LOCALFILE",tempnam('/tmp', 'data-'));
define ("USER","myusername");
define ("PASS","mypassword");
define('SERVER', "ftpserver.com");
define ("DIR","/path/");
function getFTPFile(){
// connect
if(!($conn_id = #ftp_connect(SERVER))){
Error::throwOne("Could not connect to ".SERVER);
};
// login
if(!($login_result = #ftp_login($conn_id, USER, PASS))){
Error::throwOne("LOGIN INCORRECT! user:".USER." pass:".PASS);
};
// try to change the directory to somedir
if (!ftp_chdir($conn_id, DIR)) {
Error::throwOne("Couldn't change directory\n");
};
if(!($a = ftp_rawlist($conn_id, '-1t'))){
Error::throwOne("Couldn't get ftp_rawlist\n");
};
$server_file=($a[0]);
if (!ftp_get($conn_id, LOCALFILE, $server_file, FTP_BINARY)) {
Error::throwOne("Couldn't get file\n");
};
ftp_close($conn_id);
};
The error looks to me like system-generated and have nothing to do with your tmpname. Internally, FTP stores the file in its own temp file in temp filename in standard temp directory up to the point when it's downloaded - then moves it to the location you specified.
Try putting this code before your download code:
$tmpdir = sys_get_temp_dir();
echo "Temp dir: $tmpdir\n";
echo is_writable($tmpdir) ? "Temp dir is writable" : "Temp dir is not writable";
This will tell you if you have permissions to write to the system temp dir. I further suggest that you use this $tmpdir variable instead of hardcoding "/tmp" in your code.
I found the issue.
Since my hoster does not like the /tmp directory ( who would blame them )
i need to first set
putenv('TMPDIR=/the tmp-dir my provider gave me');
which then works with tempname
I found this occurred on Windows Server 2016 - and it was because my %TEMP% environment variable pointed to a subdirectory that didn't exist for whatever reason.
It did point to the correct one when I ran it as Administrator, though.
I have Wamp (server called emerald) running and Mamp running on my Mac. People register on Mamp. Emerald is basically file hosting.
Emerald connects to Mamp's mysql database, to login users. However, I want to create a directories for new registrations on Emerald using PHP.
How can I do this? I have tried using this code:
$thisdir = "192.168.1.71";
$name = "Ryan-Hart";
if(mkdir($thisdir ."/documents/$name" , 0777))
{
echo "Directory has been created successfully...";
}
But had no luck. It basically needs to connect the other server and create a directory, in the name of the user.
I hope this is clear.
You can't create directories through http. You need a filesystem connection to the remote location (a local hard disk, or a network share for example).
The easiest way that doesn't require setting up FTP, SSH or a network share would be to put a PHP script on Emerald:
<?php
// Skipping sanitation because it's only going to be called
// from a friendly script. If "dir" is user input, you need to sanitize
$dirname = $_GET["dir"];
$secret_token = "10210343943202393403";
if ($_GET["token"] != $secret_token) die ("Access denied");
// Alternatively, you could restrict access to one IP
error_reporting(0); // Turn on to see mkdir's error messages
$success = mkdir("/home/www/htdocs/docs/".$dirname);
if ($success) echo "OK"; else echo "FAIL";
and call it from the other server:
$success = file_get_contents("http://192.168.1.71/create_script.php?token=10210343943202393403&dir=HelloWorld");
echo $success; // "OK" or "FAIL"
Create a script on another server that creates the dir and call it remotely.
Make sure you have security check (+a simple password at least)
There is no generic method to access remote server filesystems. You have to use a file transfer protocol and server software to do so. One option would be SSH, which however requires some setup.
$thisdir = "ssh2.sftp://user:pass#192.168.1.71/directory/";
On Windows you might get FTP working more easily, so using an ftp:// url as directory might work.
As last alternative you could enable WebDAV (the PUT method alone works for file transfers, not creating directories) on your WAMP webserver. (But then you probably can't use the raw PHP file functions, probably needs a wrapper class or curl to utilize it.)
I know this is old but i think this might me useful, in my experience:
if(mkdir($thisdir ."/documents/name" , 0777))
doesn't work, i need to do it:
mkdir($thisdir, 0777);
mkdir($thisdir ."/documents" , 0777);
mkdir($thisdir ."/documents/name" , 0777));
hope it helps :)
I have got the following problem since the server has safe mode turned on, and directories are being created under different users:
I upload my script to the server, it shows as belonging to 'user1'. All it is doing is making a new directory when a new user is created so it can store files in it.
New directory is created, but it belongs to 'apache' user.
'user1' and 'apache' are different users; and safe mode is turned on. So the php script cannot write to that newly created directory.
Now I have a problem!
One solution is to turn off safe mode. Also, a coworker suggested that there are settings that can be changed to ensure the directories are under the same user as the script. So I am looking to see if latter can be done.
But I have to ask. Is there a programatical solution for my problem?
I am leaning to a 'no', as safe mode was implemented to solve it at the php level. Also the actual problem may seem like the directory being created under a different user, so a programatic fix might just be a band-aid fix.
I've used this workaround:
instead of php mkdir you can create directories by FTP with proper rights.
function FtpMkdir($path, $newDir) {
$path = 'mainwebsite_html/'.$path;
$server='ftp.myserver.com'; // ftp server
$connection = ftp_connect($server); // connection
// login to ftp server
$user = "user#myserver.com";
$pass = "password";
$result = ftp_login($connection, $user, $pass);
// check if connection was made
if ((!$connection) || (!$result)) {
return false;
exit();
} else {
ftp_chdir($connection, $path); // go to destination dir
if(ftp_mkdir($connection, $newDir)) { // create directory
ftp_site($connection, "CHMOD 777 $newDir") or die("FTP SITE CMD failed.");
return $newDir;
} else {
return false;
}
ftp_close($connection); // close connection
}
}
You might be able to turn safe mode off for a specific directory via a .htaccess file (if on Apache).
php_value safe_mode = Off
You might need to get your hosting provider to make this change for you though in the httpd.conf.
I have had some success with setting the group bit of the upload directory to sticky.
PHP can then create directories inside it and write to it.
http://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories
chmod g+s directory