Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 24th, 2005, 11:56 PM
Registered User
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to fshequin
Default Register Globals File Upload Problem

Greetings I'm hoping that one of the moderators can help me re-write a PHP File Upload script for use on a server where register_globals is set to off. Or if anyone can help me to turn register_globals = on where I have no control over the php.ini file...that would work as well...but does of course leave me with a security hole !

Here is the code:

include ("inc/upload/upload_class.php"); //classes is the map where the class file is stored (inc/upload/)


$max_size = 1500*100; // the max. size for uploading

class multi_files extends file_upload {

    var $number_of_files = 0;
    var $names_array;
    var $tmp_names_array;
    var $error_array;
    var $wrong_extensions = 0;

    function extra_text($msg_num) {
        switch ($this->language) {
            case "de":
            // add you translations here
            $extra_msg[1] = "Error for: <b>".$this->the_file."</b>";
            $extra_msg[2] = "You have tried to upload ".$this->wrong_extensions." files with a bad extension, the following extensions are allowed: <b>".$this->ext_string."</b>";
            $extra_msg[3] = "Select at least one file.";
            $extra_msg[4] = "Select the file(s) for upload.";
        return $extra_msg[$msg_num];
    // this method checkes the number of files for upload
    // this example works with one or more files
    function count_files() {
        foreach ($this->names_array as $test) {
            if ($test != "") {
        if ($this->number_of_files > 0) {
            return true;
        } else {
            return false;
    function upload_multi_files () {
        $this->message = "";
        if ($this->count_files()) {
            foreach ($this->names_array as $key => $value) {
                if ($value != "") {
                    $this->the_file = $value;
                    if ($this->validateExtension()) {
                        $this->the_temp_file = $this->tmp_names_array[$key];
                        if (is_uploaded_file($this->the_temp_file)) {
                            if ($this->move_upload($this->the_temp_file, $this->the_file)) {
                                $this->message[] = $this->error_text($this->error_array[$key]);
                        } else {
                            $this->message[] = $this->extra_text(1);
                            $this->message[] = $this->error_text($this->error_array[$key]);
                    } else {
            if ($this->wrong_extensions > 0) {
                $this->message[] = $this->extra_text(2);
        } else {
            $this->message[] = $this->extra_text(3);

$multi_upload = new multi_files;

$multi_upload->upload_dir = "assets/images_properties/"; // "files" is the folder for the uploaded files (you have to create this folder)
$multi_upload->extensions = array(".png", ".jpg", ".gif"); // specify the allowed extensions here
$multi_upload->message[] = $multi_upload->extra_text(4); // a different standard message for multiple files

if(isset($Submit)) {
    $multi_upload->tmp_names_array = $_FILES['upload']['tmp_name'];
    $multi_upload->names_array = $_FILES['upload']['name'];
    $multi_upload->error_array = $_FILES['upload']['error'];
    $multi_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true

Here is the upload form code:

                 <td><p>Max. filesize = <?php echo $max_size; ?> bytes. (each) </p>
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="frm_upload" id="frm_upload">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>">
<label for="upload[]">File 1:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 2:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 3:</label>
<input type="file" name="upload[]" size="30"><br>
<label for="upload[]">File 4:</label>
<input type="file" name="upload[]" size="30"><br>
    <label for="upload[]">File 5:</label>
<input type="file" name="upload[]" size="30"><br>
    <label for="upload[]">File 6:</label>
<input type="file" name="upload[]" size="30"><br>
    <label for="upload[]">File 7:</label>
<input type="file" name="upload[]" size="30"><br>
    <label for="upload[]">File 8:</label>
<input type="file" name="upload[]" size="30"><br>
Replace files?
<input type="checkbox" name="replace" value="y">
<input type="submit" name="Submit" value="Submit">
<p><?php echo $multi_upload->show_error_string(); ?></p></td>

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP register globals question Charagrin BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 February 6th, 2006 06:02 PM
register globals problem nulogix Beginning PHP 4 June 16th, 2004 08:49 AM
Error with variable..maybe register globals codefinder Beginning PHP 1 November 19th, 2003 03:31 PM
Register Globals Off cmiller Beginning PHP 4 August 18th, 2003 05:21 PM
Code depending on Register Globals char Beginning PHP 2 July 31st, 2003 11:45 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.