PHP FTP fwrite() to Windows server not working? - php

I am attempting to fwrite() via an FTP wrapper from a UNIX-based server to a Windows-based server with PHP. I am connecting successfully, and am able to (for example) create directories. However, I can't seem to write files to the folders! When I look at the directory permissions using an FTP client, I noticed that they are all 0000, and cannot be changed. Apparently, Windows-based servers do not use the same FTP permission system.
So... what's up? What do you recommend? I am able to upload files to the directory using an FTP client, but when I attempt to write files with PHP, nothing happens.
I am at a complete loss as to why this may be occurring. I have confirmed my script works by writing to UNIX-based servers, so that is not a problem. Is it possible that the fact the destination server does not have PHP installed matters? I would not think so, but I'm open to any ideas at this point!
Thank you!
EDIT - What's really getting me is that I AM able to create directories, so it doesn't make any sense that writing shouldn't be working. On Windows servers, is there some setting that would prevent just the writing of FILES, but not folders?
EDIT 2 - More research has told me that, though you cannot CHMOD on Windows servers, PHP's CHMOD still somehow does something with the permissions. However, this does not appear to be working for me. Is there a way to change the permissions directly with PHP code, or is this something that has to be done directly on the server, outside of my reach?

I recommend file_put_contents, and make sure PHP is allowed to write in that directory.

Related

How do you manage your PHP and SQL file once uploaded to a server?

First of all I apologize for the dumb question. I'm new with all this. I already searched an answer on Google, but I couldn't find anything comprehensible for me.
So, I managed to create my html, CSS, JavaScript files. I managed to install XAMPP, create SQL databases, access and update them with PHP from my html page.
Now I have full access to my files, since they're on my computer. But how does it work when I move them to a server?
First, what files should I move exactly? All the XAMPP folder? Just html, css, js and PHP?
Second, once uploaded the files, how can I access them, for updating, or for viewing the database? Can I still use XAMPP or after uploading the files to a server XAMPP becomes useless?
Thank you a lot.
First of all if you want to upload file to a online server. there are many tools which can allow you upload them. In my Case i use filezila.
This is how it is presented:
you will move everything located under xamp/htdocs/YOUR_PROJECT and move it on the server ( in my case under public_html folder). Html, php, css, ...
You can delete, rename or modify them within filezila at server file section as described in the picture. Right click on a file > all actions will be seen
for viewing the database check in Admin panet of your host website in my case hostinger and on Databases choose for example phpmyadmin and you will be redirected to you Database panel to view all your databases
I think this helped.
Your hosting provider presumably already has the equivalent of your XAMPP stack installed. (Unless you're starting with an unmanaged VPserver, in which case you'll have to install and configure an Apache-MySQL-PHP stack before your website will function.) Assuming a standard hosting solution, you will only need to upload the files specific to your project.
Basic steps to deploy from local to remote server:
Using an FTP client of your choice, upload your PHP, HTML, CSS, JS, image etc. files to your server's public_html folder. (Full path will be something like /home/example.com/public_html/ on a Linux server, depending on your hosting provider.)
Use a database manager (such as PHPMyAdmin) to upload the SQL file into a database you've created.
Update any absolute paths you may have used in your PHP files, reflecting the live/production server's filesystem structure.
Update your database-connecting PHP file(s) with the appropriate credentials. (They're probably different from your local server's user/password.)
You will want to ensure that your remote server's configuration supports all the functionality your code depends on (e.g. PHP modules and other configuration).
If you put a bit of thought into your code, you won't have to maintain separate local/remote files, and won't have to bother with steps 3 & 4. You can e.g. define your configuration based on a local IP address check (we're testing for both IPv4/6), as follows:
if (in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
// local config here
} else {
// remote config here
}
Then, does your local server become useless? If you don't ever intend to develop code again -- yes. Otherwise, you will probably want to develop and test your code on your local machine, and only deploy it to your live/production server when you're satisfied it's stable enough for public release. You don't want to do your R&D on the live server!

Write code locally, send to server

I'm trying to configure my web server and I'm a complete beginner. Now everything is working quite fine. I want to write some php files. But I only have unix command line access to the server and vim or nano are not really convenient for writing lines of codes.
So the question is :
- Is there a unix command line php editor i can "apt-get install" on my debian server ?
- Is there a proper way of writing files on my computer and then commit them to the server ?
Here I'm looking for an easy solution... As I don't have much knowledge about the subject !
Thanks !
Yes, typically whoever is hosting your server will give you some kind of facility for uploading for files: FTP, SCP, etc. If someone else is running your server for you (e.g., a web host) you should contact them to find out what options you have. If you are administrating over the server yourself, you will need to install some kind of file transfer server on it. FTP would be the obvious choice, but not necessarily the best, especially if you have to connect to it over the public internet (FTP is not encrypted by default, so your file contents and even your login details will be sent in clear text over the network). If you are using SSH (e.g., PuTTY) to connect to your server, you should be able to use pscp to copy files securely.
But for what it's worth, Vim is super convenient for writing lines of code, once you know how to use it. Vim is a programmer's text editor, it's got loads of features that make writing lines of code easy.

How to upload a site to a server where folders are writable for php

This problem occurred to me multiple times now, and it's time for me to do it the right way!
How can I upload a website to the server, where php has access to the folders for writing data.
Usually I use an FTP program, but I can't upload as root, so there are restriction problems all over the place...
How do you do stuff like this?
Thanks!
EDIT
I'm sorry, I accidentally added rails to the tags instead off php.
Probably I need to clarify my problem, since the answers didn't really help me out here:
I already have a server running apache, DirectAdmin and some other stuff like rails.
And the problem is when I upload a website like joomla or wordpress via FTP the restrictions always need to be set to 777/775 or these sites can't write to the folders..
So what I need to know is:
How can I upload these sites (via FTP/SSH) as a user (root) that is the same as php, so that php can create files in all folders it needs to write to?
Hope I'm being more clear now, thanks for the help so far!
Use a server with ssh access and full write access to wherever your Rails app is hosted (and usually ssh access is as the user that Rails runs as).
For me this usually means a VPS type server, I like Rackspace Cloud which turns out to be around $11 - $15 per month for a low traffic, low spec server. I've also heard good things about Linode
The solution
Upload your site with FTP
SSH to the server and go to the public_html folder
chown -R [user_name]:[group_name] [folder_name]
For me the right user was apache..

php creating files that cannot be deleted

When I download a file with curl through php I cannot seem to be able to delete it afterwards through ftp. I can delete it through the php script, but that's not exactly perfect. If the file isn't downloaded via curl, but still via php I can delete the file, it's just ones downloaded via curl that I cannot delete. When I try to run chown() through php on the file it gives me a permissions error. I've tested the same php script on multiple other servers and it works fine there, it's just this particular one it doesn't work on. Maybe it has something to do with php configuration and permissions but I'm not 100% on that.
Sounds like it is saved with the file owner being the user account of the web server. A non-privileged account can't chown to a different user, either, so that explains why chown fails... Try having PHP execute chmod 777 on the file before you delete it.
When you create a file it is usually owned by the Apache user (or whatever app server you use). The FTP user however is not the same one most of the time. You can fix this by adding the FTP user to the Apache group (or the other way around). Sometimes they already share a group (like on many plesk environments) so making files readable and writeable for that shared group may solve the issue.

Creating a file on another server using PHP file functionality

I have a script on my one server and I want that script to create a file on another server of mine using PHP, NOT VIA FTP?
There are many ways to do this. I'd pick the first one myself because it's easiest to set up:
If you have PHP+Apache on another server, just call some script on the other server using file_get_contents with http URL as filename or use cURL if you need to POST file contents as well.
If the servers are in same network (LAN, VPN) you can use Windows shares/Samba or NFS to mount a remote directory to you local filesystem and simply write to file directly using fopen/fwrite functions
Use SSH via SCP or SFTP
PHP allows sending files across SSH - see the ssh2* family of functions, in particular ssh2_scp_send and ssh2_scp_recv.
I've never used them myself, but the infrastructure is there in Linux, just like SMB in Windows.
In general, FTP is the only regularly and easily available way (in PHP) to create a file on another server.
There are of course other protocols that enable you to create a file, but they all require installation of software on either one or both servers:
Samba (would enable access to the remote server through an absolute file path)
WebDaV (PHP client libraries available)
SCP (Finding a PHP client is probably going to be hard)
If both servers run PHP, it's probably the easiest to set up a PHP script on the remote server that accepts file data trough POST, and writes it out to a local file. Not a perfect solution, though, due to the limits usually imposed on POST uploads.
You could always use DAV, but it might require some configuration on the receiving server. There is also SSHFS, which lets you easily mount the remote directory locally over a SSH tunnel, or just use the ssh2_* family of functions as Andy Shellam suggested.
Really, there are lots of ways to accomplish this.

Categories