Im using php code to upload a file.
The problem is i can only code it to upload file on my local host.
How do I code the target directory that can upload file in server using info such as localhost, name, password etc.
You are looking for a remote file transfer such as ftp.
Taken from the PHP manual:
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// 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)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
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
I have a bunch of movies that I'm trying to transfer from my CentOS server onto my Windows PC. But when I run them through this script they end up being corrupt. Is there something wrong with the script?
Thanks
$allFiles = glob("/var/www/html/ftp_pending/*");
// 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);
foreach($allFiles as $singleFile)
{
// check if a file exist
$path = "/"; //the path where the file is located
$file = substr( $singleFile, strrpos( $singleFile, '/' )+1 );
$check_file_exist = $path.$file; //combine string for easy use
// Returns an array of filenames from the specified directory on success or
// FALSE on error.
$contents_on_server = ftp_nlist($conn_id, $path);
// Test if file is in the ftp_nlist array
if (in_array($check_file_exist, $contents_on_server))
{
echo "$file is already on FTP Server, no need to re-upload <br />";
}
else
{
$localfile = '/var/www/html/'.$file.'';
$remote_file = $file;
// upload a file
if (ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
{
echo "successfully uploaded $file\n";
}
else
{
echo "There was a problem while uploading $file\n";
}
};
}
// remember to always close your ftp connection
ftp_close($conn_id);
You're trying to upload something other than a text-based file while using
(ftp_put($conn_id, $remote_file, $localfile, FTP_ASCII))
You should be using FTP_BINARY instead of FTP_ASCII since movies (and images) are binary files.
Code:
$file = '/export/clients.xml';
$remote_file = '/clients.xml';
$ftp_server = "ftp://my.address.com";
$ftp_user_name = "user";
$ftp_user_pass = "password";
$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, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
Now the original file path is: ftp://my#address.com/domains/website.com/public_html/export/clients.xml
How should I write the file path so that it could complete the file transfer? I want the file to be copied into the root directory of my FTP server. But it doesn't work this way.
Problem was FTP server name. I removed the ftp:// part and now it's working.
i have this function in PHP:
function UploadFileToFTP($local_path, $remote_path, $file, $filename) {
global $settings;
$remote_path = 'public_html/'.$remote_path;
$ftp_server = $settings["IntegraFTP_H"];
$ftp_user_name = $settings["IntegraFTP_U"];
$ftp_user_pass = $settings["IntegraFTP_P"];
//first save the file locally
file_put_contents($local_path.$filename, $file);
//login
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
}
//change directory
ftp_chdir($conn_id, $remote_path);
$upload = ftp_put($conn_id, $filename, $local_path.$filename, FTP_BINARY);
// check upload status
if(!$upload) {
echo "FTP upload has failed!";
}
// close the FTP stream
ftp_close($conn_id);
}
i call it here:
UploadFileToFTP('p/website/uploaded_media/', 'media/', $_FILES["file"]["tmp_name"], $filename);
the selected file is being moved into the local directory and also being uploaded to FTP however the file is becoming corrupt because it is not being uploaded correctly.
how can i get the file uploading properly?
Depending on what kind of file you're moving you may need to switch from FTP_BINARY to FTP_ASCII
http://forums.devshed.com/ftp-help-113/ftp_ascii-ftp_binary-59975.html
When uploading a file to PHP it stores the uploaded file in a temporary location, the location is stored in $_FILES["file"]["tmp_name"].
You are then passing that value into your UploadToFTP function as the variable $file.
Then you try to save a copy of the uploaded file:
//first save the file locally
file_put_contents($local_path.$filename, $file);
What this will do is write the string contained within $file (i.e. the path of the temp file) to your new location - but you want to write the content of the file.
Instead of using file_put_contents use move_uploaded_file:
move_uploaded_file($file, $local_path.$filename);
I am trying to move an uploaded file onto a remote server, this isn't working;
move_uploaded_file($tmp_name, "uploads/$code1/$code.$fileex");
$ftp_server = "IP";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$file = $tmp_name;
$remote_file = "/public_html/test/uploads/";
// 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)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
I get this erorr;
Warning: ftp_put() [function.ftp-put]: Can't open that file: Is a directory in /home/file/public_html/uploaded.php on line 52
Your $remote_file variable is pointing to a directory when it should point to a file. Try changing $remote_file to $remote_file = "/public_html/test/uploads/".$file;
You should probably wrap the portion that uploads the file in an if statement that checks to see if you are actually connected properly to the FTP
Also, when uploading a file, you need file 1 and file 2. Right now you've supplied file 2 and a directory.
http://php.net/manual/en/function.ftp-put.php
The file you are trying to move to is the directory "/public_html/test/uploads/", you need to append the filename and extension onto the directory.
Add the following line at the end of the /etc/vsftpd.conf file
Add pasv_promiscuous=YES it