CodeIgniter Forums
Form method file placement - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Form method file placement (/showthread.php?tid=73956)



Form method file placement - Mekaboo - 06-28-2019

Hey!!!

I have a file that is apart of a form method:

<form method="post" action="postAction.php" enctype="multipart/form-data">

I have the postAction.php file in my view folder but its not working. I wonder should it be in the controller or model folder instead? Here are the contents of the file:

PostAction.php
<?php

// Start session
session_start();

// Include and initialize DB class
require_once 'DB.class.php';
$db = new DB();

$tblName = 'images';

// File upload path
$uploadDir = "uploads/images/";

// Allow file formats
$allowTypes = array('jpg','png','jpeg','gif');

// Set default redirect url
$redirectURL = 'profile.php';

$statusMsg = '';
$sessData = array();
$statusType = 'danger';
if(isset($_POST['imgSubmit'])){
    
     // Set redirect url
    $redirectURL = 'addEdit.php';

    // Get submitted data
    $image    = $_FILES['image'];
    $title    = $_POST['title'];
    $id        = $_POST['id'];
    
    // Submitted user data
    $imgData = array(
        'title'  => $title
    );
    
    // Store submitted data into session
    $sessData['postData'] = $imgData;
    $sessData['postData']['id'] = $id;
    
    // ID query string
    $idStr = !empty($id)?'?id='.$id:'';
    
    // If the data is not empty
    if((!empty($image['name']) && !empty($title)) || (!empty($id) && !empty($title))){
        
        if(!empty($image)){
            $fileName = basename($image["name"]);
            $targetFilePath = $uploadDir . $fileName;
            $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
            
            if(in_array($fileType, $allowTypes)){
                // Upload file to server
                if(move_uploaded_file($image["tmp_name"], $targetFilePath)){
                    $imgData['file_name'] = $fileName;
                }else{
                    $statusMsg = "Sorry, there was an error uploading your file.";
                }
            }else{
                $statusMsg = "Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.";
            }
        }

        if(!empty($id)){
            // Previous file name
            $conditions['where'] = array(
                'id' => $_GET['id'],
            );
            $conditions['return_type'] = 'single';
            $prevData = $db->getRows('images', $conditions);
            
            // Update data
            $condition = array('id' => $id);
            $update = $db->update($tblName, $imgData, $condition);
            
            if($update){
                // Remove old file from server
                if(!empty($imgData['file_name'])){
                    @unlink($uploadDir.$prevData['file_name']);
                }
                
                $statusType = 'success';
                $statusMsg = 'Image data has been updated successfully.';
                $sessData['postData'] = '';
                
                $redirectURL = 'manage.php';
            }else{
                $statusMsg = 'Some problem occurred, please try again.';
                // Set redirect url
                $redirectURL .= $idStr;
            }
        }elseif(!empty($imgData['file_name'])){
            // Insert data
            $insert = $db->insert($tblName, $imgData);
            
            if($insert){
                $statusType = 'success';
                $statusMsg = 'Image has been uploaded successfully.';
                $sessData['postData'] = '';
                
                $redirectURL = 'manage.php';
            }else{
                $statusMsg = 'Some problem occurred, please try again.';
            }
        }
    }else{
        $statusMsg = 'All fields are mandatory, please fill all the fields.';
    }
    
    // Status message
    $sessData['status']['type'] = $statusType;
    $sessData['status']['msg']  = $statusMsg;
}elseif(($_REQUEST['action_type'] == 'block') && !empty($_GET['id'])){
    // Update data
    $imgData = array('status' => 0);
    $condition = array('id' => $_GET['id']);
    $update = $db->update($tblName, $imgData, $condition);
    if($update){
        $statusType = 'success';
        $statusMsg  = 'Image data has been blocked successfully.';
    }else{
        $statusMsg  = 'Some problem occurred, please try again.';
    }
    
    // Status message
    $sessData['status']['type'] = $statusType;
    $sessData['status']['msg']  = $statusMsg;
}elseif(($_REQUEST['action_type'] == 'unblock') && !empty($_GET['id'])){
    // Update data
    $imgData = array('status' => 1);
    $condition = array('id' => $_GET['id']);
    $update = $db->update($tblName, $imgData, $condition);
    if($update){
        $statusType = 'success';
        $statusMsg  = 'Image data has been activated successfully.';
    }else{
        $statusMsg  = 'Some problem occurred, please try again.';
    }
    
    // Status message
    $sessData['status']['type'] = $statusType;
    $sessData['status']['msg']  = $statusMsg;
}elseif(($_REQUEST['action_type'] == 'delete') && !empty($_GET['id'])){
    // Previous file name
    $conditions['where'] = array(
        'id' => $_GET['id'],
    );
    $conditions['return_type'] = 'single';
    $prevData = $db->getRows('images', $conditions);
                
    // Delete data
    $condition = array('id' => $_GET['id']);
    $delete = $db->delete($tblName, $condition);
    if($delete){
        // Remove old file from server
        @unlink($uploadDir.$prevData['file_name']);
        
        $statusType = 'success';
        $statusMsg  = 'Image data has been deleted successfully.';
    }else{
        $statusMsg  = 'Some problem occurred, please try again.';
    }
    
    // Status message
    $sessData['status']['type'] = $statusType;
    $sessData['status']['msg']  = $statusMsg;
}

// Store status into the session
$_SESSION['sessData'] = $sessData;
    
// Redirect the user
header("Location: ".$redirectURL);
exit();
?>

Is this a controller or model file?

Heart Heart ,
Mekaboo


RE: Form method file placement - Piotr - 06-28-2019

This is a pure php. Not a CI Controller and not CI Model


RE: Form method file placement - Wouter60 - 06-29-2019

Please, take some time (a view days!) to read the CI documentation and tutorial. Your code indicates that you have no understanding of the CI principles. You really need to do some studying. If you keep asking other people on this forum to solve error messages in old style php, that won't bring you any further.


RE: Form method file placement - Mekaboo - 06-29-2019

Thank you for the response..sorry for asking too many questions!

❤️❤️,
Mekaboo


RE: Form method file placement - InsiteFX - 06-29-2019

Take the external php file and convert it to a method in your controller then post to that.


RE: Form method file placement - Mekaboo - 07-01-2019

(06-29-2019, 08:47 AM)InsiteFX Wrote: Take the external php file and convert it to a method in your controller then post to that.

I actually created a controller (Form.php) and converted it. Working on making sure it works!

Thank ya for the feedback Heart


RE: Form method file placement - Mekaboo - 07-01-2019

Hey!

Doing some research I used the form helper and things now work fine:
https://www.codeigniter.com/user_guide/helpers/form_helper.html

I think my issue is Im putting cart before horse which makes me realize that alot of my questions can be solved easy and somewhat quickly!!