i have tried to upload the image into the server but i could not have the permission to access the folder so i have used the FTP connection. Through the android app i have encode the image and sent it to the server. In the server side, i just decode it and tried to upload it. But i could not able to upload it. Can you please help me on this.
<?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['filename'];
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$destination_file = "/upload/images/".time().jpg";
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
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,$conn_id";
}
$upload = ftp_put($conn_id, $destination_file, $binary, FTP_BINARY);
if (!$upload) {
echo "FTP upload has failed! $upload";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($conn_id);
?>
The third param of the function ftp_put expects a filename. You are passing the image binary as a string.
Change tis line:
$upload = ftp_put($conn_id, $destination_file, $binary, FTP_BINARY);
To:
$fileName = uniqid().'.jpg';
$filePath = '/tmp/'.$uniqid;
file_put_contents($filePath, $binary);
$upload = ftp_put($conn_id, $destination_file, $filePath, FTP_BINARY);
please add ftp_pasv($conn_id, true); after successful login. Then only this will work.
Related
I am doing a system of upload the archives and extract for ftp but as php dont have function "ftp_unzip", I must do an archive to extract inside host.
<?php
//Upload the image file to the server
$ftp_server = 'luis.habboproject.net';
$ftp_user_name= 'login';
$ftp_user_pass = 'senha';
echo $_FILES['userfile']['name'];
$source_file= "archive.zip";
$destination_file="/public_html/archive.zip";
$source_file2= "oi.php";
$destination_file2="/public_html/oi.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);
// 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";
}
// turn passive mode on
ftp_pasv($conn_id, true);
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
$upload2 = ftp_put($conn_id, $destination_file2, $source_file2, FTP_BINARY);
// check upload status
if (!$upload) {
} else{
// Code to Acting a page http://luis.habboproject.net/oi.php to extract the archive.zip in a host
}
// close the FTP stream
ftp_close($conn_id);
?>
I want to know if there is some function in php or javascript, that have same result of a "submit button", but without click. In other words, I want to execute the archive to active the extractor.
I am trying to upload a captured image from android device to remote server via PHP - ftp_put() function. The Android code is working, I just do not get it work from php side.
Explanation: On Android/Java side I encode bitmap to string and decode it in PHP ($binary). I want to upload this bitmap to remote server. Please see my PHP code:
<?php
if ($tag == 'upload') {
//Get data from client
$uid = $_POST['uid'];
$base = $_POST["image"];
$date = date('Y-m-d-H-m-s');
if (isset($base)) {
//function creates random numbers and stores in $suffix
$suffix = $db->createRandomID();
//Prepare image name
$image_name_ = "img_".$suffix."".$uid."_".date("Y-m-d-H-m-s").".jpg";
// base64 encoded utf-8 string
$binary = base64_decode($base);
// binary, utf-8 bytes
header("Content-Type: bitmap; charset=utf-8");
//Basic ftp information
$ftp_server = "xxx.xxx.com";
$ftp_user_name = "xxxxx";
$ftp_user_pass = "xxxxx";
$file = $binary;
$remote_file = "/public_html/uploads/".$image_name_;
// 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)) {
$response["success"] = "Upload";
echo json_encode($response);
} else {
$response["error"] = "failed";
}
// close the connection
ftp_close($conn_id);
}
?>
LogCat says: No such file or directory ... Line if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII))
I'm curious how to upload file through FTP using PHP. Let's say I have SQL function and it will return a filename (dynamic name) and upload file using the return of filename. how can i do this?
i have try before and try in command prompt. but the error is "PHP Warning: ftp_put : failed to open stream: no such file or directory in ...."
my code :
<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");
/$query = pg_query($db, "select lookup_cell();");
$arr = pg_fetch_array($query, 0, PGSQL_NUM);
$file = $arr[0].'.csv';
$remote_file = '/nury/'; // <-- my directory on server
$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Passive mode
// ftp_pasv ($conn_id, true);
$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
//var_dump($ftp); die();
// upload a file
if ($ftp) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Remove the comment for Passive mode and then try once it will work.
<?php
$db = pg_connect("host=localhost port=5432 dbname=automationReporting user=postgres password=admin") or die("gagal konek.");
/$query = pg_query($db, "select lookup_cell();");
$arr = pg_fetch_array($query, 0, PGSQL_NUM);
$file = $arr[0].'.csv';
$remote_file = '/nury/'; // <-- my directory on server
$ftp_server = '192.168.1.128';
$ftp_user_name = 'polban';
$ftp_user_pass = 'polban2014';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// Passive mode
ftp_pasv ($conn_id, true);
$ftp = ftp_put($conn_id, $remote_file, $file, FTP_ASCII);
//var_dump($ftp); die();
// upload a file
if ($ftp) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
$fp = 'test.png';
$server_file = "dir/testing.png";
//-- Connection Settings
$ftp_server = 'ftp.net';; // Address of FTP server.
$ftp_user_name ='user'; // Username
$ftp_user_pass = 'password'; // Password
// set up basic connection
$conn_id = ftp_connect($ftp_server,21);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
$mode = ftp_pasv($conn_id, TRUE);
$download=ftp_get($conn_id, $fp, $server_file, FTP_BINARY);
// try to download $server_file and save to $local_file
if ( $download) {
echo "Successfully $fp\n";
} else {
echo "There was a problem\n";
}
ftp_close($conn_id);
When I run this it says it is successfully, but will not write or download the file.
If I echo file_get_contents($fp); then it will display the image deconstructed into text [in the Web browser]
In need to download images and video...
Try adding
header('Content-Type: image/png');
How can I save an image over FTP? Here's my code:
$conn_id = ftp_connect('***');
$login_result = ftp_login($conn_id, '***','***');
if( $login_result) echo 'connected';
$save = "FTP://temp/". $FileName;
imagepng($this->Picture, $save);
/*if (ftp_put($conn_id,$save,$save, FTP_ASCII))
echo "successfully uploaded \n";
else
echo "There was a problem while uploading \n";
*/
You could do:
ob_start();
imagepng($this->Picture);
$image = ob_get_clean();
$stream = stream_context_create(array('ftp' => array('overwrite' => true)));
file_put_contents("ftp://user:pass#host/folder/".$FileName, $image, 0, $stream);
Hope this helps you
<?
//Configuration
$ftp_server = "ftpaddress";
$ftp_user = "username";
$ftp_pass = "password";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
//trying to connect with login details
if (#ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "Connected! ";
} else {
echo "Couldn't connect!";
}
//You can change it with the file name, this is for if you're upload using form.
$myName = $_POST['name']; //This will copy the text into a variable
$myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored.
?>
<?PHP
$destination_path = "dest/path/";
//your desired path for uploading)
$destination_file = $destination_path."img.jpg";
//This will create a full path with the file on the end for you to use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.
$file = $myFile['tmp_name'];
//Converts the array into a new string containing the path name on the server where your file is.
$upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
if (!$upload) {// check upload status
echo "FTP upload of $destination_file has failed!";
} else {
echo "Uploaded $file to $conn_id as $destination_file";
}
?>