Help - PHP Specific Upload

Asked

Viewed 303 times

1

Guys, I needed a huge help, actually a way to try to do what I intend.

The problem is that I have no idea how to do: I have a system that generates billets with the following name:

020 112.pdf
020 113.pdf
021 163.pdf
021 164.pdf

021 seria = id de um user, 
112 seria a quantia de boletos que o user tem até agora.

I needed to have a specific area to send files. pdf in just one input (file Multiple), either in php (I know a little), or another language for me to study better. When Selecting All Of These Slips Above, I Needed You To Automatically Direct To Users' Account.

Exemplo:
020 112.pdf
020 113.pdf

These pdf files for example should go to the user who starts with "020..." and so on and so forth.

Thanks in advance.

EDIT: Necessarily I just need to make the files go to the folder according to the code started, as said in the example.

I have the following HTML code:

<html>

<head>
    <meta charset="utf-8"/>
    <title>Upload PDF</title>

    <!-- Google web fonts -->
    <link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />

    <!-- The main CSS file -->
    <link href="assets/css/style.css" rel="stylesheet" />
</head>

<body>

    <form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
        <div id="drop">
            Drop Here

            <a>Browse</a>
            <input type="file" name="upl" multiple />
        </div>

        <ul>
            <!-- The file uploads will be shown here -->
        </ul>

    </form>
    <!-- JavaScript Includes -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="assets/js/jquery.knob.js"></script>

    <!-- jQuery File Upload Dependencies -->
    <script src="assets/js/jquery.ui.widget.js"></script>
    <script src="assets/js/jquery.iframe-transport.js"></script>
    <script src="assets/js/jquery.fileupload.js"></script>

    <!-- Our main JS file -->
    <script src="assets/js/script.js"></script>


    <!-- Only used for the demos. Please ignore and remove. --> 
    <script src="http://cdn.tutorialzine.com/misc/enhance/v1.js" async></script>

</body>

script js.

    $(function(){

    var ul = $('#upload ul');

    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {

            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });

            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }

    });


    // Prevent the default action when a file is dropped on the window
    $(document).on('drop dragover', function (e) {
        e.preventDefault();
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }
}
);

EDITED UPLOAD:

   <?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip','pdf');

if(isset($_FILES['upl']))
{

    $ext = substr($_FILES['upl']['name'],-4); //Obtém a extensão do aquivo.
    $nome = substr("020 112.pdf", 0, -4); //retorna o nome sem extensão
    $dados = explode(" ", $nome); //Divide a string em partes.

    $dados[0]; //Parte "0" da string corresponde ao id do user.
    $dados[1]; //Parte "1" da string corresponde a quantia de boletos que o user tem

    $dir = 'uploads/'.$dados[0].'/'; //Diretório para uploads separando por id 

    move_uploaded_file($_FILES['upl']['tmp_name'], $dir.$_FILES['upl']['name']); //Fazer upload do arquivo

}
  • 2

    You can use the explode to capture the values 020 and 112. And to upload the file... http://php.net/manual/en/features.file-upload.php

  • You tried to use the explode (as in the answer below) to separate the file name values?

  • yes, however, it even goes to 020/020 113.pdf folder, but all the files go to this folder and not distributed to the others created. I edited the Upload

  • 1

    You can use $dir.$dados[1].pdf and replace substr("020 112.pdf", 0, -4); for substr($_FILES['upl']['name'], 0, -4);

  • It worked! For a detail! Thank you!

1 answer

0


Follow a suggestion, there are numerous ways to understand and do what you need. The "customer account" can be a record in the database or a specific folder.

Html can be similar to this:

<html>
    <head>
       <title>Basic Upload</title>
    </head>
    <body>
       <form action="#" method="POST" enctype="multipart/form-data">
          <input type="file" name="arquivos">
          <input type="submit" value="Enviar">
       </form>
    </body>
</html>

The PHP file that will receive the POST may be similar to this:

if(isset($_FILES['arquivos']))
{

    $ext = substr($_FILES['arquivos']['name'],-4); //Obtém a extensão do aquivo.
    $nome = substr("020 112.pdf", 0, -4); //retorna o nome sem extensão
    $dados = explode(" ", $nome); //Divide a string em partes.

    $dados[0]; //Parte "0" da string corresponde ao id do user.
    $dados[1]; //Parte "1" da string corresponde a quantia de boletos que o user tem

    $dir = 'uploads/'.$dados[0].'/'; //Diretório para uploads separando por id 

    move_uploaded_file($_FILES['arquivos']['tmp_name'], $dir.$_FILES['arquivos']['name']); //Fazer upload do arquivo

}

This way the files should be separated by folder, and the folder name is the user id. Example:

  • uploads/020/020 112.pdf
  • uploads/020/020 113.pdf
  • uploads/021/021 163.pdf
  • uploads/021/021 164.pdf

You should improve the code, but that’s the idea!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.