使用Zend Framework忽略文件上载的空实例?文件无法上传,因为它

时间:2013-04-05 13:58:11

标签: php javascript jquery zend-framework file-upload

我正在尝试上传ZF1中的几个文件

上传有两个选项;音频或图像。当我尝试上传图像时,只有file_audio的空实例出现问题。

这是我的打印$ _FILES

Array
(
    [image_file] => Array
        (
            [name] => boss.jpg
            [type] => image/jpeg
            [tmp_name] => C:\WINDOWS\Temp\phpA01.tmp
            [error] => 0
            [size] => 37246
            [options] => Array
                (
                    [ignoreNoFile] => 
                    [useByteString] => 1
                    [magicFile] => 
                    [detectInfos] => 1
                )

            [validated] => 
            [received] => 
            [filtered] => 
            [validators] => Array
                (
                    [0] => Zend_Validate_File_Upload
                )

        )

    [audio_file] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 
            [options] => Array
                (
                    [ignoreNoFile] => 
                    [useByteString] => 1
                    [magicFile] => 
                    [detectInfos] => 1
                )

            [validated] => 
            [received] => 
            [filtered] => 
            [validators] => Array
                (
                    [0] => Zend_Validate_File_Upload
                )
        )
)

这是我的Zend代码,只有在选择了两个文件时才能正常工作

if (! empty ( $post ['type'] )) {

    $adapter = new Zend_File_Transfer ();

//  echo "<pre>"; print_r($_FILES);   "</pre>"; exit;

    if ($post ['type'] == 'Image' && ! empty ( $_FILES ['image_file'] ['name'] )) {

        $files = $adapter->getFileInfo();


        echo "<pre>"; print_r($files);   "</pre>"; exit;
        $files = $adapter->getFileInfo();

        $files_path = $config->topic_images->path; 

        // Limit the MIME type of all given files to gif and jpeg images
//      $adapter->addValidator ( 'MimeType', false, array ('image/jpeg', 'image/png', 'image/jpg' ) );

//      $adapter->addValidator ( 'Extension', false, 'png,jpeg,jpg' );

//      $adapter->addValidator ( 'FilesSize', false, array ('min' => '1kB', 'max' => '5MB' ) );

//      $adapter->addValidator ( 'ImageSize', false, array ('minwidth' => 0, 'maxwidth' => 800, 'minheight' => 0, 'maxheight' => 800 ) );

// Limit the size of a image to a height of 100-200 and a width of 40-80 pixel


    }elseif($post ['type'] == 'Audio' && ! empty ( $_FILES ['audio_file'] ['name'] )) {

        $files_path = $config->topic_audio->path;

    }elseif($post ['type'] == 'Video' && ! empty ( $post ['video_file'] )) {

    }


$files = $adapter->getFileInfo ();

$file_name = null;
$tmpArr = null;

$fname = null;
$fext = null;


        foreach ( $files['image_file'] as $file => $info ) {
    if (! empty ( $info ['name'] )) {
        $fname = substr ( $info ['name'], 0, strrpos ( $info ['name'], "." ) );
        $fext = substr ( $info ['name'], strrpos ( $info ['name'], "." ) );
    }
}


    if (! empty ( $fname )) {

    $file_name = $fname . "-" . $post ['topic_id'] . $fext;

    $horrible_chars = array ("~", "@", "#", "$", "%", "^", "&", "*", "(", ")", " " );
echo     $cleanfilename = str_replace ( $horrible_chars, "", $file_name );


    $adapter->setDestination ( $files_path );

//  $adapter->addFilter ( 'Rename', array ('target' => $files_path . DS . $cleanfilename, 'overwrite' => true ) );



    if ($adapter->receive ()) {

    //  echo "xxx"; exit;

        $post ['filename'] = $cleanfilename;
        $result = $TopicModel->updateTopicassociation ( $post );
        $old_file = $files_path . DS . $post ['existing_file'];

        if (file_exists ( $old_file )) {
            if ($file_name != $post ['existing_file']) {
                @unlink ( $old_file );
            }
        }

        $this->_flashMessenger->addMessage ( 'Topic '.$post['type'].'added successfully' );
    } else {


    $errors = $adapter->getErrors();
    print_r($errors); exit;

        $post ['filename'] = null;
        $res_delete = $TopicModel->updateTopicassociation ( $post ['topic_id'] );
        $this->_flashMessenger->addMessage ( 'Error(s) encountered. File Association not updated.' );
    }
}

//    else {
//      if (! empty ( $post ['chkDelete'] ) && $post ['chkDelete'] == 1) {
//          $post ['filename'] = null;
//          $result = $jobModel->updateJobLogo ( $post );
//          $old_file = $files_path . DS . $post ['c_image_name'];
//          
//          if (file_exists ( $old_file )) {
//              @unlink ( $old_file );
//          }
//      }
//    }
}

我花了一整天时间尝试只上传图片或音频文件。有谁知道问题可能是什么?

已更新表单输入     的baseUrl()。 “/”。 $这 - &GT;模块名; ?&GT; /主题/动作); ?&gt;“中&GT;

      <input type="file" id="image_file" name="image_file">
    </form>

   <input type="text" name="video_file"  size="50" id="video_file" />

2 个答案:

答案 0 :(得分:4)

尝试将ignoreNoFile选项设置为true,因此如果没有文件,适配器将不会抛出拟合:

$adapter = new Zend_File_Transfer ('HTTP', false, array('ignoreNoFile'=>true));

$adapter = new Zend_File_Transfer ();
$adapter->setOptions(array('ignoreNoFile'=>true))

答案 1 :(得分:0)

我用jquery解决了它

因为我提供了上传音频,视频和图片的选项

我刚检查了提交按钮下拉列表中的值并删除了文件上传的空实例

因为zend遇到了问题。

我在下面使用这段代码非常简单。

$('#cmdSubmit').click(
        function(){

var selection = $("#type").val(); 


     if( selection == 'Image'){
         $('#videomodule').remove();
         $('#audiomodule').remove();

      }else if(selection == 'Audio'){
          $('#imagemodule').remove();
          $('#videomodule').remove();

      }else if(selection == 'Video'){
          $('#imagemodule').remove();
          $('#audiomodule').remove();

      }else{
          $('#imagemodule').remove();
          $('#videomodule').remove();
          $('#audiomodule').remove();
      }

        });

它解决了我的问题,现在其他人可以试一试或尝试摇滚乐的回答。