Page 1 of 1

[SOLVED]upload file then send it to another over FTP

Posted: Tue Sep 05, 2006 2:46 am
by SuperFly
Hi everyone, I have done some search&browse on forum but didn't find what I'm looking for.
I have 2 files, 1 is foe handling uploaded file(move_uploaded_file()) and in second is function wich should uploaded file transfer to other FTP location so I will have files on both servers, here is the code:

Code: Select all

<?
//
// Edit product form
//
session_start();
require_once("../conn.php");
require_once("../template.inc.php");

//ZV include file to upload on other server
include_once "add-file.php";

$DOWNLOAD_SERVER = '';

set_time_limit(0);
$row_ccid = @mysql_fetch_array(@mysql_query("select * from links WHERE id = $id"));
if (!$productid)
    $productid = $row_ccid['productid'];

if($action=='edit')
{
	    // Upload product
	    $file2  = $row_ccid['plink'];
	    if($plink!="" && file_exists ($plink))
	    {
		@unlink($row_ccid["plink"]);
		$file2 = realpath('../products') . "/" . $HTTP_POST_FILES['plink']['name'];
		if (move_uploaded_file($plink, $file2))
		{
			//call ftp upload
		    ftp_upload($plink, $file2);
		    chmod($file2,0644);
		    $pplink = $DOWNLOAD_SERVER . str_replace("/home/rrs/public_html","",$file2);
		}
		else
		    $file2 = "";
	    }
	    elseif ($pplink)
	    {
		if (!preg_match("/^(ht|f)tp[s]?:\/\//i",$pplink)  && !$row_ccid['plink'])
	    		$pplink = "http://$pplink";
	    }
	    else
	    	$pplink = $row_ccid['pplink'];

	if ($id)
	    mysql_query("update links set
			name = '$name',
			plink = '$file2',
			pplink = '$pplink'
			WHERE id = $id");
	else
	    mysql_query("insert into links (
				productid,
				name,
				plink,
				pplink
				) values (
				'$productid',
				'$name',
				'$file2',
				'$pplink'
				)");
?>
		<script language='Javascript'>
			if (window.opener)
			     window.opener.location.reload(window.opener.location.href);
			window.close();
		</script>
		<body onload='window.close()'>
		</body>
<?
	exit;
}

$t = new Smarty_RRS;
$t->assign("pid",$productid);
$t->assign("id",$id);
$t->assign("row_ccid",$row_ccid);
$t->assign("max_filesize",ini_get("upload_max_filesize"));

$t->display('madmin/edit_linknew.tpl');
?>
and file with ftp function is here:

Code: Select all

<?php

define("FTP_HOST","xxx.xxx.xxx.xxx");
define("FTP_USERNAME","xxxxx");
define("FTP_PASSWORD","xxxxx");
define("FTP_FILE_PATH","../../var/www/html/products/");

function ftp_upload($ftp_source_file, $filename)
{

	$remote_file = FTP_FILE_PATH.$filename;
	//setup basic connection
	$connection_id = ftp_connect(FTP_HOST);

	//connect with username and password
	$ftp_login = ftp_login($connection_id,FTP_USERNAME,FTP_PASSWORD);

	//check for connection
	if((!$connection_id) || (!$ftp_login))
	{
		 echo "FTP connection has failed!";
		 exit;
	}

	//upload file
	$ftp_upload = ftp_put($connection_id, $remote_file, $ftp_source_file, FTP_ASCII);

	//check for upload file
	if (!$ftp_upload)
	{
       		echo "FTP upload has failed!";
       		exit;
   	}

	//close connection
	ftp_close($connection_id);

	return true;
}//ftp_upload($ftp_source_file, $filename)

?>
Upload works fine, I have a problem with FTP
Any help is appreciated

Regards

Posted: Tue Sep 05, 2006 3:59 am
by SuperFly
ok lett me re-create this:

Code: Select all

<?php
//path related
define("ROOT", str_replace("\\", "/", realpath(dirname(__FILE__)."/../")."/"));
define("URL", "http://".$_SERVER["HTTP_HOST"]
		.(($_SERVER["DOCUMENT_ROOT"]{strlen($_SERVER["DOCUMENT_ROOT"])-1} == "/") ?
			"/" : "")
		.str_replace(str_replace("\\", "/", $_SERVER["DOCUMENT_ROOT"]),
				"",
				str_replace("\\", "/", realpath(dirname(__FILE__)."/../")."/")
			)
	);

	define("OBJEKAT_IMG_PATH", ROOT."jobs/slike/");
define("OBJEKAT_IMG_URL", URL."slike/lokacije/");


	$file = $_FILES["File"];
	$tmp_file_name = OBJEKAT_IMG_PATH.$file["name"];

	move_uploaded_file($file["tmp_name"], $tmp_file_name);

	$remote_file = FTP_FILE_PATH.$file["name"];
	//setup basic connection
	$connection_id = ftp_connect(FTP_HOST);

	//connect with username and password
	$ftp_login = ftp_login($connection_id,FTP_USERNAME,FTP_PASSWORD);

	//check for connection
	if((!$connection_id) || (!$ftp_login))
	{
		 echo "FTP connection has failed!";
		 exit;
	}

	//upload file

	$ftp_upload = ftp_put($connection_id, $remote_file, $file["tmp_name"], FTP_ASCII);

	//check for upload file
	if (!$ftp_upload)
	{
       		echo "FTP upload has failed!";
       		exit;
   	}

	//close connection
	ftp_close($connection_id);

	return true;
?>
I'm getting following error:
FTP upload has failed!

Posted: Tue Sep 05, 2006 4:38 am
by SuperFly
I have found solution: all I had to do is to replace this -> $file["tmp_name"] with this -> $tmp_file_name in ftp_put();

Thanks any way