Page 1 of 1

[SOLVED] PHP FTP upload

Posted: Mon Aug 29, 2005 4:43 pm
by simobk
Hi all,

Here is my problem. My host changed their security to disable file uploads, so I went to make a little search and found out about the FTP possibilities of php.

Here is the code I used :

Code: Select all

<?php
	// variables
	$ftpServer = "hostAddress";
	$ftpUser = "userID";
	$ftpPass = "password";
	$finalDir = '/httpdocs/km/tmp/';
	$finalFile = $finalDir . $_FILES['userfile']['name'];
	$sourceFile = $_FILES['source_file']['tmp_name'];
	
	// Connect and echo result
	$ftpConn = ftp_connect("$ftpServer");
	$ftpResult = ftp_login($ftpConn, $ftpUser, $ftpPass); 
	ftp_pasv($ftpConn, true);
	if ((!$ftpConn) || (!$ftpResult))
		echo "Connection failed<br><br>";
	else
		echo "Connection succeeded<br><br>";
		
	// upload the file
	$ftpUpload = ftp_put($ftpConn, $finalFile, $sourceFile, FTP_BINARY);
	
	// check upload status
	if (!$ftpUpload)
		echo "FTP upload has failed!";
	else
		echo "Uploaded $sourceFile to $ftpServer as $finalFile";
	
	// close the FTP stream 
	ftp_close($ftpConn); 
?>
It seems to work, I get both success messages, when I check with an FTP client, the file is there... But it is 0 bytes... A file is created, but with absolutely no content...

Any help?

Thanks,

Simo

Posted: Mon Aug 29, 2005 5:19 pm
by feyd
you code uses two different file upload fields... ?

Posted: Mon Aug 29, 2005 9:00 pm
by simobk
aheeemmm... :oops:

Feel a bit stupid... As I said, it's the first time I use FTP functions of PHP, got bits of code from different sources, and seems I just forgot to adapt a line :(

It now works... Thanks :)

Simo