Location: PHPKode > projects > DOOR - Digital Open Object Repository > door/admin/manage_object_attachment.php
<?php
    // include libraries
    require_once("../setup.php");

    //This function cleanObject the partially added object
    function cleanObject($id){        
        global $CFG;
        
        // remove object
        if(!fulldelobject($id)){
            error_catcher(4);
            exit;
        }
        // remove data
        $data_dir = $CFG->uploaddir . DIRECTORY_SEPARATOR . $id;
        if (file_exists($data_dir) AND is_dir($data_dir) AND is_writable($data_dir)) {
            if (!fulldelete($data_dir)) {
                error_catcher(17);
                exit;        
            }
        }            
    }
    
    // this function performs the upload and returns
    // - error code (if an error has occurred)
    // - if upload is ok then it depends from the file type
    //   'no'   :   $_FILES like variable ('empty')
    //   'url'  :   $_FILES like variable
    //   'local':   $_FILES like variable
    function uploadAttachment($id) {
        // config data
        global $CFG;
        
        // variable to return
        $file_info = array("userfile");
        $file_info["userfile"]["name"] = "";
        $file_info["userfile"]["size"] = 0;
        $file_info["userfile"]["type"] = ""; 
        
        // check input data
        if (!isset($_POST['filetype'])) {
            return 44;      // missing parameter    
        }
        
        // check attachment type        
        switch ($_POST['filetype']) {
            // no attachment
            case "no":
                return $file_info;
                break;
            
            // url
            case "url":
                // check on parameter
                if (!isset($_POST['userfile'])) {
                    return 44;
                }             
                // check on url format
                if ($_POST['userfile'] !== "" AND
                    strpos($_POST['userfile'], "://") !== FALSE AND
                    strpos($_POST['userfile'], ".") !== FALSE AND
                    !in_array($_POST['userfile'], array("http://", "ftp://", "https://"))) {
                    
                    // create data dir
                    if(!mkdir_p($CFG->uploaddir . DIRECTORY_SEPARATOR . $id)){
                        // data dir cannot be created
                        return 12;
                    }
                    
                    // save url in $file_info variable      
                    $file_info["userfile"]["name"] = $_POST['userfile'];
                    
                    // return $file_info variable
                    return $file_info;
                } else {
                    // url is not valid
                    return 13;
                }
                break;
            
            // file
            case "local":
                // upload errors with corresponding DOOR error number
                $upload_errors = array(
                    UPLOAD_ERR_INI_SIZE => 14,
                    UPLOAD_ERR_FORM_SIZE => 14,
                    UPLOAD_ERR_PARTIAL => 15,
                    UPLOAD_ERR_NO_FILE => 11,         // TO BE CHANGED
                    UPLOAD_ERR_NO_TMP_DIR => 11,      // TO BE CHANGED
                    UPLOAD_ERR_CANT_WRITE => 11      // TO BE CHANGED
                    /*UPLOAD_ERR_EXTENSION => 11,       // TO BE CHANGED*/
                );
                
                // check upload
                $error_code = $_FILES["userfile"]["error"];
                if ($error_code !== UPLOAD_ERR_OK) {
                    if (array_key_exists($_FILES["userfile"]["error"], $upload_errors)) {
                        return $upload_errors[$error_code];
                    } else {
                        // generic upload problem
                        return 11;  
                    }
                }
                
                // UPLOAD OK
                
                // get file extension
                $ext = strrchr($_FILES["userfile"]["name"], '.');
                
                // build file destination path
                $uploadfile = $CFG->uploaddir . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR . $id . strtolower($ext);
                
                // update the file_info variable
                $file_info["userfile"]["name"] = $id . strtolower($ext);
                $file_info["userfile"]["size"] = $_FILES["userfile"]["size"];
                $file_info["userfile"]["type"] = $_FILES["userfile"]["type"];
                
                // creation of the data directory
                if(!mkdir_p($CFG->uploaddir . DIRECTORY_SEPARATOR . $id)){
                    return 12;
                }
                
                // try to move file
                if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
                   return $file_info;
                } else {
                    return 11;                          // TO BE CHANGED
                }           
                break;
            
            // unexpected parameter value
            default:                 
                return 45;
                break;         
        }
    }
?>
Return current item: DOOR - Digital Open Object Repository