0

I've made a plug-in for WordPress. With this plug-in the admin can upload an image.

The problem is when i upload an image with space in it's name it don't show in the GUI. I think the best way to fix this is to change the name from the image before uploading it.

This is the code in the admin page:

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

And this is the function for the image upload:

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

this is the save function: public function save($input_array) { global $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;
}

I have been messing with this for a few days now but can't figure it out. I hope someone can help me!

  • You have the same question here. https://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-into-a-directory – jun drie Jan 16 '18 at 08:46
  • I have seen that one, i have tried to put it in my code but i can't get it working.. – Stefan Ypma Jan 16 '18 at 08:48
  • wordpress has a function for removing whitespace : sanitize_title_with_dashes( string $title, string $raw_title = '', string $context = 'display' ) – Stender Jan 16 '18 at 09:41

1 Answers1

1

The solution is in this line of code:

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

The Variable $target_file is the destination where your file should be uplaoded to. As you can see, its defined by the folowing line in your code:

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

All you have to do is to adapt this line. What I like to do is using the timestamp for the image name.

$tmp = explode(".", $afbeelding["name"]);
$time = time();
$name = $time . '.' . end($tmp);
$target_file = $target_dir . $name;
Twinfriends
  • 1,972
  • 1
  • 14
  • 34
  • I have tried this and get this error: `Warning: end() expects parameter 1 to be array, null given in C:\xampp\htdocs\ivs-canvas\wp-content\plugins\ivs-canvas\includes\model\Projecten.php on line 264 Sorry, alleen JPG, JPEG, PNG & GIF files zijn toegestaan.Sorry, je afbeelding is niet geüpload.` – Stefan Ypma Jan 16 '18 at 08:55
  • Edited. Error was `tmp` and `temp` - Guess you could have found that one by yourself. Whenever you come here to stackoverflow, please also invest some timeto find errors. We're not here that you can only copy paste without using your brain anymore. Anyways, should work now, try it again ;) – Twinfriends Jan 16 '18 at 09:21
  • yeah i had found it out myself. – Stefan Ypma Jan 16 '18 at 09:27
  • But your solutions only change the name of the file and not in the Database. In the Database it still has its old name – Stefan Ypma Jan 16 '18 at 09:31
  • Yes, thats true. In the database it still has the old name. But that was never the question. Since you see now how I changed the name, I guess you should be able to do this alone? If not, let me know, so I'll help. But it would really surprise me if you don't know how to do this changes. – Twinfriends Jan 16 '18 at 09:37
  • can u only point me what line i have to change? so i can work from there? i'm very new at php. – Stefan Ypma Jan 16 '18 at 09:50
  • For sure. Actually, you haven't posted the line where you insert the data into your database, but I guess the funcion call is this line here `$result = $projecten->save($post_array);` - So in the `$post_array` there should be an attribute containing the name of the file, you simply have to change the value in the array. With `var_dump($post_array)` you can see the content of the array. – Twinfriends Jan 16 '18 at 10:04
  • This is the return, so the image is null? array(13) { ["add"]=> string(9) "Toevoegen" ["like"]=> NULL ["update"]=> NULL ["accept"]=> NULL ["naam"]=> string(2) "df" ["level"]=> string(0) "" ["beschrijving"]=> string(0) "" ["afbeelding"]=> NULL ["status"]=> string(1) "0" – Stefan Ypma Jan 16 '18 at 10:09
  • Can you add the `save()` method code in your question? Also, whats the original name of your file you try to upload? – Twinfriends Jan 16 '18 at 10:23
  • the save funtion is in the question, The name of the images is: 2013-01-14 08.27.33.jpg – Stefan Ypma Jan 16 '18 at 10:24
  • Okay, you have to change `$post_array["naam"]` to the new name. To do that, simply take out the `time()` method I've posted in my answer and insert it in your first script. Then simply perform a `$post_array["naam"] = $name;` and you should be fine. – Twinfriends Jan 16 '18 at 10:27
  • i save the name of a person with `$post_array["naam"]` with `$_FILES['afbeelding']['name']` i save the image – Stefan Ypma Jan 16 '18 at 10:31
  • oh, then change the afbleeding part :) – Twinfriends Jan 16 '18 at 10:32
  • I hope you can still help me out here? – Stefan Ypma Jan 16 '18 at 10:50
  • I have tried (see the question) to put it in the first script. but now it save the images in the Database but without .jpg or .png. and it doesn't upload the images in the upload map – Stefan Ypma Jan 16 '18 at 11:52