PHP FTP upload not working - php

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.

Related

send file with post, then upload with ftp

This is a very basic question since I do not properly understand the underlying concepts of the different protocols. Please be indulgent.
I can upload files to my site with FTP using Filezilla. Now I'm developing a user interface for sending data to update a database and files (photos) to be saved in a directory. I send this data with POST (see here). I get the file, but cannot save it to the destination directory performing
move_uploaded_file($_FILES['PhotoToUpload']['tmp_name'],"../../Photos/".$file_name);
I think is because of permissions (currently 755). Since I can currently upload with Filezilla FTP (so I understand I have the permissions to do that),
my question is:
Is bad practice (or conceptually wrong) to take the temp file created after the POST and load it with a FTP code like the following (taken from here)?
<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "../../Photos/";
$source_file = $_FILES['PhotoToUpload']['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);
?>

PHP Script to Get File From Another Server and Save on My Server

I have been playing around with a few php scripts trying to first at least get the php file to connect to the ftp on my distributors file and THEN i would like it to save on my file. I was going to save this as a cron to run this script daily. I can't get it to connect to the distributors ftp. Their ftp access is ftp.aphrodite.WEBSITE.net
The exact location of the file is ftp.aphrodite.WEBSITE.net/exporting/xml/products.xml
I can acces it with filezilla and my browser. This is a different language to me. Any help would be great!
<?php
$conn_id = ftp_connect("ftp.aphrodite.WEBSITE.net/exporting/xml/products.xml");
$login_result = ftp_login($conn_id, "USERNAME", "PASSWORD");
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
} else {
echo "Connected";
}
// get the file
$local = fopen("products.xml","w");
$result = ftp_fget($conn_id, $local,"httpdocs/products.xml", FTP_BINARY);
// check upload status
if (!$result) {
echo "FTP download has failed!";
} else {
echo "Downloaded";
}
// close the FTP stream
ftp_close($conn_id);
?>
If I do not use the full url to the ftp file I do not get a login when using a browser.
You supplied a wrong parameter to ftp_connect(), this function accept as parameter the hostname, not url.
$conn_id = ftp_connect("ftp.aphrodite.WEBSITE.net");

Saving a file from one server to another

I have a web form that I will be hosting on a centOS web server. The code that I have written, will take the data that has been entered, and save it to an excel spreadsheet to a specific location. i also have a file server, this is where I would like to have the file saved to. this is the first time that i have done a server to server file save/copy/transfer. on the web server, would i have to install an ftp client to accomplish this? When testing the program locally on my computer, this is how i currently have the destination path set up.
file_put_contents('C:\Users\username\Box Sync\Driver Check In\Shortage'.
$month. '\Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
with the code being hosted on the web server, how can i change the destination path to point to my file server?
Here is a sample script of how to upload file from your localhost to any FTP server.
Source: PHP Manual
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($resource, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>

ftp_put always returning FALSE

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.

PHP FTP upload error: Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in

I'm having some trouble finishing my FTP file uploader using PHP. I'm using the example from php.net found here: http://php.net/manual/en/ftp.examples-basic.php and have modified the code slightly.
<?php
//Start session();
session_start();
// Checking the users logged in
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$ftp_server="*";
$ftp_user_name="*";
$ftp_user_pass="*";
$paths="members/userUploads";
$name=$_FILES['userfile']['name'];
$source_file=$_FILES['userfile']['tmp_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);
// 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, $paths.'/'.$name, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
$CurrentUser = $_SESSION['CurrentUser'];
$qry = "SELECT * FROM members WHERE username='$CurrentUser'";
$result = mysql_query($qry);
$result = mysql_fetch_array($result);
$CurrentUser = $result[memberID];
$qry = "INSERT into uploads (UploadPath, UploadUser) VALUES('$file_name', '$CurrentUser')";
echo "Uploaded $source_file to $ftp_server as $paths.'/'.$name";
}
// close the FTP stream
ftp_close($conn_id);
?>
However, the code will work for some files but not others. When it doesn't work it gives the error:
Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in ... on line 48.
The name might not be set if there was an error when sending the file. This would cause you to try to upload to members/userUploads/, which would cause ftp_upload to rightly complain about an empty filename.
A common reason for the error is exceeding the maximum allowed filesize. At the very least, check the error in the $_FILES entry for your file before attempting the FTP upload:
if ($_FILES['userfile']['error'] != UPLOAD_ERR_OK) {
// handle the error instead of uploading, e.g. give a message to the user
}
You can find the description of the possible error codes in the PHP manual.
That probably comes from $name or $source_file being blank, perhaps the file upload is sometimes failing and causing the problem. You could try using an if somewhere to make sure a file was uploaded such as:
if (empty($name)) {
die('Please upload a file');
}
Just to note, unless you are using variables in the string such as:
echo "Connected to $ftp_server, for user $ftp_user_name";
its preferable to use single quotes. Technically its faster that double quotes as it doesn't scan the string for variables. Plus I think it looks neater! :)
line 48 needs to be changed, note the double quotes and the elimination of .'/'. Depending on the name of the file you are trying to upload, you may be escaping part of it's name.
$upload = ftp_put($conn_id, "$paths/$name", $source_file, FTP_BINARY);

Categories