I'm trying my hands at my first plugin, nothing fancy just want to grab the user_id, create a folder for each user and let the user upload an image.
I know my code can't be completely wrong, because if I put the upload form into a html file and the file handling php code into a .php file on the server and then point the action= of the form to the php file it works. This is without the wordpress specific parts, of course. But apparently I fail to wrap that into a plugin so I can include the form through a shortcode in my pages, because nothing happens after I click the upload button.
Here's the code:
<?php
/**
* Plugin Name: Upload Handler
* Description: The plugin to handle uploads and such.
*/
/**
* THE UPLOAD HANDLING
*/
function upload_handling(){
//try to get the user ID from wp get_current_userid()
$userid = get_current_user_id();
//set upload directory equal to user ID under userfiles
$uploadDir = "userfiles/" . $userid . "/";
$target_dir = $uploadDir;
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//Check if directoy exists otherwise create it
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists. Please rename it or choose another one.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 750000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats only
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "bmp" && $imageFileType != "tiff"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG, BMP, TIFF & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
/**
* THE file UPLOAD FORM
*/
function upload_form() {
echo '<form action="" method="post" enctype="multipart/form-data">';
echo 'Select an image file:';
echo '<input type="file" name="fileToUpload" id="fileToUpload">';
echo '<input type="submit" value="Upload Image" name="submit">';
echo '</form>';
}
if(isset($_POST['submit']))
{
upload_handling();
}
/**
* THE SHORTCODE HANDLING
*/
//this is what happens when the shortcode is used
function mh_shortcode() {
//no idea what this does
ob_start();
//the upload button
// upload_file();
//the name + file form
upload_form();
return ob_get_clean();
}
//Adding file handler shortcode
add_shortcode( 'file_handler', 'mh_shortcode' );
Any help would be appreciated.