【问题标题】:Submit and Save PDF Files and JPG/PNG Files in same form以相同的形式提交和保存 PDF 文件和 JPG/PNG 文件
【发布时间】:2018-10-18 19:53:42
【问题描述】:

我有一个表单,用户应该能够在一个输入中上传图像,在另一个输入中上传 PDF。他们只能上传一个或另一个。我的问题是,由于我添加了 PDF 字段,用户无法再上传图像。数据库仍在检索图像名称,但尚未将图像上传到服务器。

视图中的我的表单:

 <?= $this->Form->create($work, ['class'=> 'worksform', 'enctype'=>'multipart/form-data']) ?>

<?= $this->Html->image('upload.png', ['class' => 'uploadform center']); ?>

<div class="worksformname"><?= $this->request->session()->read('Auth.User.first_name')?> <?= $this->request->session()->read('Auth.User.last_name');?></div>

<fieldset>
        <div class= "worksformtitle"><?= $this->Form->control('Title');?></div>

        <div class= "worksformcourse"><?= $this->Form->control('course_id', ['options' => $courses]);?></div>

        <div class= "worksformunit"><?= $this->Form->control('unit_id', ['options' => $units]); ?></div>

        <div class= "worksformdesc" data-container="body"><?= $this->Form->control('description');?></div>

        <p class="worksformtext">I want to upload a... </p>

        <li class="buttonworks">
        <button type="button" class="videoworks"id="videob"> Video Project </button>
        <button type="button" class="audioworks" id="audiob">  Audio Project</button>
        <button type="button" class="pdfworks" id="pdfb"> PDF File </button>
        <button type="button" class="imageworks" id="imageb"> Image File </button> </li>

        <div class="worksformvideo d-none" id="videof" data-container="body"><?= $this->Form->control('video_url')?> </div>

        <div class="worksformvideo d-none" id="audiof" data-container="body" ><?= $this->Form->control('sound_url');?> </div>

        <div class="worksformvideo d-none"id="pdff"><?= $this->Form->control('pdf', ['type'=> 'file'])?> </div>

        <div class="worksformvideo d-none"id="imagef"><?= $this->Form->control('image', ['type'=> 'file'])?> </div>
</fieldset>
<div class="buttonbox">
   <?=$this->Form->button(__('Submit')) ?>
    </div>
<?= $this->Form->end() ?>

控制器:

public function add()
{
    $work = $this->Works->newEntity();

    if ($this->request->is('post')) {
        $work = $this->Works->patchEntity($work, $this->request->getData());
        $work->user_id = $this->Auth->user('id');

        $file =$this->request->data['image'];
        $name =$this->request->data['image']['name'];

        $work->image_url = $name;

        $file =$this->request->data['pdf'];
        $name1 =$this->request->data['pdf']['name'];

        $work->pdf_url = $name1;

        if ($this->Works->save($work)) {
            move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/' . $name);
            $this->Flash->success(__('The work has been saved.'));

            move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/' . $name1);
           $this->Flash->success(__('The work has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The work could not be saved. Please, try again.'));
    }
    $users = $this->Works->Users->find('list', ['limit' => 200]);
    $units = $this->Works->Units->find('list', ['limit' => 200]);
    $courses = $this->Works->Courses->find('list', ['limit' => 200]);
    $this->set(compact('work', 'users', 'units', 'courses'));


}

视图中的输出:

   <div class="worksviewcontainer">

<p class="worksunitcourseview"> <?= $work->course->name?> / <?= $work->unit->name?></p>

<?php if(isset($work->sound_url) && !empty($work->sound_url)): ?>   
<iframe width="100%" height="166" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/<?= h($work->sound_url)?>&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"></iframe>
<?php endif; ?>

<?php if(isset($work->video_url) && !empty($work->video_url)): ?>
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/<?= h($work->video_url)?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
<?php endif; ?>

<?php if(isset($work->pdf_url) && !empty($work->pdf_url)): ?>
<object  class="pdfviewer" data="<?= '/~i7437085/project/files/'.$work->pdf_url ?>" type="application/pdf" width="600" height="500">
    <embed src="<?= '/~i7437085/project/files/'.$work->pdf_url ?>" width="600px" height="500px" />
        <p>This browser does not support PDFs. Please download the PDF to view it: 
        <a href="<?= '/~i7437085/project/files/'.$work->pdf_url ?>">Download PDF</a>.</p> 
    </embed>
    </object>
<?php endif; ?>

<?php if(isset($work->image_url) && !empty($work->image_url)): ?>
<img class="pc" src="<?= '/~i7437085/project/files/'.$work->image_url ?>">
<?php endif; ?>

<p class="workstitleview"> <?= h($work->Title)?> </p>

<p class="worksnameview"> Published by <?= $work->user->first_name?> <?=$work->user->last_name?> (<?= $work->course->name?>) on <?= h($work->created) ?></p>

<p class="descheading">Description</p>         

<p class="worksdescview"><?= h($work->description); ?></p>

<div class="commentsheadingcontainer">
<p class="commentsheading">Comments Section</p>
 </div> 

<div class="workcomments">
<?= $this->Form->create($workComment, ['url' => ['controller' => 'WorkComments', 'action' => 'add']]) ?>
    <fieldset>
       <div class="row">
        <div class="commentsname"><?= $this->request->session()->read('Auth.User.first_name')?> <?= $this->request->session()->read('Auth.User.last_name');?></div>
        </div> 
        <div class="row">
        <div class="col-lg-11 nopadding">
            <div class="comments-field"><?= $this->Form->control('comment',['placeholder'=>'Type Comment', 'label'=> false ]);?></div>
            <?= $this->Form->hidden('work_id', ['value' => $work->id]);?>
            </div>
            <div class="col-lg-1">
                <?= $this->Form->button(__('Submit')) ?>
                <?= $this->Form->end() ?>
        </div>
        </div>
        </fieldset>
</div>

【问题讨论】:

  • $file =$this-&gt;request-&gt;data['pdf']; 如果没有加载 pdf,那将是空的,会清除图像数据中可能存在的任何内容。
  • 有解决办法吗?
  • 好吧,你说用户只被允许上传一个或另一个,所以一个选项是有条件地只在上传该文件类型时运行相关代码。如果上传了图像,请运行图像代码。如果上传了 pdf,请运行 pdf 代码。或者,您可以通过使用$file$file1 来完成您对$name$name1 所做的事情。

标签: php cakephp file-upload


【解决方案1】:

如cmets中所说,问题出在:

    $file =$this->request->data['image'];
    $name =$this->request->data['image']['name'];

    $work->image_url = $name;

    $file =$this->request->data['pdf'];
    $name1 =$this->request->data['pdf']['name'];

    $work->pdf_url = $name1;

所以$file 总是等于 pdf 数据。您需要将其更改为:

if ($this->request->data['image']) {
    $file =$this->request->data['image'];
    $name =$this->request->data['image']['name'];

    $work->image_url = $name;
} else ($this->request->data['pdf']) {
    $file =$this->request->data['pdf'];
    $name1 =$this->request->data['pdf']['name'];

    $work->pdf_url = $name1;
}

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 2015-02-25
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多