<input type="file" name="" />がうまく反映されません。
- 評価
- クリップ 1
- VIEW 1,090
前提・実現したいこと
Wordpressのプラグイン「buddypress」と「Advanced Custom Fields」を使い、
指定ユーザーが記事を投稿できるシステムを作成しています。
発生している問題・エラーメッセージ
ユーザーのマイページから文章等は登録し、用意したページに反映されるのですが、
画像ファイルは上手くアップされず、困っております。
イメージページ http://misscampus-kadai.com/2016/01/14/syuukatusample/
実際のページ http://misscampus-kadai.com/store/%ef%bc%88%e6%a0%aa%ef%bc%89gakutopi%e3%80%80%e3%82%ac%e3%82%af%e3%83%88%e3%83%94/
このように実際のページでは画像が反映されておりません。
そして下記がユーザーの記事投稿画面のソースコードの一部になります。
該当のソースコード
/*---------------------------
* バリデーション
*--------------------------*/
function check_postdata(){
global $post_error;
global $upload_dir;
global $image_url;
if(!isset($_POST['title']) || empty($_POST['title'])){
$post_error[] = '販売店名が空白です。';
}
if (isset($_FILES['image']['error']) && is_int($_FILES['image']['error'])) {
// ファイルバリデーション
if (!$_FILES['image']['error']) {
// サイズ上限チェック
if ($_FILES['image']['size'] > 100000) {
$post_img_error[] = 'ファイルサイズが大きすぎます。';
}
// getimagesizeを利用しMIMEタイプをチェック
$imageInfo = getimagesize($_FILES['image']['tmp_name']);
list($orig_width, $orig_height, $image_type) = $imageInfo;
if ($imageInfo === false) {
$post_error[] = '画像ファイルではありません。';
} else {
$ext = substr($_FILES['image']['name'], strrpos($_FILES['image']['name'], '.') + 1);
if (false === $ext = array_search(
$imageInfo['mime'],
array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
$post_error[] = '画像形式が未対応です。';
}
}
$user = wp_get_current_user();
$upload_dir = wp_upload_dir();
$image_url = $upload_dir['path'] . '/'. $user->get('user_login').'-'. date(YmdHis) .'.'. $ext;
if (!move_uploaded_file($_FILES['image']['tmp_name'],$image_url)) {
$post_error[] = 'ファイル保存時にエラーが発生しました。';
}
}
}
}
function upload_image( $fname ){
global $post_id;
if (isset($_FILES[$fname]['error']) && is_int($_FILES[$fname]['error'])) {
// ファイルバリデーション
if (!$_FILES[$fname]['error']) {
$imageInfo = getimagesize($_FILES[$fname]['tmp_name']);
list($orig_width, $orig_height, $image_type) = $imageInfo;
if ($imageInfo === false) {
// $post_img_error[] = '画像ファイルではありません。';
} else {
$ext = substr($_FILES[$fname]['name'], strrpos($_FILES[$fname]['name'], '.') + 1);
$user = wp_get_current_user();
$upload_dir = wp_upload_dir();
$image_url = $upload_dir['path'] . '/'. $user->get('user_login').'-'. date(YmdHis) .'.'. $ext;
if (!move_uploaded_file($_FILES[$fname]['tmp_name'],$image_url)) {
$post_img_error[] = 'ファイル保存時にエラーが発生しました。';
}
}
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path']))
$file = $upload_dir['path'] . '/' . $filename;
else
$file = $upload_dir['basedir'] . '/' . $filename;
$image_data = file_get_contents($image_url);
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
update_post_meta($post_id, $fname, $attach_id);
}
}
}
/*---------------------------
* エラーメッセージ表示セット
*--------------------------*/
function show_post_error(){
global $post_error;
if(!empty($post_error)){
echo '<div id="error">';
echo implode('<br />', $post_error);
echo '</div>';
}
}
?>
<form action="<?php the_permalink();?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="mode" id="mode" value="<?php echo $mode; ?>" />
<input type="hidden" name="id" id="id" value="<?php echo $post_id; ?>" />
<?php wp_nonce_field('store_regist'); ?>
<table>
<tr>
<th>販売店名</th>
<td><input type="text" name="title" id="title" class="" value="<?php echo $post->post_title; ?>" /></td>
</tr>
<tr>
<th>トップイメージ</th>
<td><input type="file" name="image" /></td>
</tr>
<tr>
<th>企業イメージ</th>
<td><input type="file" name="shoptopphoto" /></td>
</tr>
<tr>
<th>アピール80文字</th>
<td>
<textarea type="text" name="appeal" id="appeal" class="" rows="5" style="width:98%;" /><?php echo $custom['appeal'][0]; ?></textarea>
</td>
</tr>
<tr>
<th>お店トップ画像</th>
<td><input type="file" name="storephoto" /></td>
</tr>
<tr>
<th>おすすめ画像①</th>
<td><input type="file" name="storephoto1" /></td>
</tr>
<tr>
<th>おすすめ画像①の説明(最大38文字) </th>
<td>
<textarea type="text" name="detail1" id="detail1" class="" rows="5" style="width:98%;" /><?php echo $custom['detail1'][0]; ?></textarea>
</td>
</tr>
投稿用スクリプトの一部になります。
<?php
$custom = get_post_custom( $post->ID );
?>
<!-- shop -->
<table style="width: 930px; height: 530px;" cellpadding="30">
<tbody>
<tr style="height: 81px;">
<td style="border-color: #ffffff; width: 783px; height: 81px; text-align: center; vertical-align: top;" colspan="2"><?php if( get_field('shoptopphoto') ): ?><img src="<?php the_field('shoptopphoto'); ?>" width="920" /><?php endif; ?></td>
</tr>
<tr style="height: 81.19px;">
<td style="border-color: #ffffff; width: 391.03px; height: 81.19px; text-align: left; vertical-align: top;" colspan="2"><div class="wc-shortcodes-row wc-shortcodes-item wc-shortcodes-clearfix"><div class="wc-shortcodes-column wc-shortcodes-content wc-shortcodes-one-half wc-shortcodes-column-first ">
<?php the_content(__('Read more', 'tcd-w')); ?>
</div><div class="wc-shortcodes-column wc-shortcodes-content wc-shortcodes-one-half wc-shortcodes-column-last ">
<span style="font-size: 14pt;"><img class="alignnone wp-image-3316" src="http://misscampus-kadai.com/wp-content/uploads/2016/01/キャプチャ_2017_01_14_14_25_41_972.png" alt="キャプチャ_2017_01_14_14_25_41_972" width="450" height="53" /></span>
<span style="font-size: 14pt;"><strong>会社名:</strong><?php echo $post->post_title; ?></span><br/>
<span style="font-size: 14pt;"><b> 住所 :</b><?php the_field('companyplace'); ?></span><br/>
<span style="font-size: 14pt;"> <strong>代表者:</strong><?php the_field('代表者'); ?><strong>
</strong></span><br/><span style="font-size: 14pt;"><b>資本金:</b><?php the_field('資本金'); ?><b><br/>
連絡先:</b><?php the_field('number'); ?><br/>
<b>エントリーページ</b><br/><a href="<?php the_field('link'); ?>"><?php the_field('link'); ?></a></span><br/></div></div></td>
</tr>
</tbody>
</table>
<table style="width: 930px;" cellpadding="30">
<tbody>
<tr>
<td style="border-color: #ffffff; width: 437.46px; text-align: center;"><img class="alignnone size-full wp-image-2965" src="http://misscampus-kadai.com/wp-content/uploads/2017/01/キャプチャ_2017_01_15_01_04_24_259.png" alt="%e3%82%ad%e3%83%a3%e3%83%97%e3%83%81%e3%83%a3_2016_11_29_06_48_01_639" width="278" height="275" /></td>
<td style="border-color: #ffffff; width: 437.46px; text-align: center;" rowspan="2"><?php if( get_field('storephoto') ): ?><img src="<?php the_field('storephoto'); ?>" width="380" /><?php endif; ?></td>
</tr>
<tr>
全く解決ができないため、ご教授していただけますと幸いです。
ではよろしくお願い致します。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
まだ回答がついていません
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.09%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正の依頼
退会済みユーザー
2017/01/29 00:49 編集
こちら( https://teratail.com/questions/63416 , https://teratail.com/questions/63767 )と同件でしょうか?
ilovemathdayo
2017/01/29 00:51
はい、同じです。
退会済みユーザー
2017/01/29 00:55
同じ質問を何度も投稿することは、私はマナー違反だと思うのですが、質問者様はどのように思われているのでしょうか。
ilovemathdayo
2017/01/29 00:59
内容を変更したため、いいと思い投稿してしまいました。初心者で同じ質問をしてしまい申し訳ございません。ただちに質問を消させていただきます。
退会済みユーザー
2017/01/29 01:07
エラーログと、投稿用のスクリプトも含めてみないとわからない気がします。できればでいいので追記をおねがいします、
ilovemathdayo
2017/01/29 01:14
エラーログはわからなかったのですが、投稿用スクリプトの一部を追記いたしました。