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.
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 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?
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.
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;
}
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.