2016-10-05 2 views
0

나는이 질문을 봤지만 대답을 찾지 못했습니다. ng-file-upload Angular 지시문을 사용하여 이미지를 백엔드에 업로드합니다. 백 엔드에서 나는 이미지를 검색하는 PHP를 사용하고 있습니다. 공백이없는 이미지를 선택하면 잘 동작합니다. 하지만 공백이 포함 된 이미지를 선택하면 오류가 발생합니다. 이것은 내가 이미지파일 이름에 공백이 포함되어 있으면 파일 업로드에 실패합니다

function savePictureToDb($fileName, $dirName) { 
$target_dir = "../image/" . $dirName . "/"; 
$target_file = $target_dir . basename($_FILES[$fileName]["name"]); 
$uploadOk = 1; 
$errorMsg = null; 
$report = array(); 
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 
// Check file size. //20mb 
if ($_FILES[$fileName]["size"] > 20000000) { 
    $uploadOk = 0; 
    $errorMsg = "File size is greater than 20 mega bytes"; 
} 
// Allow certain file formats 
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") { 
    $uploadOk = 0; 
    $errorMsg = "The file selected is not an image"; 
} 
if ($uploadOk == 0) { 
    $report[ERROR] = TRUE; 
    $report[MESSAGE] = $errorMsg; 
    return $report; 
    // if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES[$fileName]["tmp_name"], $target_file)) { 
     rename($target_file, $target_dir . generateRandStr_md5(500) . "." . $imageFileType); 
     $response['path'] = basename($_FILES[$fileName]["name"]); 
     $report[ERROR] = FALSE; 
     $report[PATH] = $response['path']; 
     return $report; 
    } else { 
     $errorMsg = "Unexpected error"; 
     $report[ERROR] = TRUE; 
     $report[MESSAGE] = $errorMsg; 
     return $report; 
    } 
} 

}

을 저장하는 기능을 쓴 내 PHP 파일이

<div role="button" ngf-select ng-model="file" name="file" ngf-pattern="'image/*'" ngf-accept="'image/*'" ngf-max-size="20MB">Upload</div> 

을했던 내가 내 html 파일에

//app.js 
         Upload.upload({ 
         url: "api/index.php/postNewFeedsWithPicture", 
           file: $scope.file, 
           method: "post" 
           }).then(function (response) { 
           $scope.isShowing = false; 
           if (response.data.error) { 
            toastr.options = {positionClass: 'toast-bottom-full-width'}; 
            toastr.error(response.data.message, {timeOut: 5000}); 
           } 
           else { 
            toastr.options = {positionClass: 'toast-bottom-full-width'}; 
            toastr.success(response.data.message, {timeOut: 5000}); 
            $scope.file = null; 

           } 

          }, function (reason) { 
           $scope.isShowing = false; 
           toastr.options = {positionClass: 'toast-bottom-full-width'}; 
           toastr.error('Message not posted! Network error', {timeOut: 5000}); 
          });       

을 한 것입니다 괜찮아요,하지만 이미지에 공백이 포함되어있는 경우 디버깅 할 때이 오류가 발생합니다

array (
    'file' => 
    array (
    'name' => 'Age Declaration.jpg', 
    'type' => '', 
    'tmp_name' => '', 
    'error' => 1, 
    'size' => 0, 
), 
) 
+0

당신이 $ TARGET_FILE = $ TARGET_DIR을 사용할 수 있습니다에 대한 임의의 값으로

$filename1=explode(".", $file); $extension=end($filename1); $file=rand().time().".".$extension; 

또는 사용의 PathInfo을() 파일 이름을 바꿉니다. (string) basename ($ _ FILES [$ fileName] [ "name"]); –

답변

0

은 확장자

$ext = pathinfo($filename, PATHINFO_EXTENSION); 
관련 문제