Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ftp_put Shows success, but the file is not on the ftp server

Posted on 2010-11-13
7
756 Views
Last Modified: 2012-05-10
I tried to run a php upload script that was the correct answer to one of the questions on this site...  It ran perfectly...  And it said the file was successfully uploaded...   However, when I log in to check that the file was placed on the server, it is not there!  

I manually uploaded the file with FileZilla with no issues...  But, I need this to happen automatically from our server nightly...

Could it be a firewall or server setting?   I have used ftp_get many times on the same server / site...

Again...  I get no errors...  But, it doesn't show up on the remote server...And, I can download files with no issues using ftp_get...

I pasted the sample script in the code section....  Thank you!
<?php
 
$ftp_user_name='USERNAME';
$ftp_user_pass='PASSWORD';
$ftp_server='SERVERNAME';
 
$file = '19276.txt';
$remote_file = '19276.txt';
 
// set up basic connection
if ($conn_id = ftp_connect($ftp_server)){
 
  // login with username and password
  if ($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: ".ftp_size ( $conn_id, $remote_file )." bytes\n";
    } else {
      echo "There was a problem while uploading $file\n";
    }
  } else {
    echo "BAD LOGIN";
  }
  // close the connection
  ftp_close($conn_id);
} else {
  echo "NO CONNECTION";
}
?>

Open in new window

0
Comment
Question by:stephenmp
7 Comments
 
LVL 2

Expert Comment

by:prhowe
ID: 34129994
I would try BINARY

<?php
// get FTP access parameters
$host = $_POST['host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$destDir = $_POST['dir'];
$workDir = "/usr/local/temp"; // define this as per local system

// get temporary file name for the uploaded file
$tmpName = basename($_FILES['file']['tmp_name']);

// copy uploaded file into current directory
move_uploaded_file($_FILES['file']['tmp_name'], $workDir."/".$tmpName)
 or die("Cannot move uploaded file to working directory");

// open connection
$conn = ftp_connect($host) or die ("Cannot initiate connection to
 host");

// send access parameters
ftp_login($conn, $user, $pass) or die("Cannot login");

// perform file upload
$upload = ftp_put($conn, $destDir."/".$_FILES['file']['name'],
 $workDir."/".$tmpName, FTP_BINARY);

// check upload status
// display message
if (!$upload) {
  echo "Cannot upload";
} else {
  echo "Upload complete";
}

// close the FTP stream
ftp_close($conn);

// delete local copy of uploaded file
unlink($workDir."/".$tmpName) or die("Cannot delete uploaded
 file from working directory -- manual deletion recommended");
?>
0
 
LVL 7

Expert Comment

by:armchang
ID: 34130523
Hi,

Try this rewritten code from the one above. I've re-organized some parts and added passive mode. See the code:

<?php
 
$ftp_user_name='USERNAME';
$ftp_user_pass='PASSWORD';
$ftp_server='SERVERNAME';
 
$file = '19276.txt';
$remote_file = '19276.txt';
 
// set up basic connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if ($conn_id) {
	// passive must be turn on
	ftp_pasv ($conn_id, true );
	
	if ($login_result){ 
    	// upload a file
    	if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
      		echo "successfully uploaded $file: ".ftp_size ( $conn_id, $remote_file )." bytes\n";
    	} else {
      		echo "There was a problem while uploading $file\n";
    	}
  	} else {
    	echo "BAD LOGIN";
  	}
  	// close the connection
  	ftp_close($conn_id);
} else {
  echo "NO CONNECTION";
}

?>

Open in new window

0
 
LVL 13

Expert Comment

by:LinuxGuru
ID: 34132356
I recommend you to just enable passive mode in your code and upload the files.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:stephenmp
ID: 34132574
This is the craziest thing I have ever seen!  I've tried all your script...  I get no errors and it prints out...

successfully uploaded /var/www/vhosts/mywebsite.com/httpdocs/import_export/carfax/daily/WWC_dealerlist_11142010.txt: 639952 bytes

Could a firewall be blocking this?   If it was blocking it, wouldn't the login not work?   It it would return an error?
0
 

Accepted Solution

by:
stephenmp earned 0 total points
ID: 34132788
ok...  This is important!!!!    PHP Safe mode will make it think it works when it isn't really uploading...   Our System Admin upgraded the server and forgot to turn safe mode off!!!

Thanks...
0
 
LVL 13

Expert Comment

by:LinuxGuru
ID: 34132793
Hi,

Glad to know that and thank you for the fix.
0
 

Author Closing Comment

by:stephenmp
ID: 34171309
Simple server setting...
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question