note 86091 modified in function.copy by danbrown
| From: | danbrown@php.net | Date: | Thu, 02 Oct 2008 18:46:59 +0000 |
| Subject: | note 86091 modified in function.copy by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-144997@lists.php.net to get a copy of this message | ||
<?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