FTP Uploader using php - php

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);

Related

No such file or directory in

I am doing an application for android and i need to upload to the server the profile picture of user i try to do it by ftp , but wen i send the path from android app of this picture it give me an error , and i dont understand much about path from android to web , what i do wrong?
Also forgot to coment i do a random strind with uniqid(); for the name of the file uploaded.
This is my error:
ftp_put(/storage/emulated/0/Download/red-bull-2384130__480.png): failed to open stream: No such file or directory in /home/nicetaxi/public_html/mobilapp/uploadfile.php on line 18
This is my code
<?php
$ftp_server="ftp.......";
$ftp_user_name="......";
$ftp_user_pass=".......";
$Random_str = uniqid();
$file = $_POST["path"];
echo $file;
$remote_file = $Random_str . ".jpg";
// 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_put(/storage/emulated/0/Download/red-bull-2384130__480.png): failed to open stream: No such file or directory in /home/nicetaxi/public_html/mobilapp/uploadfile.php on line 18
Your server is on the internet and you posted a file path for a file residing on your Android device.
Sorry, but that server connot access files on your Android device.
It does not even try as php thinks its a path on the server itself.
Well then it will not find it.
Instead you should post the file itself to to php server.

Upload files with PHP on FTP server (WinSCP works)

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"].

How to get HTTP URL of file uploaded to FTP server

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);

Storing files in a subdomain/custom folder using the Storage function in Laravel

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);
?>

FTP file upload using php

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

Categories