开发者

Extracting values from multi dimensional array

开发者 https://www.devze.com 2023-03-19 13:29 出处:网络
I\'m fairly new to php. I\'m using the following script to upload multiple images to a directory. The script works fine. The problem I have is I don\'t know how to reference the second image so I can

I'm fairly new to php. I'm using the following script to upload multiple images to a directory. The script works fine. The problem I have is I don't know how to reference the second image so I can store it in a mysql database. The variable $filename stores the image elements of the array. I want to add the second image to the column itm_pic_name2 in my mysql database. Please can someone point me in the right direction.

public function move($overwrite = false)
{
    $field = current($this->_uploaded);
    if (is_array($field['name'])) {
        foreach ($field['name'] as $number => $filename) {
            print_r($field);
            //process the multiple upload
            $this->_renamed = false;
            $this->processFile($filename, $field['error'][$number], $field['size'][$number], $field['type'][$number], $field['tmp_name'][$number], $overwrite);
        }
    } else 开发者_JS百科{

        $this->processFile($field['name'], $field['error'],
                           $field['size'], $field['type'], $field['tmp_name'], $overwrite);
    }
}

protected function processFile($filename, $error, $size, $type, $tmp_name, $overwrite)
{
    $OK = $this->checkError($filename, $error);
    if ($OK) {
        $sizeOK = $this->checkSize($filename, $size);
        $typeOK = $this->checkType($filename, $type);
        if ($sizeOK && $typeOK) {
            $name = $this->checkName($filename, $overwrite);
            echo $filename;
            echo $type;
            echo $size;
            $success = move_uploaded_file($tmp_name, $this->_destination . $name);
            if ($success) {
                //add the amended filename to the array of filenames
                $this->_filenames[] = $name;

                $this->execSQL("INSERT INTO itm_pic_detail(itm_pic_name, itm_pic_name2,itm_pic_type, itm_pic_size) VALUES (?,?,?)",
                               array('ssss', $filename, $not_sure_how_to_refence_this_image, $type, $size), true);
                $message = "$filename uploaded successfully";
            }
            if ($this->_renamed) {
                $message .= " and renamed $name";
            }
            $this->_messages[] = $message;
        } else {
            $this->_messages[] = 'Could not upload ' . $filename;
        }
    }
}


It's not enough to add a single itm_pic_name2 column to the database. The database structure contains more than just the file name. It contains the size and type as well. Instead, you should be inserting multiple rows in the database - one file per row. Every time you insert a picture, it should return a fileId for the file.

Essentially, what you need is a data table for your form data. That table would have two columns. One for file 1. One for file 2. In that table, you store the unique file id for each file. Then, you use joins to get the file information that you need.

I can't write it out for you exactly, because I don't have any background of what you're trying to do with the files. But, hopefully, you can take this as a start and run with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消