Can someone figure out what this is not working? I kept trying different things, double checking syntax etc but I get the error message I have setup for when a file does not transfer. This is my first PHP script.
I appreciate any help I will do research in the meantime.
Using NetBeans
<?php
$user = "username";
$password = "password";
$server_file = 'mag_hounddog.mp3';
$local_file = 'C:\users\kazuiman\desktop\mag_hounddog.mp3';
$ftp_server = "ftp.mysite.com";
// setup a basic connection to the ftp server
$connectionID = ftp_connect($ftp_server) or die("Connection was Not Successful");
// login with username and password for your account
$loginResult = ftp_login($connectionID, $user, $password);
echo ftp_pwd($connectionID);
// changes the directory to the tvstation you can hard code one in here.
if (ftp_chdir($connectionID, "test1nbiiwcdp_apps/tv2beta/streams/_definst_/")) {
echo "Current Directory is Now..." . ftp_pwd($connectionID) . "\n";
} else {
echo "Error Changing Directory .. perhaps it doesn't exist?";
}
// Now to download the files and put them onto your local machine
if (ftp_get($connectionID, $local_file, $remote_file, FTP_BINARY)) {
echo "Succesfully written $server_file to $local_file\n";
} else {
echo "There was a problem with the FTP transfer please check script\n";
}
// close the connection
ftp_close($connectionID)
?>
You have never defined the variable $remote_file.
In your preamble it is called $server_file.
Is this a copy-paste-error?
Related
In my php code below, i am trying to upload a file. I have adapted this code from one of the posts on the subject. The file i have to upload has a serial number as part of the name, which is incremented everyday. So I use the glob function to put the file name in an array and loop through the array. The script works but the file created on the server is empty. I have seen a response to my question, which is to use
ftp_pasv($conn_id, true);
But I do have this in the script, it does not work for me. I hope someone can assist me locate what the problem is.
I am working in ubuntu 18.04, also connecting to a unix machine.
As an extra question, how would I delete the local file after transfer.
Thanks for understanding.
The code is below.
#!/usr/bin/php
<?php
//define some variables
$ftp_server='server-address';
//set up basic connection
$conn_id = ftp_connect($ftp_server);
$ftp_user_name="myuser";
$ftp_user_pass="mypasswd";
$remote_path = "/";
$local_path="./";
chdir($local_path);
$local_file = glob("$local_path/OBS*");
$arrlength=count($local_file);
$cur_dir= getcwd();
echo $cur_dir . "\n";
//exit;
//login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
print_r(ftp_nlist($conn_id,"."));
for($x=0;$x<$arrlength;$x++){
//upload $local_file and save to $remote_path
if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_ASCII)){
echo "Successfully written to $remote_path)\n";
}
else {
echo "There was a problem\n";
}
}
//close the connection
ftp_close($conn_id);
?>
try like this
if(ftp_put($conn_id, "$local_path/$local_file[$x]", $remote_path, FTP_BINARY)){
echo "Successfully written to $remote_path)\n";
}
I am trying to upload image from one server to another server through FTP by using PHP.But the uploaded data shows.pureftpdupload.5809ed2f.15.7b36.24316ca6 error.
I am using this code.
$connection = 'servername';
$username = 'xxxx';
$password = 'yyyy';
$local_file = 'http://servername/test.jpg';
$remote_file = 'admin/files/company/test.jpg';
$connection = ftp_connect($server);
if (#ftp_login($connection, $username, $password)) {
// successfully connected
//echo 'connected';exit;
} else {
echo 'not connected';
return false;
}
if (ftp_put($connection, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded\n";
} else {
echo "There was a problem while uploading \n";
}
My guess is that you want to upload a local file from a web server. But by mistake or misunderstanding, you are using file's HTTP URL (http://servername/test.jpg) instead of file's local path.
Try to use a real local path, like /home/user/test.jpg.
This may work as a generic solution: $_SERVER['DOCUMENT_ROOT']."/test.jpg"
I would like to copy a file (eg. MYFILE.csv) from FTP 'SOURCE' to FTP 'TARGET'. What type of command would you recommend for a script in php?
I have tried this but it didn't work.
<?php
$server = 'ftp.TARGET.com' ;//address of ftp server
$user_name = 'USER_TARGET'; // Username
$password = 'PASSWORD_TARGET'; // Password
$source = 'MYFILE.csv';
$dest = '/in/MYFILE.csv';
$mode='FTP_ASCII';
// set up basic connection
$connection = ftp_connect($server) ;
// login with username and password
$login_result = ftp_login($server, $user_name, $password);
// upload a file
if (ftp_put($connection, $dest, $source, $mode)) {
echo "successfully uploaded $source\n";
} else {
echo "There was a problem while uploading $source\n";
}
// close the connection
ftp_close($connection);
?>
=> The php script will be hosted in a folder on FTP A.
Thank you for your guidance.
You can use file_put_contents
http://php.net/manual/en/function.file-put-contents.php
file_put_contents('ftp://user:pass#server/path/to/file.txt', $data);
It will return false on a failure:
if(file_put_contents('ftp://user:pass#server/path/to/file.txt', $data)) {
// ftp upload successful
} else {
// ftp upload failed
}
Why does your current method fail though?
I'm generating a .csv file from a mysql query and the file is correctly saved on the local server where the creating query is. The next step would be to take that new file and upload it to a different FTP location (completely different server). I've tried almost everything, but I still can't get the ftp_put to work.
Here is the part of my code in question:
//Creating the file
$file = fopen(dirname(__FILE__) . "/local_export.csv","w");
$csv_line = array("item1","item2");
fputcsv($file,$csv_line);
fclose($file);
$ftp_server = "00.000.000.00";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$file = dirname(__FILE__) . "/local_export.csv";
$remote_file = "remote_export.csv";
// set up basic connection
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
ftp_pasv($conn_id, true);
// login with username and password
if (ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_server\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// change the current directory
if (ftp_chdir($conn_id, "transfer")) {
echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
} else {
echo "Couldn't change directory\n";
}
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
In terms of the location of the two files, the local file is in:
http://subdomain.domain/folder/local_export.csv
While the remote file is going to go on the root / folder of the server (which is also the only one I have access to).
To me it looks like everything is in order, but still the code only generates the local file, always failing to save it to the remote location. I'm fairly new to moving files around, so I'm sorry in advance if the solution is in front of my eyes but I can't see it!
Many thanks in advance for your help.
EDIT: as pointed out in the comments below, I should've checked if the connection to the server is successful or not. I've done those checks. The connection return successful (I've also tried independently via Filezilla to make sure the account I was given is able to upload in that location and it is.). So I guess I'm back at square one.
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.