note 86091 modified in function.copy by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
<?php
// here is my own version of UploadFile and renameIfExists
// sample: UploadPicture($_FILES['picture'],"images/",true);
// if the third parameter it is true, UploadPicture overwrites the picture
// if it is false, returns the new name and dont overwrites. regards
/////////////////////////

function IfExistsRename($file_name, $new_location){
	if(substr($new_location,-1,1) != "/"){
		$new_location += $new_location . "/";
	}

	if(file_exists($new_location . $file_name)){
		$cont = 0;
		$original_fila_name = $file_name;
		do{
			$file_parts = explode(".",$original_fila_name,2);	
			$file_name = $file_parts[0] . $cont . "." . $file_parts[1];
			$cont++;
		}while(file_exists($new_location . $file_name));
	}
	
	return $file_name;
}

 function UploadPicture($posted_picture, $new_location, $overwrite){
	$file_name ="";

	if(substr($new_location,-1,1) != "/"){
		$new_location += $new_location . "/";
	}

	if(!$overwrite){
		$file_name = IfExistsRename($posted_picture['name'], $new_location);
	}else{
		$file_name = $posted_picture['name'];
	}
	
	// checking if it was send it by post
	
	if(is_uploaded_file($posted_picture['tmp_name'])){
		// Uploading the file with the new name
		if(!copy($posted_picture['tmp_name'], $new_location . $file_name)){
		 	echo "Unable to copy the file " . $posted_picture['tmp_name'] . " to the server";
		}else{
			echo "<!-- Field Copied-->";
		}
	}else{
		echo "Unable to detect the file source";
	}
	
	return $file_name;

}
?>

--was--
// here is my own version of UploadFile and renameIfExists
// sample: UploadPicture($_FILES['picture'],"images/",true);
// if the third parameter it is true, UploadPicture overwrites the picture
// if it is false, returns the new name and dont overwrites. regards
/////////////////////////

function IfExistsRename($file_name, $new_location){
	if(substr($new_location,-1,1) != "/"){
		$new_location += $new_location . "/";
	}

	if(file_exists($new_location . $file_name)){
		$cont = 0;
		$original_fila_name = $file_name;
		do{
			$file_parts = explode(".",$original_fila_name,2);	
			$file_name = $file_parts[0] . $cont . "." . $file_parts[1];
			$cont++;
		}while(file_exists($new_location . $file_name));
	}
	
	return $file_name;
}

 function UploadPicture($posted_picture, $new_location, $overwrite){
	$file_name ="";

	if(substr($new_location,-1,1) != "/"){
		$new_location += $new_location . "/";
	}

	if(!$overwrite){
		$file_name = IfExistsRename($posted_picture['name'], $new_location);
	}else{
		$file_name = $posted_picture['name'];
	}
	
	// checking if it was send it by post
	
	if(is_uploaded_file($posted_picture['tmp_name'])){
		// Uploading the file with the new name
		if(!copy($posted_picture['tmp_name'], $new_location . $file_name)){
		 	echo "Unable to copy the file " . $posted_picture['tmp_name'] . " to the server";
		}else{
			echo "<!-- Field Copied-->";
		}
	}else{
		echo "Unable to detect the file source";
	}
	
	return $file_name;

}

http://php.net/manual/en/function.copy.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.