I want to upload a file on a FTP Server (I use WinSCP). But I don't know how use ftp_put.
$file = 'somefile.txt';
$remote_file = 'readme.txt';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
ftp_close($conn_id);
I receive the file with a form ($_FILES). In $file, maybe $_FILES['myFile']['name'] but I don't know what to put in $ftp_server and $remote_file. The server http://10.XX.X.XX/myProject/ and my file, in WinSCP are in : /var/www/myProject/
Maybe the hostname is XXXX#xxxxx
It works ! Thanks. The problem was the path, the good is : /myProject/
$ftp_server should be the host name you use, when connecting with WinSCP, like "example.com".
$remote_file should be "/var/www/myProject/" . $_FILES['myFile']['name'].
$file should be $_FILES["myFile"]["tmp_name"].
Related
Below is my code to upload file to other server ,
$ftp_server="host";
$ftp_user_name="";
$ftp_user_pass="";
$file = "referesh.php";//tobe uploaded
$name = "diff_adtaggeneration";
$remote_file = $name."/referesh.php";
// 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);
$res = ftp_rawlist($conn_id, $name);
//print_r($_SERVER);exit;
if(count($res) > 0)
{
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
else
{
ftp_mkdir($conn_id, $name);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
ftp_close($conn_id);
above code works perfectly, the file succesfully uploaded to other server, the new folder name 'diff_adtaggeneration' will create in root of the directory and file uploaded in that folder, and I need to get the uploaded file path ..! I use print_r($_SERVER) , I know this shows only current server details, but how to get the root directory full file path of uploaded server(other server). Any help appreciated...
There's no magical way to find out an HTTP URL of the file uploaded to an FTP server, that happens to share a file storage with the HTTP server.
It's a matter of configuration of both the FTP and HTTP servers; how do they map virtual HTTP and FTP paths to and from an actual file system paths.
The mapping can be completely virtual and undeterministic.
Having that said, most web hostings have some root both on FTP and HTTP, from where the mapping is deteministics.
If you connect to the FTP, you usually land in the root folder of the HTTP hosting, or immediately below it (there would be a subfolder like httpdocs or www that maps to the HTTP root).
If the FTP account is chrooted, the root folder will be / (or /httpdocs, etc).
If you have your own domain (www.example.com), then an FTP path like /index.html maps to https://www.example.com/index.html.
That's the most simple case. It can be a way more complicated. But that's something we cannot help you with. That's a question for your web hosting provider and/or administrator.
Using ftp_pwd you can achieve to this kind of a level. But will be relative to (may be public_html). If you want to access the file using http add the file to a http exposed directory.
$ftp_server="host";
$ftp_user_name="";
$ftp_user_pass="";
$file = "referesh.php";//tobe uploaded
$name = "diff_adtaggeneration";
$remote_file = $name."/referesh.php";
// 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);
$res = ftp_rawlist($conn_id, $name);
//print_r($_SERVER);exit;
if(count($res) > 0)
{
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
else
{
ftp_mkdir($conn_id, $name);
ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
}
$remotePath = ftp_pwd($conn_id)."/".$remote_file; ///public_html/name/refresh.php
ftp_close($conn_id);
I'm trying at the moment to upload files onto a subdomain, hosted on the same server, while the Laravel application is hosted on the main domain. Is there any way to use the Storage functions with the subdomain? Or anything similar?
You can use FTP to transfer file from your server to another server.
First, the receiver must have ftp account.
Second, you can use this below code to transfer your file [source].
<?php
$file = 'somefile.txt';
$remote_file = 'readme.txt';
// 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);
// 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);
?>
I've made a form that is waiting for a file. Once it's submitted, the file is uploaded to a remote server through the ftp_put() function. The problem is that it take something like 2/3 minuts.
I tried removing lines one by one to find out what's wrong and it's the call to ftp_put() that is taking so much time. The file is only 20Ko. Is there a way to upload the file in an other thread or something ?
EDIT :
Here is my php code.
$file = $_FILES['attachment']['tmp_name'];
$ext = pathinfo($_FILES['attachment']['name'], PATHINFO_EXTENSION);
$remote_file = '/cv_'.$_POST['first_name'].'_'.$_POST['last_name'].'_'.strval(time()).'.'.$ext;
$ftp_server = 'xxxxxxxxx';
$ftp_user_name = 'xxxxxx';
$ftp_user_pass = 'xxxxxx';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (!ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) /* This line is taking so long */
{
echo "There was a problem uploading the file $file\n";
}
ftp_close($conn_id);
EDIT 2 : The file is correctly uploaded after a few minuts. The code is working fine and fast on localhost, but when it's on the production server, it starts taking a long time.
Solved it enabling passive mode.
I need to upload a file automatically from a local computer to a remote server. I have found the following code on here:
<?php
require_once('ftp.php');
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
ftp.php is my file with the ftp authentication information. The connection works but I am getting the following error:
There was a problem while uploading C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv
EDIT: I amnot sure if this makes a difference or not, but here are my $remote_file and my $file:
$file = "C:/xampp/htdocs/testbcos/accounting/checkslastmonth.csv";//tobe uploaded
$remote_file = "/home/bookcell/public_html/testbcos/accounting/checkslastmonth3.csv";
What am I doing wrong here? Also, is it possible to do this if the file is on a mapped drive on my local server?
Thanks.
First thing: Try to set passive mode. You need it if you're sitting behind a firewall. (What probably is the case)
ftp_pasv($conn_id, true); // after ftp_login
Second, you have to change to dir first:
ftp_chdir($conn_id, '/home/bookcell/public_html/testbcos/accounting/');
ftp_put($conn_id, 'checkslastmonth3.csv', $file, FTP_ASCII);
If you want to know what's really going on, try to get error message with error_get_last() or
$php_errormsg
I am making a ftp image upload using jquery and php i am using ajax to send the data. I have the following code but its not working i am getting the following error.
Error:
Warning: ftp_put() [function.ftp-put]: httpdocs/user_images/: Not a regular file in /var/www/vhosts/kbba.biz/httpdocs/admin/php/upload.php on line 21
This is the tmp_name: /tmp/phpQbG3el
Code:
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_put($conn_id, "httpdocs/user_images/", $_FILES['fileToUpload']['tmp_name'], FTP_BINARY);
print_r($_FILES['fileToUpload']['tmp_name']);
ftp_close($conn_id);
You need to add a file name to the remote path like so:
ftp_put($conn_id, "httpdocs/user_images/" . $_FILES['fileToUpload']['name'], $_FILES['fileToUpload']['tmp_name'], FTP_BINARY);
This would be the outcome if I upload myfile.txt
ftp_put($conn_id, "httpdocs/user_images/myfile.txt","/tmp/phpQbG3el", FTP_BINARY);