HTML/PHP/JS Select multiple file and upload via FT

2019-07-31 08:40发布

问题:

Hy, I need to upload a series of files to a server (in specific a set of images). I need a single file input to select multiple files (also in IE), I have no permission to write directly with PHP on the server (permission are 775 but FTP and apache users are into 2 different groups) so I need to use an FTP connection (I'm already able to do this with a single file). Can someone advice me if there is someway to do this? Thanks in advance

Michele

Edit: I'm trying to use uploadify as Nick suggested.

 <script type="text/javascript">
    $(document).ready(function() {
        $('#file_upload').uploadify({
            'uploader'  : 'JS/uploadify.swf',
            'script'    : 'upload.php',
            'cancelImg' : 'JS/cancel.png',
            'folder'    : 'TEST/UPLOADS',
            'auto'      : true,
            'multiple'  : true,
            'removeCompleted' : false,
            'queueSizeLimit' : 3,
            'queueID' : 'queue',
            'simUploadLimit' : 1
         );
    });
</script>

I tried to put ftp connection and ftp_put into upload.php it is right?? If I try to add the 'scriptData' parameter it cannot be accessible by $_POST[] as suggested by documentation, why?

here my test link: test

The test shows that files get uploaded but no files in the server's folder.

here is my upload.php code:

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];                          // 1

    //$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';  // 2
    //$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; // 3


    $ftp_server = "***";  //address of ftp server.
    $ftp_user_name = "***"; // Username
    $ftp_user_pass = "***";   // Password
    $conn_id = ftp_connect($ftp_server);
    ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
    ftp_pasv ( $conn_id, true );

    if( ftp_fput($conn_id, 'TEST/' . $_FILES['Filedata']['name'], $tempFile, FTP_BINARY)){                       // 4
        echo true;
    }else{
        echo false;
    }

    ftp_close($conn_id);
} else {
    echo false;
}

回答1:

To allow the upload of multiple files I usually use uploadify It's documentation is fairly good, and it's pretty simple to use.

You need to use something like this in order to keep the connection open and send the files without reloading the page.

EDIT:

Your code should be something like this:

<script type="text/javascript">
$(function() {
    $('#custom_file_upload').uploadify({
        'uploader'       : 'JS/uploadify.swf',
        'script'         : 'JS/uploadify.php',
        'cancelImage'      : 'JS/uploadify-cancel.png',
        'multi'          : true,
        'auto'           : true,
        'fileTypeExts'        : '*.jpg',
        'fileTypeDesc'       : 'Image Files (.JPG)',
        'queueID'        : 'custom-queue',
        'queueSizeLimit' : 3,
        'simUploadLimit' : 3,
        'sizeLimit'   : 10240000,
        'removeCompleted': false,
        'onDialogClose'   : function(queue) {
            $('#status-message').text(queue.filesQueued + ' files have been added to the queue.');
        },
        'onQueueComplete'  : function(stats) {
            $('#status-message').text(stats.successful_uploads + ' files uploaded, ' + stats.upload_errors + ' errors.');
        }
    });             
});
</script>

swf changed to uploader and uploader changed to script.