2011-03-29 9 views
0

jQuery로 "향신료"를주고 싶지만 실제로 작동하도록 만드는 방법을 생각할 수없는 코드 조각을 만들고 있습니다. 나는 그것의 단순함을 확신하고, 나는 단지 나를 얻기 위해 약간의 충고가 필요하다.PHP Foreach and jQuery

파일을 다운로드 한 다음 S3 버킷에 업로드하는 큰 루프를 시작하기 위해 Ajax 요청을 내보내는 코드를 만들고 싶습니다. 내가 갇혀있는 곳은 파일이 업로드 될 때마다 브라우저에 다시 요청을 보내고 완료시 화면에 일련의 텍스트를 출력하려고합니다.

나는 프론트 엔드 코드가 전혀 작동하지 않는다. 단지 내 머리를 논리로 둘러싼 다. 어떤 아이디어라도?

PHP 백엔드 코드 :

<?php 
    public function photos($city) { 
     if(isset($city)) 
      $this->city_name = "{$city}"; 

     // grab data array from Dropbox folder 
     $postcard_assets = $this->conn->getPostcardDirContent("{$this->city_name}", "Photos", TRUE); 
     $data = array(); 

     foreach($postcard_assets['contents'] as $asset) { 
      //only grab contents in root folder... do not traverse into sub folders && make sure the folder is not empty 
      if(!$asset['is_dir'] && $asset['bytes'] > 0) { 
       // get information on file 
       $file = pathinfo($asset['path']); 

       // download file from Dropbox 
       $original_file = $this->conn->downloadFile(str_replace(" ", "%20", $asset['path'])); 

       // create file name 
       $file_name = $this->cleanFileName($file['basename']); 

       // write photo to TMP_DIR ("/tmp/photos/") for manipulation 
       $fh = fopen(self::TMP_DIR . $file_name, 'w'); 
       fwrite($fh, $original_file); 
       fclose($fh); 

       // Resize photo 
       $this->resize_photo($file_name); 

       // hash file name 
       $raw_file = sha1($file_name); 
       // create S3 hashed name 
       $s3_file_name = "1_{$raw_file}.{$file['extension']}"; 
       // Upload manipulated file to S3 
       $this->s3->putObject($s3_file_name, file_get_contents(self::TMP_DIR . $file_name), $this->photo_s3_bucket, 'public-read'); 

       // check to see if file exists in S3 bucket 
       $s3_check = $this->s3->getObjectInfo($s3_file_name, $this->photo_s3_bucket); 

       // if the file uploaded successully to S3, load into DB 
       if($s3_check['content-length'] > 0) { 
        $data['src'] = $s3_file_name; 
        $data['width'] = $this->width; 
        $data['height'] = $this->height; 
        Photo::create_postcard_photo($data, "{$this->city_name}"); 
        // Now that the photo has been uploaded to S3 and saved in the DB, remove local file for cleanup 
        unlink(self::TMP_DIR . $file_name); 
        echo "{$file_name} uploaded to S3 and resized!<br />"; 
       } 
      } 
     } 
     // after loop is complete, kill script or nasty PHP header warnings will appear 
     exit(); 
    } 
?> 
+2

지금까지 무엇이 있습니까? – Neal

+0

백엔드 PHP 코드를 포함하도록 게시물이 업데이트되었습니다. 루프 foreach 파일을 완성 할 때마다 jQuery를 출력하는 방법을 모르겠습니다. 감사합니다! – dennismonsewicz

답변

2

주요 문제는 한 번에 한 줄을 반환하지 않도록 PHP로, 출력 버퍼된다는 점이다. 플러시를 시도하고 강제 할 수는 있지만 항상 신뢰할만한 것은 아닙니다.

교환되는 각 파일에 대해 DB에 항목을 추가하고 완료된 세부 사항을 얻기 위해 별도의 API를 작성할 수 있습니다.

일반적으로 Jquery는 요청이 완료 될 때까지 기다린 후에 HTTP 요청의 데이터를 조작 할 수 있습니다.

+0

감사합니다! 나는 내가해야 할 일에 대해 좋은 생각을 가지고 있다고 생각한다. 나는 당신이 말한 DB API를 구현하려고 노력할 것이다! – dennismonsewicz