ftp_put and overwriting files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

ftp_put and overwriting files

Post by daven »

Synopsis:
I am using ftp_put().
Files are being uploaded correctly.
However, if a file exists on the server, re-uploading it will not overwrite the existing copy.

Ideas as to why this is occurring?

To work around the problem, I currently have to delete the file off of the server before uploading it.
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

can I see your code?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

[edit]
I will be putting the whole script up on SourceForge once I figure out the overwrite problem.

Code: Select all

<?
###################################################
# upload.php
# form to upload files to the server
#--------------------------------------------------
# Author: 			Daven Williams
# Last Modified: 	2003-06-20
#--------------------------------------------------
# Notes:
# 1. Large files take a long time to upload.
# 2. There is a popup window which reports the 
# 	 amount of data which has been uploaded.
#	 It is an ugly hack, but PHP cannot access such 
#	 information natively.
# 3. Please make sure your php.ini has the proper settings.
#	 the ini_set() commands here should take care of 
#	 in on a case by case basis, but changing the *.ini
#	 is a better solution.
###################################################
session_start();
require "./config.php";
require "./file_list.php";
include "./functions_JS.php";
$functions_JS=functions_JS();

ini_set('upload_max_filesize','500M');
ini_set('post_max_size','500M');

$limit=(isset($_REQUEST['limit']))?$_REQUEST['limit']:5;

if(isset($_POST['submit'])){
	if($useFTP){
		include "./functions_FTP.php";
		$mode=($_POST['mode']=="ascii")?FTP_ASCII:FTP_BINARY;
		
		for($i=0;$i<$limit;$i++){
			if($_FILES['file']['name'][$i]==""){continue;}
			$upload=ftp_put($ftp_conn,$_POST['path']."/".$_FILES['file']['name'][$i],$_FILES['file']['tmp_name'][$i],$mode) or die("no transfer");
		}
	}
	else{
		$path=$baseDir.$_SESSION['username']."/"; // path to move files to.  /home/$name/$dir
		chdir($baseDir.$_SESSION['username']);
		for($i=0;$i<$limit;$i++){
			if(($_FILES['file']['name'][$i]=="")||($_FILES['file']['size'][$i]==0)){continue;}
			if(is_uploaded_file($_FILES['file']['tmp_name'][$i])){
				move_uploaded_file($_FILES['file']['tmp_name'][$i],$path.$_FILES['file']['name'][$i]);
			}
		}
	}
	header("Location: ".$_SERVER['PHP_SELF']."?limit=".$limit."&refresh=true");
}

?>
<html>
<head>
<title>File Uploads</title>

<script language="javascript">
// figure out how many file entry lines to display
function limit_select(limit){
	switch (limit){
		case 5:document.form_limit.limit[0].checked=true;break;
		case 10:document.form_limit.limit[1].checked=true;break;
		case 15:document.form_limit.limit[2].checked=true;break;
		case 20:document.form_limit.limit[3].checked=true;break;
	}
}
</script>

<script language="javascript">
// opens a child window which displays the amount of data uploaded.
// basically it is a progress meter
// *** NOTE: This is an UGLY HACK.  ***
// However, there is no way to make a progress meter in PHP without re-writing the bloody language.
/*function open_timer(limit,loc){
	var theurl="upload_timer.php?limit="+limit;
	var loc=<?=$_SERVER['PHP_SELF']?>;
	alert(loc);
	for(i=0;i<limit;i++){
		if(document.form_upload.file[i].value!=""){
			theurl += "&file_"+i+"="+document.form_upload.file[i].value;
		}
	}

	var stats='toolbar=no,location=no,directories=no,status=no,menubar=no,';
	stats += 'scrollbars=no,resizable=no,width=400,height=50,left=300,top=165';
	
	topwin=window.open (theurl,"upload_timer",stats);
}*/
</script>
<?
echo $functions_JS;

// after the upload is complete, refresh the manager frame and reload this frame
// the reload will cause the child window (the progess meter) to close
echo '<script language="javascript">';
echo "function do_refresh(){";
if(isset($_GET['refresh'])){
	echo "refresh('frm_right');";
	echo "self.location='".$_SERVER['PHP_SELF']."?limit=".$_REQUEST['limit']."'";
}
echo "}";
echo '</script>';

echo $stylesheet;
?>

</head>
<body class="gray" onload="do_refresh();limit_select(<?=$limit?>)">
<table width="100%" cellpadding="0" cellspacing="2" border-style="inset" border="2" bgcolor="#FFFFFF">
<form name="form_upload" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" onsubmit="">
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="2">
	<tr><td class="mediumb">Files to upload</td></tr>
	<tr><td class="smallb">Warnings:  There is a 500MB limit on file size.  Also, large files take a long time to upload, so don't get impatient.</td></tr>
	<input type="hidden" name="MAX_FILE_SIZE" value="500000000">
	<?for($i=0;$i<$limit;$i++){
		echo '<tr><td class="mediumb"><input type="file" name="file[]" size="40"></td></tr>';
	}?>
	<tr>
		<td align="center">
			<input type="submit" value="Upload" name="submit" class="mono">
			<input type="reset" class="mono" value="Reset">
			<input type="hidden" name="limit" value="<?=$limit?>">
			<input type="hidden" name="path" value="">
		</td>
	</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="2">
	<tr>
		<td class="mediumb">Binary <input type="radio" name="mode" value="binary" checked></td>
		<td class="mediumb">ASCII <input type="radio" name="mode" value="ascii"></td>
	</tr>
</table>
</td>
</tr>
</form>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="2">
	<tr><td class="mediumb" colspan="8">Number of entry lines</td></tr>
	<form name="form_limit" action="<?=$_SERVER['PHP_SELF']?>" method="get">
		<tr>
			<td class="mediumb" width="5%">5</td><td><input type="radio" name="limit" value="5" onclick="javascript:submit()"></td>
			<td class="mediumb" width="5%">10</td><td><input type="radio" name="limit" value="10" onclick="javascript:submit()"></td>
			<td class="mediumb" width="5%">15</td><td><input type="radio" name="limit" value="15" onclick="javascript:submit()"></td>
			<td class="mediumb" width="5%">20</td><td><input type="radio" name="limit" value="20" onclick="javascript:submit()"></td>
		</tr>
	</form>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="2">
	<form name="logout" action="login.php" method="post" target="_top">
		<tr>
			<td class="mediumb" align="center"><input class="mono" type="submit" value=" Logout " name="logout"></td>
		</tr>
	</form>
</table>
</td>
</tr>
<?if(!$useFTP){?>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="2">
	<form name="manage" action="index.php" method="get" target="_top">
		<tr><td class="mediumb" align="center">Manage users</td></tr>
		<tr>
			<td class="mediumb" align="center">
				<input class="mono" type="submit" value=" Manage " name="user_manage">
				<input type="hidden" name="user_manage" value="1">
			</td>
		</tr>
	</form>
</table>
</td>
</tr>
<?}?>
</table>
</body>
</html>
Post Reply