how to convert file in zip and at a time store in mysql data with php (dynamically)

Discussion in 'PHP' started by dineshsingh1984, Jul 21, 2011.

  1. #1
    how to convert file in zip and at a time store in mysql data with php (dynamically)

    I make a form in php where i want upload image in database but i want first it's convert in zip and after that store in mysql database

    how can do this............

    plz help me it's urgent
    plzzzzzzzzzzzzzzzz

    thanks......................
     
    dineshsingh1984, Jul 21, 2011 IP
  2. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    You can try following codes:

    
    $temp_dir = './';
    $zip_file = $temp_dir . uniqid() . '.zip';
    $image_file = $_FILES['file']['tmp_name'];
    $image_name = basename($image_file);
    
    $zip = new ZipArchive();
    if ($zip->open($zip_file, ZIPARCHIVE::CREATE) !== TRUE) {
        exit("cannot open <$filename>\n");
    }
    $zip->addFile($image_file, $image_name);
    $zip->close();
    
    $db = mysql_connect("server", "username", "password");
    mysql_select_db("database", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
    $data = addslashes(fread(fopen($zip_file, "r"), filesize($zip_file)));
    $sql = "INSERT INTO tbl_Images ";
    $sql .= "(data, filename) ";
    $sql .= "VALUES ('$data', '$image_name')";
    $result = mysql_query($sql, $db);
    mysql_free_result($result);
    
    Code (markup):
     
    Last edited: Jul 22, 2011
    dthoai, Jul 22, 2011 IP