当文件名包含空格

时间:2016-10-05 02:49:20

标签: php angularjs slim ng-file-upload

我用谷歌搜索了这个问题,但没找到答案。我正在使用ng-file-upload Angular指令将图像上传到我的后端。在后端我使用PHP来检索图像。如果我选择一个不包含空格的图像,它可以正常工作。但是,如果我选择包含空白区域的图像,则会抛出错误。这就是我所做的

 //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});
                            });                         

在我的html文件中我做了这个

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

在我的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;
    }
}

}

它工作正常,但如果图像包含空格,这是我在调试时得到的错误

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

1 个答案:

答案 0 :(得分:0)

使用随机值重命名文件名

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

或使用pathinfo()进行扩展

$ext = pathinfo($filename, PATHINFO_EXTENSION);