Ifrit's avatar
Level 2

Image uploader uploading file size

I've created a page where you can drag and drop your image to be uploaded. The issue I'm having is that when I drag an image to the upload section I get the filesize of that image as well, so what happens is that when I save that image the filesize is saved along with the file name. Maybe someone can see where I went wrong.

I'm using this for the file upload http://hayageek.com/docs/jquery-upload-file.php

my create.blade.php

@extends('templates::layouts.admin')
@section('content')

    <div class="row">
        <div class="col-lg-12">
            @if($errors->any())
                <ul>
                    {{ implode('', $errors->all('<li class="error">:message</li>')) }}
                </ul>
            @endif

            {{ Form::open(array('route' => 'admin.frames.store', 'class' => 'add-form')) }}
                <div class="form">
                    <div class="title_input">
                        <div>
                            {{ Form::label('title', 'Title') }}
                        </div>
                        <div>
                            {{ Form::text('title','', array('id' => 'title', "class" => "form-control")) }}
                        </div>
                    </div>

                    <div class="content_input">
                        <div>
                            {{ Form::label('content', 'Content') }}
                        </div>
                        <div>
                            {{ Form::textarea('content','', array('id' => 'content', "class" => "form-control")) }}
                        </div>
                    </div>

                    <div class="image_uploader_test">
                        <div id="fileuploader_test">Upload</div>
                    </div>

                    <script type="text/javascript">
                        $(document).ready(function(){
                            $("#fileuploader_test").uploadFile({
                                url:'{{ asset("upload/upload.php") }}',
                                dragDrop:true,
                                multiple : true,
                                showFileCounter:false,
                                showDone: false,
                                fileName: "myfile",
                                allowedTypes:"jpg,png,gif,pdf", 
                                returnType:"json",
                                showDelete:true,
                            });
                        });
                    </script>

                    <div class="submit_button">
                        {{ Form::submit('Submit', array("class" => "btn btn-info submit", "role" => "button")) }}
                    </div>
                </div>
            {{ Form::close() }}
        </div>
    </div>
@stop

if there is anything else you need me to provide please let me know.

0 likes
7 replies
Ifrit's avatar
Level 2

my upload.php

<?php
$output_dir = "uploads/";
if(isset($_FILES["myfile"]))
{
    $ret = array();
    
//  This is for custom errors;  
/*  $custom_error= array();
    $custom_error['jquery-upload-file-error']="File already exists";
    echo json_encode($custom_error);
    die();
*/
    $error =$_FILES["myfile"]["error"];
    //You need to handle  both cases
    //If Any browser does not support serializing of multiple files using FormData() 
    if(!is_array($_FILES["myfile"]["name"])) //single file
    {
        $fileName = $_FILES["myfile"]["name"];
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
        $ret[]= $fileName;
    }
    else  //Multiple files, file[]
    {
      $fileCount = count($_FILES["myfile"]["name"]);
      for($i=0; $i < $fileCount; $i++)
      {
        $fileName = $_FILES["myfile"]["name"][$i];
        move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName);
        $ret[]= $fileName;
      }
    
    }
    echo json_encode($ret);
 }
 ?>
mstnorris's avatar

I don't see where you are getting the file size??

Please describe what it is exactly that you are trying to achieve. Are you using Laravel too? I see you are using the Form helper methods, however, your upload.php is basic php.

Ifrit's avatar
Level 2

I'm trying to create a form where I can add images by dragging and then dropping the images on to the image section of the form. I've managed to do it and it's uploading and saving but the filesize gets saved as well and I'm looking for a way to remove that, but I haven't found it yet.

mstnorris's avatar

Ahh I misunderstood, I thought you wanted the file size too, but I couldn't see it anywhere. I see now.

You say:

the filesize gets saved as well...

Where does it save the file size? In the database? If so, show us the code where it is saving the other stuff to the database. I don't fully understand what you are asking as of yet.

Ifrit's avatar
Ifrit
OP
Best Answer
Level 2

I finally figured it out and I feel a bit silly. I had used the minified version of the jquery and I hadn't seen any documentation regarding the file size. So I went to the github for to check for an unminified version and I saw that the showFileSize was set to true and all I had to do was make it false. Sorry for all the trouble.

1 like
mstnorris's avatar

Always good to check out the documentation :D

Please or to participate in this conversation.