上传Android文件时出错

时间:2017-05-02 10:08:43

标签: php android cakephp file-upload

我想将图片从galery上传到服务器。我成功上传了图片,但我只能上传1个文件。如果我上传2个文件,第二个文件将被上传。第一个文件不会上传。或者,如果我编辑并上传第二个图像文件,则第一个图像文件将被第二个图像文件替换。

这是webservicecontroller.php

$imagePath      =   IMAGE_PATH_FOR_TIM_THUMB.'/'.STUDY_MATERIAL_DIR.'/';
    if (!empty($_POST))
    {
        $this->loadModel('StudyMaterial');
        if($_POST['type'] != 'delete')
        {
            $shift = array();
            if($_POST['type'] == 'edit')
            {
                $shift['StudyMaterial']['id'] = $_POST['study_material_id'];
            }
            $shift['StudyMaterial']['user_id'] = $_POST['user_id'];
            $shift['StudyMaterial']['standard_id'] = $_POST['standard_id'];
            $shift['StudyMaterial']['subject_id'] = $_POST['subject_id'];
            $shift['StudyMaterial']['topic_id'] = $_POST['topic_id'];
            $shift['StudyMaterial']['lesson_id'] = $_POST['lesson_id'];
            $shift['StudyMaterial']['name'] = $_POST['name'];
            $shift['StudyMaterial']['description'] = $_POST['description'];

            $this->StudyMaterial->save($shift);
            $study_material_id = $this->StudyMaterial->id;

            if(isset($_FILES['study_material_file']) && !empty($_FILES['study_material_file']) && ($_FILES['study_material_file']['error'] == 0))
            {
                $name = $_FILES['study_material_file']['name'];
                $ext = substr(strrchr($name, "."), 1);
                $only_name = $name . "_" . time();
                $filename = 'topic_file_'.microtime(true).'.'.strtolower($ext);
                $original = STUDY_MATERIAL_DIR . "/" . $filename;
                $file_type = $_POST['file_type'];           
                if($file_type == 'video')
                {
                    if(strtolower($ext) == 'mov')
                    {
                        $original = STUDY_MATERIAL_DIR . "/" . $filename;
                        @move_uploaded_file($_FILES["study_material_file"]["tmp_name"], $original);
                        $new_filename = $only_name. '.mp4';
                        $srcFile = STUDY_MATERIAL_DIR . "/" . $filename;
                        $destFile = STUDY_MATERIAL_DIR . "/" . $new_filename;                   
                        exec('ffmpeg -i '.$srcFile.' -f mp4 -s 320x240 '.$destFile.'');         
                        if($_POST['type'] == 'edit')
                        {
                            $checkAlready   =   $this->StudyMaterial->find('first',array('conditions'=>array('StudyMaterial.id'=>$_POST['study_material_id'])));        
                            $image = $checkAlready['StudyMaterial']['file_name']; 
                            if($image &&  file_exists(WWW_ROOT.STUDY_MATERIAL_DIR.DS.$image )) 
                            {
                                @unlink(WWW_ROOT.STUDY_MATERIAL_DIR.DS.$image);
                            }
                        }
                        $this->StudyMaterial->saveField('file_name',$filename,false);
                        $this->StudyMaterial->saveField('type',$file_type,false);
                    }
                    else
                    {
                        @move_uploaded_file($_FILES["study_material_file"]["tmp_name"], $original);
                        if($_POST['type'] == 'edit')
                        {
                            $checkAlready   =   $this->StudyMaterial->find('first',array('conditions'=>array('StudyMaterial.id'=>$_POST['study_material_id'])));        
                            $image = $checkAlready['StudyMaterial']['file_name']; 
                            if($image &&  file_exists(WWW_ROOT.STUDY_MATERIAL_DIR.DS.$image )) 
                            {
                                @unlink(WWW_ROOT.STUDY_MATERIAL_DIR.DS.$image);
                            }
                        }
                        $this->StudyMaterial->saveField('file_name',$filename,false);
                        $this->StudyMaterial->saveField('type',$file_type,false);
                    }
                }
                else
                {           
                    @move_uploaded_file($_FILES["study_material_file"]["tmp_name"], $original);
                    if($_POST['type'] == 'edit')
                    {
                        $checkAlready   =   $this->StudyMaterial->find('first',array('conditions'=>array('StudyMaterial.id'=>$_POST['study_material_id'])));        
                        $image = $checkAlready['StudyMaterial']['file_name']; 
                        if($image &&  file_exists(WWW_ROOT.STUDY_MATERIAL_DIR.DS.$image )) 
                        {
                            @unlink(WWW_ROOT.STUDY_MATERIAL_DIR.DS.$image);
                        }
                    }
                    $this->StudyMaterial->saveField('file_name',$filename,false);
                    $this->StudyMaterial->saveField('type',$file_type,false);
                }
            }
            else
            {
                if($_POST['type'] == 'edit')
                {
                    $this->StudyMaterial->id = $_POST['study_material_id'];
                    $this->StudyMaterial->saveField('file_name','',false);
                    $this->StudyMaterial->saveField('type','',false);
                }                   
            }
            if($_POST['type'] == 'edit')
            {
                $responseData =   array(
                    'status' =>  1,
                    'message' =>  "Study Material updated successfully."
                );
            }
            else
            {
                $responseData =   array(
                    'status' =>  1,
                    'message' =>  "Study Material added successfully."
                );
            }
        }

是webservice还是android中的问题?这是add_material.java

private void selectImage() {

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    getActivity().startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

1 个答案:

答案 0 :(得分:0)

服务器端出现问题,请检查数据库查询更新列的位置。