V
V
Vyacheslav Ryzhkov2017-07-18 16:31:24
PHP
Vyacheslav Ryzhkov, 2017-07-18 16:31:24

How can I make the archive download?

In general, I muddied a script that downloads an archive with photos and unzips it. At the end of the script, it transfers to another page. But not only does it not transfer to another page, it does not even download the archive. So, look where I screwed up...
HTML:

<form enctype="multipart/form-data" method="post" action="http://8bgsg.ru/storage/add_photos.php">
    <span class="word">Прикрепить ZIP архив с фото:</span><br>
    <input type="file" id="file" name="archive"><br>
    <span class="word">Выберете альбом:</span><br>
    <select id="combobox" name="album">
                    
                    <?php
                    
                        mysql_connect('127.0.0.1', 'root', '');
                        mysql_select_db('site');
                        
                        $result = mysql_query("SELECT * FROM `albums`");
                    
                        while ($row = mysql_fetch_array($result)) {
                            echo '<option value="' . $row['id'] . '">' . $row['title'] . '</option>';
                        }
                    
                    ?>
                    
    </select><br>
    <input type="submit" id="button" value="Отправить"><br>
</form>

PHP:
<?php
    
    mysql_connect('127.0.0.1', 'root', '');
    mysql_select_db('site');

    if (sizeof($_FILES) != 0 and $_FILES['archive']['type'] == "application/zip") {
        
        $name = date("d_m_Y_H_i_s");
        $zip_info = pathinfo($_FILES['archive']['name']);
        
        $upload_dir = 'archives';
        $upload_file = $upload_dir . $name . '.' . $zip_info['extension'];
        
        if (move_uploaded_file($_FILES['name']['tmp_name'], $upload_file)) {
            
            $zip = new ZipArchive();
            
            if ($zip->open($upload_file)) {
                
                $num_files = $zip->numFiles;
                
                for ($i = 0; $i < $num_files; $i++) {
                    
                    $result = $zip->statIndex($i);
                    
                    $name = date("d_m_Y_H_i_s") . "_" . $i;
                    $img_info = pathinfo($result['name']);
                    
                    $upload_dir = 'images/albums/';
                    $upload_file = $upload_dir . $name . '.' . $img_info['extension'];
                    
                    if (move_uploaded_file($result['name'], $upload_file)) {
                        
                        $disk = 'http://8bgsg.ru/storage/' . $upload_file;
                        $album = $_POST['album'];
                
                        mysql_query("INSERT INTO `photos` (`name`, `album_id`, `photo_url`, `date`) VALUES('$name', '$album', '$disk', NOW())");
                        header('Location: http://8bgsg.ru/photos');
                        
                    }
                    
                }
                
            }
            
            $zip->close();
            
            header('Location: http://8bgsg.ru/photos');
            
        }
    }

?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Zakharov, 2017-07-18
@cashalot

Add the novalidate attribute to the form

4
4X_Pro, 2017-07-18
@XXXXPro

Question: what is the size of the archive? The first thing that comes to mind is that it exceeds the sizes specified in the PHP directives max_upload_size and max_post_size.
Further, instead of sizeof($_FILES) it is better to use !empty($_FILES['archive'])
And also do print_r($_FILES) at the end, and look at its output to understand what the problem is.
PS Relying on a user-supplied type is not the best idea from a security point of view. (However, in this particular case, this is not critical.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question