Iphone上的图片上传问题

时间:2015-11-04 07:07:39

标签: php iphone cakephp-2.0 image-uploading

当我尝试在Iphone上传多张图片时,它会多次上传相同的图片。

例如,如果我上传image1,image2,image3,那么它会上传image3 3次。

我的PHP代码是

public function upload_image($ student_id = NULL) {

ini_set("upload_max_filesize",'300M');
ini_set('post_max_size','300M');
$this->loadmodel("StudentParent");
$this->loadmodel("Student");
$this->loadmodel("StudentImage");
if($this->request->is('post')) 
{
    $student_image_save = array();
    $image_array = array();
    //pr($this->request->data);exit;
    if(empty($this->request->data['StudentImage']['image_name'][0]))
    {
        echo "ERROR";exit;
    }
    if(isset($this->request->data['StudentImage']['image_name']) &&             !empty($this->request->data['StudentImage']['image_name']))
    {
        $parentuserId = $this->Session->read("UserAuth.User.id");
        $parent_id = $this->StudentParent->find("first", array('conditions' => array('user_id' => $parentuserId), 'recursive' => 2));
        $path = $this->Common->getpathbyid($parent_id['StudentParent']['id'], $student_id);
        $image_array = $this->request->data['StudentImage']['image_name'];
        $student_school_id = $this->Student->find('first', array('conditions' => array('Student.id' => $student_id), 'fields' => array('Student.student_school_id', 'StudentSchool.id', 'Student.first_name')));
        for($i = 0;$i < count($image_array); $i++)
        {
            chmod ($image_array[$i]['tmp_name'], 0644);
            $path_info = pathinfo($image_array[$i]['name']);
            if($path_info['extension'])
            $uid = time();
            $fileName = $uid."_".$path_info['filename'].".".strtolower($path_info['extension']);
            move_uploaded_file($image_array[$i]['tmp_name'], $path.DS.str_replace(" ", "_", $fileName));
            chmod($path.DS.str_replace(" ", "_", $fileName), 0777);
            $student_image_save['id'] = null;
            $student_image_save['student_id'] = $student_id;
            $student_image_save['student_school_id'] = $student_school_id['StudentSchool']['id'];
            $student_image_save['student_id'] = $student_id;
            $student_image_save['places'] = "Uploaded by ".$parent_id['User']['first_name'];
            $student_image_save['folder_name'] = null;
            $student_image_save['image_name'] = str_replace(" ", "_", $path_info['filename'].".".strtolower($path_info['extension']));
            $student_image_save['uploaded_date'] = date("Y-m-d", time());
            $student_image_save['uuid'] = $uid;
            $student_image_save['author_type'] = 2;
            $student_image_save['rotation'] = 0;
            $this->StudentImage->save($student_image_save);
        }
        $totalimg = $this->StudentImage->find('count', array('conditions' => array('StudentImage.student_id' => $student_id)));
        echo $totalimg;exit;
    }
}

}

我的Jquery代码是

function image_upload() {

var $fileUpload = $("input[type='file']");
if (parseInt($fileUpload.get(0).files.length)>20)
{
    alert("Image size or number exceeds configured limit");
    return false;
}
var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp', 'JPEG', 'JPG'];
if ($.inArray($("input[type='file']").val().split('.').pop(), fileExtension) == -1) {
    alert("Only '.jpeg','.jpg', '.png', '.gif', '.bmp' formats are allowed.");
    return false;
}
var student_id = $('#student_id').val();
$("#StudentImageUploadImageForm").ajaxForm({
    url: '<?php echo SITE_URL; ?>parent_dashboards/upload_image/'+student_id,
    complete:function(){
    $('#window_progress').show();
    },
    success: function(result) {

        if(result == "ERROR")
        {
            alert("Image size or number exceeds configured limit");
            return false;
        }

        alert("Images Added");
        $('#window_progress').show();
        $('#image_count'+student_id).html('');
        $('#image_count'+student_id).html(result);

        $('.imgnotfound'+student_id).hide();
        reload_div(student_id, "Date", "all");
        $.colorbox.close();
        $('#StudentImageUploadImageForm')[0].reset();

    }
}).submit();

}

0 个答案:

没有答案