上传前重命名图像

时间:2018-01-16 08:44:08

标签: php wordpress

我为WordPress制作了一个插件。 使用此插件,管理员可以上传图像。

问题是当我上传一个带有空格的图像时,它没有在GUI中显示。 我认为解决此问题的最佳方法是在上传之前更改图片名称。

这是管理页面中的代码:

      if (isset($post_array['add'])) {
    // Save images
     $tmp = explode(".", $afbeelding["name"]);
    $time = time();
    $name = $time . '.' . end($tmp);


    $_FILES['afbeelding']['name'] = $name;

    $check = getimagesize($_FILES["afbeelding"]["tmp_name"]);
    if ($check !== false) {
        //echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "Dit is geen afbeelding.";
        $uploadOk = 0;
    }
    // Upload afbeelding
    $projecten->upload($_FILES['afbeelding']);


    // Save project
    $result = $projecten->save($post_array);
    if ($result) {
        // Save was succesfull
        $add = TRUE;
    } else {
        // Indicate error
        $error = TRUE;
    }

这是图片上传的功能:

    public function upload($afbeelding) {
    $target_dir = IVS_CANVAS_PLUGIN_INCLUDES_UPLOAD_IMGS_DIR;
   // $target_file = $target_dir . basename($afbeelding["name"]);

    $target_file = $target_dir . $name;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, deze afbeelding bestaat al.";
        $uploadOk = null;
    }

    // Check file size
    if ($afbeelding["fileToUpload"]["size"] > 500000) {
        echo "Sorry, je afbeelding is te groot.";
        $uploadOk = null;
    }

    // Allow certain file formats
    if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
        echo "Sorry, alleen JPG, JPEG, PNG & GIF files zijn toegestaan.";
        $uploadOk = null;
    }

    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == null) {
        echo "Sorry, je afbeelding is niet geüpload.";
    // if everything is ok, try to upload file
    } else {


        if (move_uploaded_file($afbeelding["tmp_name"], $target_file)) {
            echo "De afbeelding " . basename($afbeelding["name"]) . " is geüpload.";
        } else {
            echo "Sorry, er ging iets fout tijdens het uploaden.";
        }
    }
    return $uploadOk;
}

这是保存功能:         public function save($ input_array){         全球$ wpdb;

     // Insert query
    $wpdb->query($wpdb->prepare("INSERT INTO `" . $wpdb->prefix . "ivs_canvas_tabel` 
        ( `naam`, `level`, `beschrijving`, `afbeelding`, `status`)" .
                    " VALUES ( '%s', '%s', '%s', '%s', '%s');", $input_array['naam'], $input_array['level'], $input_array['beschrijving'], $_FILES['afbeelding']['name'], $input_array['status']));

    // Error ? It's in there:
    if (!empty($wpdb->last_error)) {
        $this->last_error = $wpdb->last_error;
        return FALSE;
    }

    return TRUE;
}

我现在已经搞乱了几天,但是无法理解。 我希望有人可以帮助我!

1 个答案:

答案 0 :(得分:1)

解决方案就在这行代码中:

 if (move_uploaded_file($afbeelding["tmp_name"], $target_file)) {

变量$target_file是应将文件上传到的目标位置。如您所见,它由代码中的下一行定义:

 $target_file = $target_dir . basename($afbeelding["name"]);

你所要做的就是调整这条线。我喜欢做的是使用图像名称的时间戳。

$tmp = explode(".", $afbeelding["name"]);
$time = time();
$name = $time . '.' . end($tmp);
$target_file = $target_dir . $name;