FTP file upload in php - php

I have a windows server from which i wish to transfer some images to my FTP server. below is my code
$file = "docs/1.png";
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = '';
$destination_file = "1.png";
// 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);
// 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";
}
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
// upload the file
// ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 180);
ftp_chdir($conn_id, "example/");
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
// ftp_put($conn_id, $destination_file, $file, FTP_BINARY);
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);
// check upload statu
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 getting **
ftp_put(): Opening BINARY mode data connection
** this error and file not uploading.

Related

Image losing quality after upload to FTP server using PHP

I made a script to upload an image to an FTP server but the image is losing quality - and seems like it only has 256 pixels
My HTML:
<form action="upload_image_travel.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" accept="image/jpeg"/>Upload foto in folderul travel/imagini/ pentru ARTICOL
<input name="submit" type="submit" value="Upload File in TRAVEL" />
</form>
And my upload.php:
$ftp_server = "xxxxx";
$ftp_user_name = "xxxxx";
$ftp_user_pass = "xxxxx";
$destination_file = "/public_html/travel/imagini/" . $_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
if (ftp_put($conn_id, $destination_file, $source_file, FTP_ASCII)) {
echo "successfully uploaded $destination_file\n";
header('Location: administrator/admin_index_travel.php');
} else {
echo "There was a problem while uploading $destination_file\n";
}
// close the FTP stream
ftp_close($conn_id);
maybe it should be FTP_BINARY? not ascii?

Uploade image to FTP via php code error

I have problem with my image uploade, it doesnt work...
give me this error
Connected to ftp.exemple.com, for user exemple#exemple.com Warning:
ftp_put(): Filename cannot be empty in
/home/user/public_html/foto-test.php on line 26 FTP upload has
failed!
<?php
$ftp_server = "ftp.xxxx.com";
$ftp_user_name = "xxxx#xxxx.com";
$ftp_user_pass = "xxxxxxx";
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILE['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); //line 26
// 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 tried to change the $folder destination but it give me the some error...
Check out the answers here PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in
Also your destination path should have a slash at the end
$folder = "/public_html/images";
$destination_file = $folder . $_FILES['file']['name'];
change that to
$folder = "/public_html/images/";
$destination_file = $folder . $_FILES['file']['name'];
So it will be clear that whatever(image) you uploading is going into the image folder and not 'image+SomeName' folder

PHP - Upload input="file" image to ftp directory and display direct link of image

I'm trying to make this sort of very simple image-hosting website.
I'm using the code below to upload the images from the input="file" to my ftp directory. The next step is to display the direct image link (eg: www.example.com/123.png) right after uploading on a different page.
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "/public_html/my/directory/";
$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 have no idea how to do this. I checked everywhere and couldn't find a place that explains how to do this.

ftp_put says file is uploaded successfully but there is no file found

So the ftp_put says the file is successfully uploaded, but if i check the server there is no file found, i have checked the chmod permissions and they are 777.
here is the message i get when i uploaded a file:
conectd as a9408563#server23.000webhost.com Het bestand is succesvol geupload(the file has been successfully uploaded). connection closed
and this is my code:
<?php
$ftp_server = "xxxx";
$ftp_username = "xxx";
$ftp_password = "exxx";
$uploadedfile = $_FILES["uploadedfile"]["tmp_name"];
$destination_path = "/public_html/files";
//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";
}
ftp_pasv($conn_id, true);
/*
$remote_file_path = " /home/a9408563/public_html/files".$uploadedfile;
ftp_put($conn_id, $remote_file_path, $uploadedfile,
FTP_BINARY);
ftp_chdir($conn_id, '/public_html/files/');
ftp_put($conn_id, $destination_path.$_FILES["uploadedfile"]["name"], $_FILES['uploadedfile']['tmp_name'], FTP_BINARY );
*/
if (ftp_nb_put($conn_id, $destination_path.$_FILES["uploadedfile"]["name"], $_FILES['uploadedfile']['tmp_name'], FTP_BINARY))
{
echo "Het bestand is succesvol geupload.";
}
else
{
echo "Het bestand is niet geupload.";
}
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
ftp_nb_put is more flexible than ftp_put, but also more complicated to check its result. A simple if(ftp_nb_put()) call isn't enough.
Try using ftp_put() instead, or learn how to check for ftp_nb_put's success in the manual:
// Initiate the Upload
$ret = ftp_nb_put($my_connection, "test.remote", "test.local", FTP_BINARY);
while ($ret == FTP_MOREDATA) {
// Do whatever you want
echo ".";
// Continue uploading...
$ret = ftp_nb_continue($my_connection);
}
if ($ret != FTP_FINISHED) {
echo "There was an error uploading the file...";
exit(1);
}

Downloading a file via ftp using php

I'm using ftp_put() to download a file from another server. Everything (connection etc.) working but it wont download.
ftp_chdir($conn_id, $destination_file);
$upload = ftp_put($conn_id, $name, $source_file, FTP_BINARY);
above is my code and its giving error "Can't change directory to"...
when i out ftp_put without 'ftp_chdir' it didn't work so i used ftp_chdir. but still not working. $destination_file equal to a path and $name equal to a file. Please give me an idea what a i doing wrong?
p.s even $upload is returning true. But i can't find the file anywhere.
Use ftp_get not ftp_put
<?php
// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';
// 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);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
ftp_get from PHP manual
This is my code,
$source_file="http://example.com/ex/ex2/".$file_name; and $destination_file is equal to a path.
$conn_id = ftp_connect($ftp_server);
// 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
//ftp_chdir($conn_id, $destination_file);
$upload = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Downloaded $source_file";
}
// close the FTP stream
ftp_close($conn_id);*/

Categories