0

In Linux, .DEB files have 'control' text files that are arranged as follows:

Name: Value
Size: Value
Information: Mutliline
             value

What is the best way to get the control file into a PHP array resembling:

Array ( "Name" => Value, "Size" => Value, "Information" => Value);

keeping in mind that the values can be multi-line and include the ":" separator.

Thanks!

3
  • Too bad it seems to be a proprietary format and not something like YAML, at least not according to this debian.org/doc/debian-policy/ch-controlfields.html Commented Dec 8, 2010 at 22:18
  • From my (limited) work packaging .debs, I think it follows the same syntax as HTTP headers... maybe there is a PHP library that can parse those? Commented Dec 8, 2010 at 22:19
  • @Pekka: come again? It is basic RFC822 format. Commented Sep 16, 2011 at 7:16

2 Answers 2

1
$source = fopen('path/to/file');
$index = '';
while( ($line = fgets($source)) !== false ){
    if(preg_match('/^\s*$/', $line))
        continue 1; // ignore empty lines //

    if(!preg_match('/^\s+/', $line)){ // if the line does not start with whitespace then it has a new key-value pair //
        $items = explode(':', $line, 2); // separate at the first : //
        $index = strtolower($items[0]); // the keys are case insensitive //
        $value = preg_replace('/^\s+/', '', $items[1]); // remove extra whitespace from the begining //
        $value = preg_replace('/\s+$/', '', $value); // and from the end //
    }
    else{ // continue the value from the previous line //
        $value = preg_replace('/\s+$/', '', $line); // remove whitespace only from the end //
    }
    $data[$index] .= $value;
}
fclose($source);

Implemented as described here: http://www.debian.org/doc/debian-policy/ch-controlfields.html

If I made mistakes corrections are welcomed!

Sign up to request clarification or add additional context in comments.

1 Comment

Tested with a range of control files and it seems to be working nicely! Thanks!
0

//image attribute is the name attribute in the Input File Field just as name that used for the Post Methode

// return array of errros
public function Controle_upload_file($file_field_name,$max_photo_uploading_size=10485760,$allow_exts=["JPG","GIF","PNG","JPEG"],$request_type="POST"){
    //fix bytes shows
    $errors=array();
    if($_SERVER['REQUEST_METHOD'] == $request_type && isset($_FILES[$file_field_name])){
        $name=$_FILES[$file_field_name]["name"];
        $size=$_FILES[$file_field_name]["size"];
        $type_file=explode("/",$_FILES[$file_field_name]["type"]);
        $type=strtoupper(end($type_file));
        $tmp_name=$_FILES[$file_field_name]["tmp_name"];
        //check if user choosed a photo
        if(empty($size)){$errors[]="You Must Choose A Photo ";}
        else{
            if(! in_array($type,$allow_exts)){$errors[]="Invalid File Format Must Be ".implode(",",$allow_exts);}
            if($size>$max_photo_uploading_size){$errors[]="Too Large File the Size Must be under ". $max_photo_uploading_size." Bytes";} 
}//end else 
    }else{$errors[]="Somthing Went Wrong";}
    return $errors;
//end function
}
$errors=$function->Controle_upload_file("image"); //image attribute is the name attribute in the Input File Field just as name that used for the Post Methode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.