I allow users to upload zip files I extract, and look into them to see if they contain a .jpg file. I use that file to create a new node. The differences between the .jpg and the .zip file uploaded are that the first came as an object, while I don't know how I can take the second as an object.
I'm using the PclZip library.
This is part of the code I use when the uploaded file is a JPEG.
$file = $form_state['values']['file'];
$original_filename = $file->filename;
$original = file_unmanaged_copy($file->uri, 'public://work', FILE_EXISTS_REPLACE);
file_unmanaged_delete($file->uri);
list($file, $tfile) = some_fileprep($original_filename);
This is the code I use when the file is a .zip file.
include $pclzip_lib.'pclzip.lib.php';
$zip = new PclZip($file->uri);
for ($i=0; $i<$total_files; $i++) {
$file_name = $list[$i]['filename'];
list($f_name,$xt) = explode('.', $file_name);
$xt=strtolower($xt);
$regex = '/\.(' . preg_replace('/ +/', '|', preg_quote('png gif jpg jpeg tif tiff html')) . ')$/i';
if (preg_match($regex, $xt) and ($list[$i]['size'] < 10000000)) {
$error[] = "File with conflict: ".$file_name;
if((($xt == 'png') or ($xt == 'gif') or ($xt == 'jpg') or ($xt == 'jpeg') or ($xt == 'tif') or ($xt == 'tiff')) and ($f_name == 'preview')){
$file->filename = $f_name.".".$xt;
$file->uri = $f_name.".".$xt;
echo "Yes: ".$f_name.".".$xt;
$preview = true;
}
}
if(is_null($error))
{
$to_extract = $GLOBALS['_SERVER']['DOCUMENT_ROOT'] . '/' . drupal_get_path('module', 'some') . '/testing/';
$ar = $zip->extract(PCLZIP_OPT_PATH, $to_extract);
$file->filename = $ar->filename;
$file->uri = $ar->filename;
}
}
some_fileprep()
's code.