当我打印时,我正在尝试通过 wordpress 中的 custom.php 上传图像
`$_FILES['uploadfiles'];`
它没有显示我之前在另一个 wordpress 网站上使用过的相同代码的任何东西,该代码在ec2
我当前的 wordpress 上运行良好,在 ec2 上,并且有 bitnami 图像我已经检查了php.ini
文件中的所有配置upload_tmp_dir=/opt/bitnami/php/tmp
, . file_uploads=on
, upload_max_filesize=40M
.
我没有使用任何表单,我刚刚在一个钩子中添加了上传字段,该字段在一页上显示该字段。
我正在使用论文主题。所以任何人都知道为什么在使用此代码时会发生这种情况,而同一代码的一个副本在另一个具有大师主题的 wordpress 上工作。
贝娄是我用来上传图片的代码。
<input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" />
<?php
$uploadfiles = $_FILES['uploadfiles'];
print_r ($uploadfiles);
if (is_array($uploadfiles)) {
echo "1";
foreach ($uploadfiles['name'] as $key => $value) {
// look only for uploded files
if ($uploadfiles['error'][$key] == 0) {
$filetmp = $uploadfiles['tmp_name'][$key];
//clean filename and extract extension
$filename = $uploadfiles['name'][$key];
// get file info
// @fixme: wp checks the file extension....
$filetype = wp_check_filetype( basename( $filename ), null );
$filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];
$upload_dir = wp_upload_dir();
/**
* Check if the filename already exist in the directory and rename the
* file if necessary
*/
$i = 0;
while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
$filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
$i++;
}
$filedest = $upload_dir['path'] . '/' . $filename;
/**
* Check write permissions
*/
if ( !is_writeable( $upload_dir['path'] ) ) {
$this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
return;
}
/**
* Save temporary file to uploads dir
*/
if ( !@move_uploaded_file($filetmp, $filedest) ){
$this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
continue;
}
$attachment = array(
'post_mime_type' => $filetype['type'],
'post_title' => $filetitle,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filedest );
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
echo $attach_data;
echo $filedest;
wp_update_attachment_metadata( $attach_id, $attach_data );
//echo wp_get_attachment_url( $attach_id );
//exit();
$urlimage=wp_get_attachment_url( $attach_id );
$_SESSION["oldurl"]=$urlimage;
echo $_SESSION["oldurl"];
}
}
}
?>