How can I upload images with ftp in PHP? - php

I tried to upload images to ftp server, but I can't connect to it.
a.html:
<html>
<body>
<form method="post" action="b.php" enctype="multipart/form-data">
<input id="file" name="file" type="file">
<input value="upload" name="submit" type="submit">
</form>
</body>
</html>
and b.php:
<?php
$host = 'ftp.uw.hu';
$usr = 'myname';
$pwd = 'mypass';
$local_file = $_FILES['file'];
$ftp_path = '/a/'.$_FILES['file'].'(2)';
$conn_id = ftp_connect($host) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
?>
I always get "Cannot connect to host" error message, but I don't know why.
I tried another ftp server (ftp.atw.hu) but I got the same error.

http://php.net/manual/en/function.ftp-put.php
This may help you!!! Refer it!!!

Related

PHP ftp_put warning filename cannot be empty

I have a home FTP server. I made a little file uploader for that.
index.php:
<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>
upload_file.php:
<?php
$ftp_server = "192.168.0.11";
$ftp_username = "";
$ftp_password = "";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("Pogreska tijekom spajanja.");
// login
if (#ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "Spojen kao $ftp_username#$ftp_server\n";
}
else
{
echo "Pogreska tijekom spajanja $ftp_username\n";
}
$file = $_FILES["uploadedfile"]["name"];
$remote_file_path = "/KUCNI_FTP-506D-ADD5/".$file;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"], FTP_ASCII);
echo "\n\nconnection closed";
?>
And I get this error:
Warning: ftp_put(): Filename cannot be empty in C:\xampp\htdocs\ftp\upload_file.php on line 22
I tried to fix it, but it is not working. Please help me.
EDIT(And sorry for a little bit of Croatian...)
Have a look at MAX_FILE_SIZE, that's an option which is often ignored.
Make sure the file you're trying to upload is within the limits of what you previously defined.

FTP & PHP File Uploader, change directory

so i'm trying to make a ftp php uploader on my localhost setup with xampp.
It works fine to upload files from C:\xampp\htdocs\ but if I try to upload a file from my desktop it wont work. is there a way to get the path of the "source" file so I can upload from other places than ...\htdocs\ or how can I fix this?
Files are uploaded to a ftp folder on my desktop, and as I said that works fine when you chose a file from C:\xampp\htdocs.
<?php
if($_POST){
$source = basename($_FILES["source"]["name"]);
$fileType = pathinfo($source, PATHINFO_EXTENSION);
$remote_file = $source;
$ftp_server = '127.0.0.1';
$ftp_user_name = 'xxx';
$ftp_user_pass = 'xxx';
$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, $source, FTP_ASCII)) {
echo "successfully uploaded $source\n";
} else {
echo "There was a problem while uploading $source\n";
}
ftp_close($conn_id);
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" id="source" name="source"></input><br>
<input type="submit" value="Upload file" name="submit" id="submit"></input><br>
</form>
</body>
</html>
Your using the name. To access the file that was uploaded you need to use tmp_name. Change $source = basename($_FILES["source"]["name"]); to $source = basename($_FILES["source"]["tmp_name"]);

Images are not storing in FTP

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,..
<!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 = "XXXX";
$ftp_user_name = "XXXXX";
$ftp_user_pass = "XXXXXXXXXX";
$destination_file = "./imagetest/123/";
$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);
?>
You have to add filename after path.
Replace below code and try,
$destination_file = "./imagetest/123/".$_FILES['file']['name'];
$destination_file is not a file it is a directory.
Also you need to change directory to upload file.
Eg.
<?php
ftp_chdir($conn, $destination_directory);
ftp_put($conn, $filename , $source_file, FTP_BINARY );
?>

file uploading using ftp_put [duplicate]

This question already has an answer here:
PHP Warning: ftp_fput(): Can't open that file: Is a directory in
(1 answer)
Closed 2 years ago.
I have solved my original question below. What is the best method if I need to insert a progress bar for the file uploader? Using the ftp_put method, doesn't allow to get a response from the server until the file is successfully uploaded.
Thanks!
I'm trying to upload a file using ftp_put and for some reason when I run the code through localhost, it gives me the below error:
Warning: ftp_put(): Can't open that file: Is a directory in C:\xampp\htdocs\ftp\upload_file.php on line 25
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input name="uploadedfile" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
PHP Code:
<?php
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$destination_file = "/public_html/testing/";
$source_file = $_FILES['uploadedfile']['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);
?>
Thank you.
Please test chdir that destination or look at this on php.net
Got this cryptic error
Warning: ftp_put() [function.ftp-put]: 'STOR' not understood in
C:\wamp\www\kevtest\ftp_todays.php on line 48
Found the prob, you can't put a path to the destination file
(even though I can do that in the dos ftp client...?)
e.g. - this doesn't work
ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);
you have to put
<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>
<?php
if(isset($_POST['submit']))
{
ini_set('max_execution_time', 60000);
$file = $_FILES['video']['tmp_name'];
$destination_file = "/public_html/".time().basename($_FILES['video']['name']);
// connect and login to FTP server
$ftp_server = ".com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, 'name', 'pass');
// upload file
if (ftp_put($ftp_conn, $destination_file, $file, FTP_BINARY))
{ echo "Successfully uploaded ".$_FILES['video']['name']." "; }
else
{ echo "Error uploading $file."; }
// close connection
ftp_close($ftp_conn);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<div class="container">
<form name="frm" id="frm" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="file" name="video" required="required" />
</div>
<div class="input-group">
<input type="submit" name="submit" value="Submit" onClick="move()"/>
</div>
</form>
</div>
</body>
</html>

FTP upload via PHP form

I want to upload a file via FTP upload in a form.
<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>
Here is the PHP file:
<?php
$ftp_server = "xxx";
$ftp_username = "xxx";
$ftp_password = "xxx";
// 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["file"]["name"];
$remote_file_path = "/home/www/lifestyle69/import/".$file;
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
The FTP connection connects successfully but the file is nowhere.
Can anybody help me?
Thanks!
Because you have <input name="uploadedfile" type="file" />:
$file = $_FILES["file"]["name"]; // wrong
$file = $_FILES["uploadedfile"]["name"]; // right
Because you need the filename of the temporary copy stored by PHP, which exists on the server:
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII); // wrong
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
FTP_ASCII); // right
Refer to the PHP documentation for more information about $_FILES.
Are you sure that the folder you are uploading to has the correct permissions? Try chmoding it to 777 and see if that works.
The file is stored on server with temporary name, so when you try uploading $_FILES['file']['name'], it fails, because file with such name does not exist. Instead you should call ftp_put() with $_FILES['file']['tmp_name']
It's explained a little better here

Categories