| Server IP : 112.74.42.100 / Your IP : 216.73.216.142 Web Server : nginx/1.20.2 System : Linux iZwz96lx4pjqiy84w4hni7Z 5.10.134-17.3.al8.x86_64 #1 SMP Thu Oct 31 14:29:57 CST 2024 x86_64 User : www ( 1000) PHP Version : 8.0.26 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,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_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/sanxuany.cn/wp-content/themes/WebStack-1.1824/inc/ |
Upload File : |
<?php
/**
* WordPress 投稿页上传图片,支持游客上传
* 原文地址:https://www.iowen.cn/wordpress-visitors-upload-pictures
* 一为忆
* WebStack 导航主题
*
* 弃用,已经移至ajax.php
*/
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
header('Allow: POST');
header('HTTP/1.1 405 Method Not Allowed');
header('Content-Type: text/plain');
exit;
}
require dirname(__FILE__).'/../../../../wp-load.php';
nocache_headers();
$extArr = array("jpg", "png", "jpeg");
$file = $_FILES['files'];
if ( !empty( $file ) ) {
$wp_upload_dir = wp_upload_dir(); // 获取上传目录信息
$basename = $file['name'];
$basesize = $file['size'];
$baseext = pathinfo($basename, PATHINFO_EXTENSION);
/* 使用前台 js 判断
if (!in_array($baseext, $extArr)) {
echo '{"status":3,"msg":"图片类型只能是jpeg,jpg,png!"}';
exit();
}
if ($basesize > (1000 * 1024)) {
echo '{"status":3,"msg":"图片大小不能超过1M"}';
exit();
}
*/
$dataname = date("YmdHis_").substr(md5(time()), 0, 8) . '.' . $baseext;
$filename = $wp_upload_dir['path'] . '/' . $dataname;
rename( $file['tmp_name'], $filename ); // 将上传的图片文件移动到上传目录
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . $dataname, // 外部链接的 url
'post_mime_type' => $file['type'], // 文件 mime 类型
'post_title' => preg_replace( '/\.[^.]+$/', '', $basename ), // 附件标题,采用去除扩展名之后的文件名
'post_content' => '', // 文章内容,留空
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename ); // 插入附件信息
if($attach_id != 0){
require_once( ABSPATH . 'wp-admin/includes/image.php' ); // 确保包含此文件,因为wp_generate_attachment_metadata()依赖于此文件。
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data ); // 生成附件的元数据,并更新数据库记录。
// 返回消息至前端
print_r(json_encode(array('status'=>1,'msg'=>'图片添加成功','data'=>array('id'=>$attach_id,'src'=>wp_get_attachment_url( $attach_id ),'title'=>time()))));
exit();
}else{
echo '{"status":4,"msg":"图片上传失败!"}';
exit();
}
}