Uploading image to FTP using PHP - php

I have tried files upload to server using ftp connection in php and its not working, its connecting but getting Error like "Connected to XXXXXXXXXXX, for user XXXXXXXXXXXXX FTP upload has failed!" I have tried following code please help by correcting it,..
image.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$destination_file = "imagetest/123/".$_FILES['file']['name'];
$source_file = $_FILES['file']['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've tested your code and had a bit of a hard time to get it working, but what worked for me was to use something like /http_docs, or /public_html as the base/root folder.
Root folder names will vary from hosting services, so modify accordingly.
I.e. and with a few modifications:
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$folder = "/http_docs/imagetest/123/";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// rest of code
Sidenote:
Do not use a full path name.
I.e.: /var/user/you/public_html/ it won't work.

Related

send file with post, then upload with ftp

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

Unable to connect a server with FTP and PHP

I am unable to connect to a FTP server when i am connecting it through our PHP code. Most embarrassing thing is that i get only false when trying to connect server with ftp_connect() method. No errors are reported. This is my first time when i am working with FTP in PHP. if anyone who have previously worked on it can help me out here. Here is my code :
/* Source File Name and Path */
$remote_file = $_SERVER['DOCUMENT_ROOT'] ."/some-folder/file.txt";
/* New file name and path for this file */
$ftp_host = 'ftp://targetserver.com/some folder/';
$ftp_user_name = 'user';
$ftp_user_pass = 'XXXXX';
/* New file name and path for this file */
$local_file = 'ftp://targetserver.com/some-folder/file.txt';
/* Connect using basic FTP */
$connect_it = ftp_connect( $ftp_host, 21);
var_dump($connect_it);
/* Login to FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
/* Download $remote_file and save to $local_file */
if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
/* Close the connection */
ftp_close( $connect_it );
Actually getting false means that your ftp client couldn't connect to the server for some reason
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
The best to do before you dive into php debugging is to try out another ftp client such as https://filezilla-project.org/ and see if all works fine, if not you will have saved your time because the issue not coming from your php script.
UPDATE 1
Then try this:
$ftp_host = 'targetserver.com';
http://php.net/manual/en/function.ftp-connect.php#refsect1-function.ftp-connect-parameters
The FTP server address. This parameter shouldn't have any trailing slashes and shouldn't be prefixed with ftp://.
And as for local_file and remote_file, they must be paths and not urls
http://php.net/manual/en/function.ftp-get.php#refsect1-function.ftp-get-examples
As ftp_connect function doesn't produce an error, I assume the extension is installed.
It is very possible this could be a firewall issue with port 21 blocked. (This is a very common reason)
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
<?php
$ftp_server = "94.23.x.xxx";
$ftp_username = "anxxxxx";
$ftp_password = "6xxxxxxxx";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
// login
if (#ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "conectd as $ftp_username#$ftp_server\n";
}
else
{
echo "could not connect as $ftp_username\n";
}
$file = $_FILES["uploadedfile"]["name"];
$remote_file_path = "/JustForTest".$file;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
Then what's the problem with the above code?

Upload files to remote server using PHP

I’m a novice in php and html, and I found some codes that allowed me to upload files to a remote server using a html form and a php code.
I tested the code locally using wamp and I found some bugs, like I cannot overwrite an existing file and the formatting of the name of the file is not taken into consideration.
The files that I would like to upload are named in French and have spaces between them, the server is a debian server.
The goal of this code is to retrieve a URL.
The HTML code:
<form enctype="multipart/form-data" action="remote_upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
The PHP code:
$ftp_server = "192.168.1.111";
$ftp_user_name = "";
$ftp_user_pass = "";
$remote_dir = "C:\wamp\www";
// 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);
//default values
$file_url = "";
if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);
$file = $_FILES["uploadedfile"]["tmp_name"];
$remote_file = $_FILES["uploadedfile"]["name"];
$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}
if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}

How to upload a file via ftp in PHP?

I have a form with html browse type and a submit button. I chose a file using browse button and submit the form. On form submission following code is called.
$conn_id="myid";
$conn_id = ftp_connect ( 'server' );
$ftp_user_name="username";
$ftp_user_pass="password";
// 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!" ;
exit;
} else {
echo "Connected to for user $ftp_user_name" ;
}
// upload the file
$upload = ftp_put( $conn_id, "images/signatures/" . $fileName , $_FILES['tmp_name'] , FTP_BINARY );
// check upload status
if(!$upload){
echo "FTP upload has failed!" ;
} else {
echo "Successfully Uploaded." ;
}
But it produce the following warning:
Warning: ftp_put(): Filename cannot be empty in /var/www/echdp/_ProviderSignature.php on line 70 FTP upload has failed!
But when I hard code the source path in above code then it upload the file on server:
$upload = ftp_put( $conn_id, "images/signatures/myfile.txt" , "/var/www/images/hello.txt" , FTP_BINARY );
Try this:
$file = 'somefile.txt';
$remote_file = '/public_html/dir/dir2/dir3/somefile.txt';
$ftp_server = "yourdomain.in";
$ftp_user_name = "ftpusername";
$ftp_user_pass = "ftppassword";
// 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);
Your file will be uploaded in the /public_html/dir/dir2/dir3/ directory of your domain.
Caution: Never upload an index file when you are testing this feature.
Source: PHP.net
Example of html code with enctype:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="myfile" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
Do you have enctype in Form tag? Without the attribute your file won't be send.
For debugging of $_FILES array. Try
var_dump($_FILES);
to see if the data are correct.
Your variable:
$_FILES['tmp_name']
is used wrongly because $_FILES contain files not a one file. Therefore you should write:
$_FILES['your-name-in-html-form']['tmp_name'] // your-name-in-html-form = myfile in the example above
Have a look on the example: http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/
$_FILES['tmp_name']
isn't name of your file. It is stored in:
$_FILES['inputname']['tmp_name']
where inputname is name of your field. Also check your form for enctype ="multipart/form-data".
Be aware of simply putting on your server everything that user can send - if there is access to uploaded files by http (like http://yoursite/images/signatures/uploadedfile.xxx) someone can put .php files on your server and execute it !
Obviously $_FILES['tmp_name'] does not contain the filename. Try print_r($_FILES) to see what it contains. Also, use multipart/form-data as your form enctype.

php ftp upload problem

I am trying to write a small php function that will upload files to an FTP server and I keep getting the same error but I cannot find any fix by googling the problem, I am hoping you guys can help me here...
The error I get is: Warning: ftp_put() [function.ftp-put]: Unable to build data connection: No route to host in .
The file was created at the FTP server but it is zero bytes.
Here is the code:
<?php
$file = "test.dat";
$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";
$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}
$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($cid);
?>
I forgot to put FTP in passive mode using:
ftp_pasv($cid, true);

Categories