【问题标题】:File uploads fails when file name contains space文件名包含空格时文件上传失败
【发布时间】:2016-10-05 02:49:19
【问题描述】:

我搜索了这个问题,但没有找到答案。我正在使用 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,
  ),
 )

【问题讨论】:

  • 你可以使用 $target_file = $target_dir 。 (string)basename($_FILES[$fileName]["name"]);

标签: php angularjs slim ng-file-upload


【解决方案1】:

用随机值重命名文件名

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

或者使用pathinfo()进行扩展

$ext = pathinfo($filename, PATHINFO_EXTENSION);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2021-11-16
    相关资源
    最近更新 更多