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!