What is a simple PHP File download script? - php

I only just bought a web domain yesterday(cryptum.net) and I intend to upload some files to it. My problem is I don't have a good PHP5 download script. I just want a simple script that will save the file to the users download folder. The files are located on the websites ftp server. Here is what I have so far-
<?php
$conn = ftp_connect("ftp://username#cryptum.net") or die("Could not connect");
ftp_login($conn,"username","password");
//download script goes here
ftp_close($conn);
?>

Another very elegant solution which uses the powerful stream features of PHP:
<?php
$ftp_server = 'ftp://username:password#cryptum.net/';
$remote_file = 'user/directory/file';
$local_file = '/path/to/the/local/file';
file_put_contents($local_file, file_get_contents($ftp_server.$destination_file));

<?php
//...
//download script goes here
ftp_get ($conn, '/destination/on/my/hardDrive', '/file/to/download');
//...
?>
and you could use php-curl instead...

Related

Does using FTP URL Protocol Wrapper equals using FTP for copying file?

I'm trying to create FTP Process that output its progress.
I found this answer rather simple on Getting ftp_put progress
by Martin Prikryl
<?php
$local_path = "\Local\Path\Local_File.zip";
$remote_path = 'ftp://username:password#address/Remote_File.zip';
$size = filesize($local_path);
$hin = fopen($local_path, "rb") or die("Cannot open source file");
$hout = fopen($remote_path, "wb") or die("Cannot open destination file");
while (!feof($hin))
{
$buf = fread($hin, 10240);
fwrite($hout, $buf);
echo "\r".intval(ftell($hin)/$size*100)."%";
}
fclose($hin);
fclose($hout);
This code is working.
What I want to ask is, if I were to use FTP URL Protocol Wrapper (ftp://) as the URL. Does it copy file using the FTP? Or is it just copying with a normal PHP Command?
Because I was asked to create an FTP process for my project, but I need the progress when it is copying.
Thanks before :)
FTP URL wrapper uses FTP protocol under the hood. That's why they are called "FTP".
I have no idea, what you mean by "copying with a normal PHP Command". If your only interface to the server is FTP, and the wrapper were using anything else than FTP, then the wrapper would obviously fail. If it works, it only proves that it uses FTP.
Documentation for FTP URL protocol wrapper says:
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.

php FTP file: How can check if a file is open by another process

I have a list of videos (.mp4) in a remote server.
From my application codeigniter, I connect with FTP to the remote server and I can list those videos.
I want to rename those files, but I should verify if file is opened by another process (video is playing) before renaming it.
How can check if a file is opened by another process with php?
This my code:
$ftp = new Simple_ftp();
$ftp->init("server",'login','password');
$conn_id = $ftp->connexion();
if($conn_id == 3){
$files = $ftp->ls('path_files');
foreach($files as $file){
...
}
}
You cannot check for that having only FTP access.
If the server is using Linux based OS and you have ssh access, you can remotely connect and use the lsof program.
There is no way of knowing by FTP, however other methods I suggest taking a look at the following: flock
It checks for file locking

How do I make this script copy files to local disk?

I have copied and tried many PHP scripts from SO posts. I am trying to download files from a server running Centos. Via psftp (putty) I can login manually and copy files. But I want to automate the process, hence the need for a script.
On a similar server running on Windows am able to download files by ftp via a simple Perl script. On the Centos server I get connection refused with the Perl script. So I tried several php scripts. Are the scripts below (from SO posts) for the job? or what is wrong with the scripts?
script 1
#!/usr/bin/php
<?php
include('Net/SSH2.php');
$sftp = new Net_SFTP('xx.xx.xxx.xxx');
if (!$sftp->login('myuser', 'mypasswd')) {
exit('Login Failed');
}
// outputs the contents of filename.remote to the screen
echo $sftp->get('gateway_data*');
?>
Script 2
#!/usr/bin/php
<?php
include('Net/SSH2.php');
username='myuser';
password='mypasswd';
// Create SCP connection using a username and password
$scp = new SCP(
'xx.xx.xxx.xxx',
new SSH2Password($username, $password)
);
#################################
$sftp = ssh2_sftp($conn);
// Create a new local folder
ssh2_sftp_mkdir($sftp, './data');
// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
################################################################
?>
In the first of PHP script you have posted you're doing echo $sftp->get('gateway_data*'); whereas in the Perl script you're doing cp gateway_data_301.txt. Try doing that in the PHP script. eg. echo $sftp->get('gateway_data_301.txt');.
As is it is unclear what you're expecting to happen. Unless the file name /actually/ has a wild card in it then are you expecting it to download every file that starts off with gateway_data* and just concatenate them in the output? Personally, I think just returning false or NULL would be better than that.
You can use your script 2 in PHP. However something is missing there. You are only opening the source directory. You must write a loop over all files in that folder.
// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
foreach ($files as $key => $value) {
See the example of how to send a file with SFTP using SFTPConnection.

Can you unzip a .zip on a remote server?

I have a script unzips files on the server but is it possible to unzip files on an external or remote server.
For instance can I go
<?php
$zip = new ZipArchive;
$zip->open('ftp://user:password#host/path/file.zip');
$zip->extractTo('ftp://user:password#host/');
$zip->close();
echo "Ok!"; ?>
Thanks a lot
to unzip a remote file on a server with PHP a rather simple solution that worked for me is:
ftp the zip file, say a.zip, to the remote folder where you want it extracted
create a php file unzip.php and ftp it to the same folder as the zip file above
insert the following code into unzip.php:
<?php
$zip = new ZipArchive;
$zip->open('a.zip');
$zip->extractTo('x/');
$zip->close();
echo "Ok!";
?>
set chmod of unzip.php to executable
execute the php file from any browser - you shd get an 'Ok' confirming the extraction
You could try the following via an ssh connection to that server:
<?php
$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'unzip /path/to/file.zip');
?>
More info here:
http://www.php.net/manual/en/function.ssh2-exec.php

What is the best way to move files from one server to another with PHP?

I want to setup a CRON that runs a PHP script that in turn moves XML file (holding non-sensitive information) from one server to another.
I have been given the proper username/password, and want to use SFTP protocol. The jobs will run daily. There is the potential that one server is Linux and the other is Windows. Both are on different networks.
What is the best way to move that file?
If both servers would be on Linux you could use rsync for any kind of files (php, xml, html, binary, etc). Even if one of them will be Windows there are rsync ports to Windows.
Why not try using PHP's FTP functions?
Then you could do something like:
// open some file for reading
$file = 'somefile.txt';
$fp = fopen($file, 'r');
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
Why not use shell_exec and scp?
<?php
$output = shell_exec('scp file1.txt dvader#deathstar.com:somedir');
echo "<pre>$output</pre>";
?>
I had some similar situation.
After some tries, I did some thing different
We have 2 servers,
a (that have the original files)
b (files should moved to it)
And for sure the data is NOT sensitive
Now in server a I made a file to do the following when called:
1. Choose the file to move
2. Zip the file
3. Print the .zip file location
4. Delete the .zip file (and the original file) if delete parameter passes
In the server b the file should do:
1. Call the file on the a server
2. Download the zip file
3. Unzip and copy it to the proper location
4. Call the delete function on server a
This way I have more control on my functions, tests and operations!

Categories