skip to Main Content

This function, seems pretty standard, I am not really getting an active button that uploads to the list in my database?

if(isset($_POST['submit'])){ //THis is how you upload a picture to a website
    $file = $_FILES['file'];
    print_r($file);

    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    //array for
    $allowed = array('jpg', 'jpeg', 'png', 'pdf');

    if(in_array($fileActualExt, $allowed)){
        if($fileError === 0) {
            if($fileSize < 1000000) {
                $fileNameNew = uniqid ('',true). ".". $fileActualExt;
                $fileDestination = '_uploads/'. $fileNameNew;
                move_uploaded_file($fileTmpName);
                header("Location: index.php");
                echo "success";
            }else{echo "Your file is too big hoe";}
        }
        else{echo "good try, youll have to upload again";}
    }
    else{echo "You cannot upload file of this type";}
}

echo $_POST['value'];
//if $_POST == "yes"

$encodedStr = base64_encode('yes.txt'); //may need to encode inside of a seperate function
//may need to implement a search based on user input
//if search matches the stored access value, then decode the .txt document
echo $encodedStr; //cannot concat a string by itself, must make into a numeric value
$decodedStr = base64_decode ($encodedStr);
echo nl2br($decodedStr);

?>

Is anything unusual about the above code? new to php.

2

Answers


  1. Try this one for uploding a image .

       //javascript
        var image_uploader = new plupload.Uploader({
            runtimes: 'html5,flash,html4',
            browse_button: "image_uploader",
            container: "image_upload_container",
            url: base_url + 'Blog/upload_files',
            chunk_size: '1mb',
            unique_names: true,
            multi_selection: false,
            resize: {
                width: 300,
                height: 300
            },
            flash_swf_url: base_url + 'assets/js/plupload/js/Moxie.swf',
            silverlight_xap_url: base_url + 'assets/js/plupload/js/Moxie.xap',
            filters: {
                max_file_size: '10mb',
                mime_types: [
                    {title: "Image files", extensions: "jpg,jpeg,png,pdf"}
                ]
            },
            init: {
                FilesAdded: function (up, files) {
                    setTimeout(function () {
                        up.start();
                    }, 1);
                },
                FileUploaded: function (up, file) {
                    $("#image_div").html('<div class="form-group col-sm-6 col-md-offset-3 text-right">n
         <a href="javascript:;" onclick="remove_photo2()">n
    <i class="fa fa-2x fa-times-circle"></i></a>n
    <div class="form-group text-left"><input type="hidden" name="image" value="' + file.target_name + '">n
    <img alt="" class="img img-responsive" src="' + base_url + 'uploads/' + file.target_name + '" height="100" width="100"/></div></div>');
                    $("#image").val(file.target_name);
                },
            },
            UploadComplete: function () {
    //            $('#image_div').unblock();
            },
            Error: function (up, err) {
    //            $('#image_div').unblock();
                bootbox.alert(err.message);
            }
    
        });
        image_uploader.init();
        function remove_photo2(id) {
            $("#image_div").empty();
        }
    //php file code 
    <div class="form-group">
                                    <div class="fileinput fileinput-new row mt-3" data-provides="fileinput" >
                                        <div id="image_div">
                                            <div class="form-group">
                                                <?php if (isset($blog_data['blog_image'])) { ?>
                                                    <input class="form" type="hidden" name="image" id="image" value="<?php echo isset($blog_data['blog_image']) ? $blog_data['blog_image'] : ''; ?>">
                                                    <img src="<?php
                                                    if ($blog_data['blog_image'] != '' && file_exists(FCPATH . 'uploads/blog/' . $blog_data['blog_image'])) {
    
                                                        echo base_url('uploads/blog/' . $blog_data['blog_image']);
                                                    } else {
    
                                                        echo base_url() . '';
                                                    }
                                                    ?>" height="70px" width="100px"/>
                                                     <?php } ?>
    <!--<input class="form" type="hidden" name="image" id="image">-->
                                            </div>
                                        </div>
                                    </div> 
                                </div>
                                
                                
                                
    // controller code
    $time_now = date('Y-m-d H:i:s');
                    // upload blog image
                    $blog_image = $this->input->post('image');
                    if ($blog_image != '') {
                        $time = time();
                        if (file_exists(FCPATH . 'uploads/' . $blog_image)) {
                            $destination_path = FCPATH . 'uploads/blog/' . $time . '/';
                            if (!is_dir($destination_path)) {
                                mkdir($destination_path, 0777, TRUE);
                            }
                            if (copy(FCPATH . 'uploads/' . $blog_image, $destination_path . $blog_image)) {
                                unlink(FCPATH . 'uploads/' . $blog_image);
                            }
                        }
                    }
    Login or Signup to reply.
  2. Try this one for uploding a image .

       //javascript
        var image_uploader = new plupload.Uploader({
            runtimes: 'html5,flash,html4',
            browse_button: "image_uploader",
            container: "image_upload_container",
            url: base_url + 'Blog/upload_files',
            chunk_size: '1mb',
            unique_names: true,
            multi_selection: false,
            resize: {
                width: 300,
                height: 300
            },
            flash_swf_url: base_url + 'assets/js/plupload/js/Moxie.swf',
            silverlight_xap_url: base_url + 'assets/js/plupload/js/Moxie.xap',
            filters: {
                max_file_size: '10mb',
                mime_types: [
                    {title: "Image files", extensions: "jpg,jpeg,png,pdf"}
                ]
            },
            init: {
                FilesAdded: function (up, files) {
                    setTimeout(function () {
                        up.start();
                    }, 1);
                },
                FileUploaded: function (up, file) {
                    $("#image_div").html('<div class="form-group col-sm-6 col-md-offset-3 text-right">n
         <a href="javascript:;" onclick="remove_photo2()">n
    <i class="fa fa-2x fa-times-circle"></i></a>n
    <div class="form-group text-left"><input type="hidden" name="image" value="' + file.target_name + '">n
    <img alt="" class="img img-responsive" src="' + base_url + 'uploads/' + file.target_name + '" height="100" width="100"/></div></div>');
                    $("#image").val(file.target_name);
                },
            },
            UploadComplete: function () {
    //            $('#image_div').unblock();
            },
            Error: function (up, err) {
    //            $('#image_div').unblock();
                bootbox.alert(err.message);
            }
    
        });
        image_uploader.init();
        function remove_photo2(id) {
            $("#image_div").empty();
        }
    //php file code 
    <div class="form-group">
                                    <div class="fileinput fileinput-new row mt-3" data-provides="fileinput" >
                                        <div id="image_div">
                                            <div class="form-group">
                                                <?php if (isset($blog_data['blog_image'])) { ?>
                                                    <input class="form" type="hidden" name="image" id="image" value="<?php echo isset($blog_data['blog_image']) ? $blog_data['blog_image'] : ''; ?>">
                                                    <img src="<?php
                                                    if ($blog_data['blog_image'] != '' && file_exists(FCPATH . 'uploads/blog/' . $blog_data['blog_image'])) {
    
                                                        echo base_url('uploads/blog/' . $blog_data['blog_image']);
                                                    } else {
    
                                                        echo base_url() . '';
                                                    }
                                                    ?>" height="70px" width="100px"/>
                                                     <?php } ?>
    <!--<input class="form" type="hidden" name="image" id="image">-->
                                            </div>
                                        </div>
                                    </div> 
                                </div>
                                
                                
                                
    // controller code
    $time_now = date('Y-m-d H:i:s');
                    // upload blog image
                    $blog_image = $this->input->post('image');
                    if ($blog_image != '') {
                        $time = time();
                        if (file_exists(FCPATH . 'uploads/' . $blog_image)) {
                            $destination_path = FCPATH . 'uploads/blog/' . $time . '/';
                            if (!is_dir($destination_path)) {
                                mkdir($destination_path, 0777, TRUE);
                            }
                            if (copy(FCPATH . 'uploads/' . $blog_image, $destination_path . $blog_image)) {
                                unlink(FCPATH . 'uploads/' . $blog_image);
                            }
                        }
                    }
    
                $blogInsertArray = array(
                    
                    'blog_image' => "$time" . "/" . $blog_image,
                );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search