将文件上传到自定义文件夹wordpress



我广泛搜索了这个问题的答案,但没有成功。 我想将文件从联系表格 7 上传到根目录中的文件夹。 我可以将文件上传到上传文件夹,但这并不令人满意。 我找到了会更改文件夹位置的代码,但看不到如何将附件加载到此文件中。

$folderPath = "/candidates//candidate_id/photos/";
mkdir(ABSPATH.$folderPath, 0777, true);
$filename = $image_name;        
if ($filename > '') {
require(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'guid'           => $wp_upload_dir['url'] . '' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
//Define the new Thumbnail can be also a ACF field
update_post_meta($newpostid, "_thumbnail_id", $attach_id);
}

此时将显示新目录,但图像仍最终位于上传文件夹中。 我看不到在 if ($filename 条件中在哪里定义新文件夹路径。

我现在可以上传到自定义文件夹,但图像的文件权限是 0400。 我们怎样才能改变这种状况? 代码如下:

'$folderPath = "/candidates//candidate_id/photos/";
mkdir(ABSPATH.$folderPath, 0777, true);
$filename = $image_name;        
if ($filename > '') {
require(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'guid'           => $wp_upload_dir['url'] . '' . basename( $filename ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
//Define the new Thumbnail can be also a ACF field
update_post_meta($newpostid, "_thumbnail_id", $attach_id);
}

此时将显示新目录,但图像仍最终位于上传文件夹中。 我看不到在 if ($filename 条件中在哪里定义新文件夹路径。

我尝试了以下内容,上传到新目录,但照片具有权限 0400。如何更改为 0644?

'

$folderPath = "/candidates/".$time."/photos/";
mkdir(ABSPATH.$folderPath, 0755, true);
$destination= ABSPATH.$folderPath;
// save any attachments to a temp directory
if(strlen($image_name) > 0 and !ctype_space($image_name)) {
$mail_attachments = explode(" ", $image_name);
foreach($mail_attachments as $attachment) {
$uploaded_file_path = ABSPATH . 'wp-content/uploads/wpcf7_uploads/' . $attachment;
$new_filepath = $destination .'/'. $attachment;
rename($image_location, $new_filepath);
}
}`

这是我想到的,

function contactform7_before_send_mail( $form_to_DB ) {
global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB ) 
$formData = $form_to_DB->get_posted_data();
$uploaded_files = $form_to_DB->uploaded_files(); // this allows you access to the upload file in the temp location
$time = /*date("dmY")."".*/time() ;
if (!empty($formData[Photo])){
$image_name = $formData[Photo];
//$image_name = $time.".jpg";
} else {$image_name="";}
$image_location = $uploaded_files[Photo];
if (!empty($formData[Photo])){
$folderPath = "/candidates/".$time."/photos/";
} else {$folderPath= "/empty/index.php/";}
mkdir(ABSPATH.$folderPath, 0755,true);
$destination= ABSPATH.$folderPath;
// save any attachments to a temp directory
$new_filepath = $destination .'/'. $image_name;
rename($image_location, $new_filepath);
chmod($new_filepath, 0644);

最新更新