How to update/replace php script on server - php

I am working on update feature for a CMS. But I stuck on this. The scenario is user will able to upload a zip file, the updater script will extract it and replace the old one.
I have a problem with replacing, I've done the following
<?php rename($old, $new);
I always get "Permission Denied"
using ftp_rename:
<?php
$conn = ftp_connect($host);
ftp_login($conn, 'user', 'pass');
ftp_rename($conn, $src, $dest);
I always get
Warning: ftp_rename(): Rename Failed. on....
Is there a proper way to do this ? thanks.

Depending on your linux distro, php actually executes command as a specific user. Check your apache (or whatever server you are using) settings and put the permission accordingly
For example in apache, it is set
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
You can then change that upload folder to be owned by www-data
For more information you can read this useful discussion
https://unix.stackexchange.com/questions/30879/what-user-should-apache-and-php-be-running-as-what-permissions-should-var-www

Related

How to get file from SFTP server

I've been working at this for the last 2 days to no avail. I've also searched on StackOverflow for a solution but I couldn't find one that works for me.
I'm trying to pull a .csv file from a SFTP server. I found out you cannot do this with a default installation of PHP. I found 2 solutions.
1) Enable the ssh2_sftp extension in my PHP. I couldn't get this to work. I downloaded the required files, put them in my php/ext folder as directed, and modified the line in php.ini as required. Wouldn't work.
2) Use phpseclib. Couldn't get this to work as you need to use composer with it and composer wont load my php.ini because I have curl enabled?
Are there any other solutions for logging into a sftp server?
Appreciate the help.
Since it sounds like you have root access on the machine you're working with, why not use scp? It's a native php function so long as you have shell_exec enabled, and the user www-data (or whatever you call your httpd user) has shell access.
<?php
$connection = ssh2_connect('your_remote_server', 22); //remote connection
ssh2_auth_password($connection, 'username', 'password'); //authentication
ssh2_scp_recv($connection, '/remoteServer/whatever/yourFile.csv', '/localServer/yourNewFilename.csv'); //remote file -> local file
?>
NOTE
I have used this, but I also store the "username" and "password" in a MySQL database using bcrypt with a minimum cost of 12. More about that Here

PHP connection to remote mysql with ssl works via command but not in browser

I want to connect to a remote (AWS) mysql server using ssl in PHP.
My script works when I execute it via command line, but doesn't when I call it from the browser.
$con=mysqli_init();
mysqli_ssl_set($con,NULL,NULL,"path/to/cacert.pem",NULL,NULL);
$link = mysqli_real_connect($con, "host", "username", "password");
I am using php7/Apache/CentOs. I tried changing the ownership and permissions of the CA file, and noticed that it requires read permission the be executed on console. But in browser even if I give full permission to everybody (chmod 777) it still doesn't work.
The error i get is:
Warning: failed loading cafile stream.
When I check existence of file it returns true, but when I check is_readible, then also error.
Can somebody help?Thanks!
So as I figured there was (is) something wrong with readability and maybe permissions. I could narrow down the problem to the certificate file being existent but not readable. I moved it to my server with filezilla via ftp.
I could solve my problem by creating a new .pem file and simply copying the content of my original file into it. This one is readable now and works in browser, but I can't figure out why as they both have the same chmod xxx permissions and chown ownership.
Detailed description(for users with similar problem using AWS MYSQL):
open rds-combined-ca-bundle.pem file(download link: https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem) in text editor.
Copy content.
On your server, where php script should run create new file and paste text into it.(it might require some additional editing, begin/end tags in separate line and new lines end at the same place as in original text)

dbase_open error on deleting rows from remote file

I hope you can help me with my problem.
I have a local site that reads data from DBF files on a terminal within the network. So what I did, I shared the DBF files on a PC running Windows XP, with full read/write permission.
When the PHP script is run on the Ubuntu server, it will first ping the PC on the network. After confirming that it is connected, it will mount the shared files within the server. Please note that the directory /mnt has 777 permissions and it is set to subdirectories as well. The owner is also www-data which is the user for PHP.
The script is able to read the data from the DBF files using this setup. I use the below script to read the data from the dbf file:
$db1 = dbase_open("/mnt/test.dbf", 0) or die(mysql_error());
for($counter=1;$counter<=dbase_numrecords($db1);$counter++){
$dbf_data = dbase_get_record_with_names($db1, $counter) or die(mysql_error());
// insert data to MYSQL database
}
The script above works, but only for reading the data. When I change to:
dbase_open("/mnt/test.dbf", 2)
it should have read and write access, However it does not let me remove the rows that were already read. I use:
for($count=1;$count<=dbase_numrecords($db1);$count++){
dbase_delete_record($db1, $count);
}
dbase_pack($db1);
but it doesnt work. I also tried to put dbase_pack inside the loop, but still the same. Here is a screenshot of the error:
Hope you can help. Thanks in advance!

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

Trouble with writing to file with php and my home server

So, I have this simple little php script. It runs and compiles fine and works the way I want it to on the machine that I coded it. I'm running it on a personal home web-server running Debian 6.0.6, 32bit. It's apache with php. And I know for a fact that php is working on the server.
<?php
$hitsfile = "hits.txt"; #name of file
$filehandle = fopen($hitsfile, 'r') or die ("Couldn't read file."); #Opens file, 'hitsfile' to be read.
$hits = fread($filehandle, 5); #reads file to the introduced variable, 'hits'
fclose($filehandle); #closes file
$hits++; #increments the variable that it read.
$filehandle = fopen($hitsfile, 'w') or die ("Couldn't write to file."); #opens file to be read.
fwrite($filehandle, $hits); #writes the hits variable to file.
fclose($filehandle); #closes file.
echo $hits; #outputs the hits variable.
?>
When I access the file from the server, via a web browser, I get the "Couldn't write to file." error. So then, it's opening the file properly, and reading it. And when it opens it to write, it fails. I'm assuming this is some sort of problem with permissions or something. I'm sort of at a loss as to how to solve the issue. Any ideas? Assistance would be greatly appreciated! I've googled for a couple days now, and I can't solve the issue. I'm a php 'noob' and I'm very new to running a linux-based web-server, but hey, you gotta learn somehow. :*l
tried to check the permissions to the file? The Linux file system have a very strict permission system. Write on terminal:
ls -la /path/to/my/file.txt
This would give you your permissions on the left column. Please read this article to be sure, and check if Apache have the "write" permissions to the file. If not, use the chmod command to give Apache access to the file (or the chown command, to change the owner of this file to apache, if the owner of this file have writing permissions).

Categories