If i have a local file, for example, c:/test.txt, what path do i need to type in the ftp_put function to make it work (string $local_file)? When i try with "c:/test.txt" i get an error.
Thanks
Its on a remote server. Am i using wrong php function? I want to upload a local file to a remote ftp ...
Yes, you're using the wrong function.. Remember - PHP executes on the SERVER, not in your browser and not on your local machine. Any FTP connection you establish in the PHP script will be relative to the server.
e.g. If you've got something like this:
(your machine) ----> (your website) ----> (other machine you ftp to)
The FTP connection will be between "your website" and "other machine you ftp to". Any "local" path you specify for a file will be local to "your website", not "your machine".
You'd first have to upload the file via regular HTTP file sending mechanisms via a form on your site, which gets the file from "your machine" to "your server". The PHP script which handles the upload can then use the FTP functions to transfer the file from "your website" to "other machine you ftp to".
I think that if you have to use ftp to pull a file off your machine (as opposed to using a html form) your best bet would be to set your local machine up as an ftp server. You would probably need an static ip address for this to be consistent. You could then have your script connect to your local machine and use ftp_get to grab test.txt.
Related
I'm running this from Filezilla remote server
anyone have any solution
<?php
echo "what a lab";
?>
For a web browser to load the output of a PHP file you need to make an HTTP request to an HTTP server which supports PHP.
FileZilla is an FTP client. It connects to FTP servers. FireZilla, if it comes into it at all, will be used only to copy the PHP file to the computer which is running both an FTP and HTTP server.
You need to enter the matching HTTP URL (e.g. http://example.com/your.php) into the address bar of your browser.
I am working on a php web app .
I need to upload a file to the web server, with customer info - customers.csv.
but this process needs to be automated ,
The file will be generated in a Point of Sale app , and the app can open a browser window with the url ...
first i taught i would do something like this www.a.com/upload/&file=customers.csv
but read on here that is not possible,
then i taught i would set a value for the file upload field and submit form automatically after x seconds. Discovered thats not possible .
Anybody with a solution , will be appreciated .
EDIT
I have tried this and it works ,file is uploaded to remote server
is it working only because the php script is running on the same pc where csv is sitting ???
$file = 'c:\downloads\customers.csv';
$remote_file = 'customers.csv';
// set up basic connection
$conn_id = ftp_connect('host.com');
// login with username and password
$login_result = ftp_login($conn_id,'user','password');
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
This is of course not possible, imagine how this could be abused to upload on linux as example the /etc/passwd. The only way it might be possible is to use a Java Applet, but this is for sure not the best way.
You could try to let your PoS Application make a web request with the customers.csv file and let a WebAPI handle the upload, this may be possible, but I have no expierence with Point of Sale Applications.
Best might be, if the solution above cannot be considered, to just prompt the user to provide the file above and check over name + content if it is the correct one.
This is a bit tricky, but if your CSV is not too long, you could encode it in base64, send to the webserver as a GET parameter and then, in the server side, decode and store it as a CSV file.
If the file is too big to do that, you have to use other method, like the java applet pointed by #D.Schalla or even install and configure a FTP server, and make the Point of Sale app uploads the file there.
Other alternative, specially good if you cannot modify the sale app, is to install a web server in the client side and write a small php script to handle the upload process. In this way, the sale app could call a local url (something like: http:// localhost/upload.php) and it's this script the one in charge to upload the file which can be achieve with a classical HTTP POST, a FTP connection or any other way you can think about.
MY Solution , which will work with out setting up web server on client side.
This is for windows but can be adapted to linux
On client side
Local Application opens cmd and runs this command ftp -n -s:C:\test.scr
WHICH opens test.scr - a file with ftp commands e.g.
open host.com
user1
passwOrd
put C:\downloads\customers.csv public_html/customers.csv
more info here :
http://support.microsoft.com/kb/96269
more commands :
http://www.nsftools.com/tips/MSFTP.htm#put
I am currently opening and writing a text file into my local server with the following:
$mypath="sms_file\\cbsms_";
$fp = fopen($file_name.'.txt', "w");
fwrite($fp, $value. "\r\n");
fclose($fp);
I want to now copy that file to a remote server like /home/project on 10.10.18.23 (home network)
Assuming that I have R/W access in that directory, what would be the best way of achieving this?
The remote server needs to know that there is a request coming in to store a file on it. There are several possibilities here, the easiest would be to run a FTP server.
Another option would be to use the exec() function call scp on the command line (provided you have exchanged ssh keys with the remote server).
Another option would be to create a PHP page on the remote server that accepts POST requests with files and stores them. You must provide your own security measures in this case.
If you can mount the remote host as a permanent volume (via NFS or CIFS), you can use the regular PHP copy() function.
You can try using exec() to run an SCP command:
exec('scp /path/to/file.txt user#homenetworkhost:/home/project/file.txt');
// Obviously, you'll have to set up your SSH permissions and for 'user#homenetworkhost' you'll want to change it to your home network's user and host names.
By the looks of your exmample, I would say your PHP server is on Windows (looking at the backslash in $mypath="sms_file\\cbsms_";), and your remote host is UNIX/LINUX (looking at forward slashes and location /home/project). I would suggest setting up SSH or FTP on the remote host and rather use those protocols than copying it to a network location. Your PHP server (Windows box) will then have to communicate via SSH/FTP and copy the file.
References:
http://php.net/manual/en/book.ftp.php
http://php.net/manual/en/function.ssh2-scp-send.php
How to create a PHP script that would create a file.txt on the local pc through an IP address, there is a given IP address and a port which I can pass through.
How could I achieve this?
Any help will be appreciated, thank so much.
You can either, FTP it to the local machine, or simply let a person download it as a file. The second solution is the easiest, but requires someone on the local machine to initiate the download. The first solution can be automated, but requires you to set up an FTP server on the local machine.
Header will help you output as a download for the client.
http://php.net/manual/en/function.header.php
FTP will help you upload through FTP.
http://php.net/manual/en/book.ftp.php
on linux:
cat >dooda.php <<EOF
#!/usr/bin/env php
<?php
file_put_contents('file.txt',file_get_contents("http://example.com/of_some_url.txt"));
EOF
php dooda.php
I have files that are automatically uploaded onto a server from mobile phones, and I need to automatically transfer these files from the server to another server using PHP.
Could someone please explain how I would do this?
Thanks for any help
PHP has FTP functionality built in with FTP wrappers:
Allows read access to existing files and creation of new files via FTP. If the server does not support passive mode ftp, the connection will fail.
This means you can use FTP like any other file - an extremely simple example:
<?php
$data = file_get_contents('some/other/file.txt');
$fname = "ftp://name:yourpassword#127.55.41.10:21/some/path/filename.txt";
file_put_contents($fname,$data);
?>