HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/wp-content/themes/ormedia/page-api-upload-file.php
<?php
/*Template Name: upload-file*/
get_header('app');
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$rv = new stdClass();
if(isset($_FILES['orm_file'])){
    $uploadedfile = $_FILES['orm_file'];
    $filename = basename($uploadedfile['name']);
    if($_REQUEST['allow'] == 'image'){
        $allowed = array('jpeg','gif','png' ,'jpg');
    }else{
        $allowed = array('jpeg','gif','png' ,'jpg','pdf','bmp','docx','doc','xls');
    }
    
    $etx = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
    $rv->etx = $etx;
    if(!in_array($etx,$allowed)){
        $error = true;
        $rv->etx = $etx;
        $rv->error_msg = "File type no supported";
    }
    if($error==false){
        $attachment_id = media_handle_upload('orm_file',0);
        if(!is_wp_error($attachment_id)){
            $rv->error = false;
            $rv->id  = $attachment_id;
            $rv->name=$_FILES['orm_file']['name'];
            $post = get_post($attachment_id);
            $rv->url = $post->guid;
            $post->post_excerpt = $rv->type.",".$rv->year;
            
            wp_update_post($post);
            update_post_meta($attachment_id,'file_info',$_FILES['orm_file']);
            update_post_meta($attachment_id, 'in_use', 0);
            
            $post = get_post($attachment_id);
            $rv->name=substr($post->post_date, 0, 10)." ".$rv->name;
            wp_update_post($post);
        }else{
            $rv->error = true;
            $rv->error_msg = $attachment_id->get_error_messages();
        }
    }
}
echo json_encode($rv);
?>