简体   繁体   中英

Extract data into array from foreach

Please go easy on me, this is my first time and obviously I have no idea what I'm doing...

I am using the following script that I found online to upload files and write the upload information into a database, I would like to collect the $file_name information into another array to be used outside of this code block in an implode function. I've searched high and low but have been unsuccessful in my searching. I figured out how to print the $file_name(s) from inside the if statement but not outside of both it and the php block but I can't figure out how to make it into an array instead.

Any help would be greatly appreciated.

    <?php
if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];
        $timestamp= date('Y-m-d G:i:s');
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }       
        $query="INSERT into upload_data (FILE_NAME, FILE_SIZE, FILE_TYPE, timestamp) VALUES(:file_name,:file_size,:file_type, :timestamp)";
        $desired_dir="files";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"files/".$file_name);
            }else{                                  //rename the file if another one exist
                $new_dir="files/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
            $q = $dbo->prepare($query);     
            $q->execute(array(':file_name'=>$file_name,':file_size'=>$file_size,':file_type'=>$file_type, ':timestamp'=>$timestamp));

        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}

?>

Set an array before the loop:

$file_name_array = array();

Put the following code on the line after $file_name.

$file_name_array[] = $file_name;

This will give you an array containing the contents on $file_name, and will add to the array on each pass with each one.

Then you can do whatever you wish with the array.

Declare an array:

$file_names();

Add this line inside loop after $file_name :

$file_names[] = $file_name;

This $file_names will give you name of all the files.

For your understanding (as you said you are new)

In PHP An array can be created using the array() language construct. It takes any number of comma-separated key => value pairs as arguments.

Example #1 A simple array

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];

?>

Now for your concern you just have to put all chopped out data from the foreach loop into a new array as:

 <?php
if(isset($_FILES['files'])){
    $errors= array();
    $filename_array = array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $filename_array[] = $file_name;
          $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];
    $timestamp= date('Y-m-d G:i:s');
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }       
    $query="INSERT into upload_data (FILE_NAME, FILE_SIZE, FILE_TYPE, timestamp) VALUES(:file_name,:file_size,:file_type, :timestamp)";
    $desired_dir="files";
    if(empty($errors)==true){
        if(is_dir($desired_dir)==false){
            mkdir("$desired_dir", 0700);        // Create directory if it does not exist
        }
        if(is_dir("$desired_dir/".$file_name)==false){
            move_uploaded_file($file_tmp,"files/".$file_name);
        }else{                                  //rename the file if another one exist
            $new_dir="files/".$file_name.time();
             rename($file_tmp,$new_dir) ;               
        }
        $q = $dbo->prepare($query);     
        $q->execute(array(':file_name'=>$file_name,':file_size'=>$file_size,':file_type'=>$file_type, ':timestamp'=>$timestamp));

    }else{
            print_r($errors);
    }
}
if(empty($error)){
    echo "Success";
}
}

?>

Now in your second block get the file names as

foreach ($filename_array as $filename) {
    echo $filename;
}

Refer for understanding PHP Arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM