I am trying to change the directory with ftp_chdir and it shows me the error:
no such file or directory
But the directory is available at the server side. What am I doing wrong?
Here is my code:
<?php
#session_start();
error_reporting(0);
// define some variables
$local_file = $_GET['f'];
$server_file = $_GET['f'];
$ftp_user_name='abcd';
$ftp_user_pass='abcd';
$ftp_server='abcd';
// 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);
if (ftp_chdir($conn_id, $_SESSION['SITE_IMG_PATH']."x_directory/")) {
echo 'Directory change';
} else {
echo "Couldn't change directory\n :".ftp_pwd($conn_id);
print_r (error_get_last());
}
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "success";
} else {
echo "No";
}
// close the connection
ftp_close($conn_id);
?>
It shows me the error:
Couldn't change directory
my path is
/home/website/public_html/upload/x_directory
Related
Someone tells me. if it is possible for me to download a specific file from a remote server at my location via php?
I've tried codes and I couldn't
<?php
// define some variables
$local_file = '/httpdocs/teste.pdf';
$server_file = 'teste.pdf';
$ftp_server="209.126.127.143";
$ftp_user_name="username";
$ftp_user_pass="password";
$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);
?>
I don't think you can download a folder. Try to include filename in $server_file
ftp_get($conn_id, './teste.pdf', '/httpdocs/teste.pdf', FTP_BINARY)
Also, /httpdocs/ should be relative from your FTP user's home folder
<?php
$local_file = 'filename';
$remote_file = '/folder name/filename';
$ftp_server ='IP';
$ftp_user_name = 'NAME';
$ftp_user_pass = 'PW';
// 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);
// turn passive mode on
ftp_pasv($conn_id, true);
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
// close the connection
ftp_close($conn_id);
?>
I'm using the above script successfully on a different server with file located in the root folder. But on this one I need to fetch a file that's in a sub-directory (let's say 'folder name' for now). I've tried different things but all throw an error "failed to open stream: No such file or directory".
I've tried:
$remote_file = '/folder name/filename.csv';
$remote_file = './folder name/filename.csv';
$remote_file = '../folder name/filename.csv';
$remote_file = '/../folder name/filename.csv';
It's my first time dealing with ftp_get command. Can someone please help I'm badly stuck... don't know what I'm doing wrong here
For those who might run into the same issue, I've sorted it out by added the following lines of code (using the function ftp_chdir) and it worked fine:
// turn passive mode on
ftp_pasv($conn_id, true);
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// try to change the directory to somedir
if (ftp_chdir($conn_id, "Live Stock")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
// upload a file
if (ftp_get($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded $local_file\n";
} else {
echo "There was a problem while uploading $local_file\n";
}
I am using the below code to download using ftp
$local_file = $_GET['u'];
$server_file = $_GET['u'];
$ftp_host = '';
$ftp_user_name = '';
$ftp_user_pass = '';
$ftp_conn = ftp_connect($ftp_host) or die("Could not connect to $ftp_host");
$login = ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp_conn,true);
ftp_chdir($ftp_conn, '');
$file_list = ftp_nlist($ftp_conn, ".");
//var_dump($file_list);
//var_dump(ftp_get($conn_id, $local_file, $server_file, FTP_BINARY));
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
am getting successfully written to filename but i cant seem to find the file on my computer. Do you if i can change where the file is being downloaded to.
Pass an absolute path (instead of "myfile.txt" try "/home/user/path/to/yourfile.txt")
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.
$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');