Can I create/write on a file on another host & domain with file_put_contents() OR fwrite()?
If I can, what permissions and other property should set on that host?
Thanks ..
check this out from http://www.php.net/manual/en/function.file-put-contents.php#101408
To upload file from your localhost to any FTP server.
pease note 'ftp_chdir' has been used instead of putting direct remote file path....in ftp_put ...remoth file should be only file name
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($conn_id, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>
I wrote a function similar to PHP file_put_contents() which is writing to a FTP server:
function ftp_file_put_contents($remote_file, $file_string)
{
// FTP login
$ftp_server="my-ftp-server.com";
$ftp_user_name="my-ftp-username";
$ftp_user_pass="my-ftp-password";
// Create temporary file
$local_file=fopen('php://temp', 'r+');
fwrite($local_file, $file_string);
rewind($local_file);
// Create FTP connection
$ftp_conn=ftp_connect($ftp_server);
// FTP login
#$login_result=ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
// FTP upload
if($login_result) $upload_result=ftp_fput($ftp_conn, $remote_file, $local_file, FTP_ASCII);
// Error handling
if(!$login_result or !$upload_result)
{
echo('FTP error: The file could not be written on the remote server.');
}
// Close FTP connection
ftp_close($ftp_conn);
// Close file handle
fclose($local_file);
}
// Usage
ftp_file_put_contents('my-file.txt', 'This string will be written to the remote file.');
If you want to use file_put_contents specifically, you have to use a stream context for a protocol the remote server accepts for uploading. For instance, if the server is configured to allow PUT requests, you could create an HTTP context and send the appropriate method and content to the server. Another option would be to setup an FTPs context.
There is an example in the comments for file_put_contents on how to use it with a stream context for FTP. Note that the used ftp://user:pass#host URI scheme is transmitting the user credentials in cleartext.
Additional examples
Related
This is a very basic question since I do not properly understand the underlying concepts of the different protocols. Please be indulgent.
I can upload files to my site with FTP using Filezilla. Now I'm developing a user interface for sending data to update a database and files (photos) to be saved in a directory. I send this data with POST (see here). I get the file, but cannot save it to the destination directory performing
move_uploaded_file($_FILES['PhotoToUpload']['tmp_name'],"../../Photos/".$file_name);
I think is because of permissions (currently 755). Since I can currently upload with Filezilla FTP (so I understand I have the permissions to do that),
my question is:
Is bad practice (or conceptually wrong) to take the temp file created after the POST and load it with a FTP code like the following (taken from here)?
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "../../Photos/";
$source_file = $_FILES['PhotoToUpload']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
I have a web form that I will be hosting on a centOS web server. The code that I have written, will take the data that has been entered, and save it to an excel spreadsheet to a specific location. i also have a file server, this is where I would like to have the file saved to. this is the first time that i have done a server to server file save/copy/transfer. on the web server, would i have to install an ftp client to accomplish this? When testing the program locally on my computer, this is how i currently have the destination path set up.
file_put_contents('C:\Users\username\Box Sync\Driver Check In\Shortage'.
$month. '\Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
with the code being hosted on the web server, how can i change the destination path to point to my file server?
Here is a sample script of how to upload file from your localhost to any FTP server.
Source: PHP Manual
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($resource, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>
I am trying to uplaod a zip file using php in ftp connection. only the zip is uploading but there is no folders or file in the zip folder.
Here is the code which i m trying:
$host = 'Your Ftp host';
$usr = 'Your Ftp User Name';
$pwd = 'Your Ftp password';
// file to move:
$local_file = 'C:\wamp\www\demo.zip';
$ftp_path = '/demo.zip';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id,$ftp_path,$local_file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
/*
** Chmod the file (just as example)
*/
// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
// try to chmod the new file to 666 (writeable)
if (ftp_chmod($conn_id, 0644, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 644\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
Please provide me a solution, m trying to do this from past 3 days..
If the file appears on the server but the size/content of the file is not correct, it may be a problem with your transfer mode (FTP_ASCII or FTP_BINARY).
Try replacing your put line with:
// perform file upload
$upload = ftp_put($conn_id,$ftp_path,$local_file, FTP_BINARY);
What I'm trying to do is automaticly upload a zip-file through FTP using PHP, and then extract the zip-file as well. However first things first: I'm having trouble with uploading the zip-file using PHP's ftp_put function. This is my script:
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $user, $pass); //<!--same as cPanel account user and pass?
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $user";
exit;
} else {
echo "Connected to $ftp_server, for user $user";
}
// server & file info
$file = 'phpBB3.zip';
$ftp_root = '/public_html/';
$site_root = $_SERVER['DOCUMENT_ROOT'].'/scripts/';
// >>>HERE<<<
// upload the file
// >>>HERE<<<
$upload = ftp_put($conn_id, $ftp_root .$file, $site_root . $file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $file to $ftp_server as $file";
}
// unzip the uploaded file (from FTP?)
$unzip = shell_exec("unzip {$file}"); //<!-- how to do this through FTP uploaded files?
// close the FTP stream
ftp_close($conn_id);
What's happening here is that it (SOMETIMES) uploads A PART of the file ( always the same amount; like 900 kB ) and sometimes doesn't upload anything at all. It's as if the uploading process is interrupted by the rest of the script being executed while the uploading wasn't finished yet. Though I'm not sure if that's the cause of the problem.
Though, it always gives me this error:
Warning: ftp_put() [function.ftp-put]:
Connecting to port 38694 in
/home/quicksit/public_html/createacct.php
on line 93
Where the output port is always different and always larger than 20.000 ( like 30.000-50.000 ).
Could anyone help me out on this?
Thanks in advanced,
Skyfe.
There is a timeout and upload limit in the configuration file for PHP... you may want to check the setting there.
<?php
ini_set('max_execution_time', '0');
$host = '234.546.155.485';
$usr = 'fgfgfgdf';
$pwd = 'fghghh';
// file to move:
$file = 'http://vsomesite.com/file.flv';
$ftp_path = '/public_html/video57242/test.flv';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
// ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
?>
upload fails when the file is remote not local. what problem?
Your variables don't add up. You use $local_file in your ftp_put() call, but you only declare $file earlier in the script
And are you sure you should be using FTP_ASCII for a FLV file? I'd have thought that was binary.
Edit
Scratch my first point. Seems you edited it away :)