Creating an “unzip” custom module in drupal 7 - php

I am creating a website that will allow users to upload projects. Project submissions will consist of .zip file. Once the user uploads their project, I need to unzip the .zip file and find the .html file . Also, it needs to be possible to upload these projects through the web interface "Add New Content" and through Node.create by way of the services module.
note I'm using the PclZip library.
This is part of the code I use when the uploaded file but it does not extract the file
function custom_unzip_node_submit($node, $form, &$form_state) {
if ($node->type == 'flipper') {
$p_zipname = base_path() .'sites/default/files'.$form_state["complete form"]["#node"]->field_file["und"][0]["filename"];
$to_extract = base_path() . 'test/';
$zip = new PclZip($p_zipname);
$ar = $zip->extract(PCLZIP_OPT_PATH, $to_extract);
}}

Related

Wordpress: separate upload media dir from plugins dir

Hello i'm testing on a new wordpress multi-site where i want to serve media files from an external object storage as cdn.
what i did:
mounted the external object storage (s3 compatible) with Goofys inside wp-content/uploads
I made this filter i my custom plugin:
function new_upload_dir($args) {
$cdn = 'https://cdn.domain.com';
$site= get_blog_details()->blog_id;
if($site == 1) {
$args['baseurl'] = $cdn;
} else {
$args['baseurl'] = $cdn . "/sites/" .$site;
}
$args['url'] = $args['baseurl'] . $args['subdir'];
return $args;
}
add_filter('upload_dir', 'new_upload_dir');
All the images are served over cdn.domain.com pointing to the s3 bucket, and uploads works well too. But now i noticed that some plugins also store some files in the upload dir for every subdomain site like the matomo plugin:
I need to know if there is a system in wordpress to specify different upload dirs, one for media files, one for plugins files, example wp-content/uploads-media and wp-content/uploads-plugins.
Thank you very much

is it possible to send a zipped folder from server (php) to front end (angular) ? or is there another way to do it

I am using drupal as a back end.
in drupal I get some .pdf files. and then zip them using drupal zip archive. then saving this archive in my server tmp folder, i get the tmp folder using php sys_get_temp_dir()
now...
what should i return to the front end (Angular) so that the user can download this folder..
this is an example code i used for zipping:
$nodesIds = [1024, 1023, 1022]; // those are just some example ids, i get the real ids from the front end post request.
$zipName = $this->generateUniqueName();
$zip = new \ZipArchive;if(!$zip->open(sys_get_temp_dir() . '/' . $zipName, constant("ZipArchive::CREATE"))) {
return new JsonResponse('could not open zip');
}
foreach ($nodesIds as $id) {
$node = \Drupal\node\Entity\Node::load($id);
$uri = $node->filed_file->entity->getFileUri();
$name = $node->field_file->entity->id() . '_'. $node->field_file->entity->getFilename();
$url = $file_system->realpath($uri);
$zip->addFile($url, $name);
}
$zip->close();
i tried returning a link to the zipped folder in the server:
return new JsonResponse(sys_get_temp_dir() . '/' . $zipName);
but i dont know what to do with that from angular.
Also tried to return a stream using symfony:
$stream = new Stream(sys_get_temp_dir() . '/' . $zipName);
$response = new BinaryFileResponse($stream);
a stream and not a file because the user chooses which files to zip.. so they can choose as much as they want.. it might get to even a 100 .pdf files ..
every thing works fine and i get the zipped folder in tmp.. but now what ?
I want to download this zipped folder to the user browser..
but I do not know how!. should I return the zipped folder, then in angular use it as a blob or somehow deal with it and serve it to the user,,
or maybe the right way is to send back the link to its location in the tmp folder,, and in angular i only access that location and somehow get the folder (i dont know if this is even possible due to permissions and security), or is there another better way that I do not know about.
thank you very much.

yii2 Create new folder and add file to it

I'm creating Yii2 application that allows users to download tutorial and it's subtitle. Currently I can do it separately but, what I want to do is to create a new folder with the video name and include both video and subtitle file in that folder and then create a zip and give that to the user for download. but I have no idea how to do it. I can zip the files using php ZipArchive but i have no idea how to create new folder and include those two files in to it.
my download action is
public function actionDownload($id)
{
// get all videos relevan for this video id
$video = TempVideo::findOne($id);
$file = $video->path;
if (file_exists($file))
{
Yii::$app->response->sendFile($file);
}
}
helps are highly appreciated.
You could use ZipArchive
http://php.net/manual/en/zip.examples.php
or launch command line with:
system('zip filecompress.zip file1 file2 file3');
I don't have all the information but i think it need to mlook something like this:
you need to make a new directory with mkdir() (doc: http://php.net/manual/en/function.mkdir.php);
and copy the needed files with copy()
(doc:http://php.net/manual/en/function.copy.php)
public function actionDownload($id)
{
// get all videos relevan for this video id
$video = TempVideo::findOne($id);
$file = $video->path;
if (file_exists($file))
{
Yii::$app->response->sendFile($file);
$new_video_path = '/path/to/save/dir';
//create a new dir
mkdir($new_video_path);
//copy the file to a new path
copy($file,$new_video_path.'newfilename.mp4');
copy('subtitle/pathe.txt',$new_video_path.'subtitle.txt');
}
}
and after that you can create a zip file with the ZipArchive that doc can be found here: http://php.net/manual/en/class.ziparchive.php

How do I enable the uploading of KMZ/KML/GPX files in Silverstripe?

I'm setting up a site in Silverstripe 3 and have come up with a weird issue around uploading KMZ/KML files into the CMS.
The end goal is to allow CMS users to upload multiple KMZ/KML/GPX files to a page. These files will then be displayed as routes on a Google Map.
This is the has_many declaration:
static $has_many = array(
'RouteFiles' => 'File'
);
This is the getCMSFields code:
$routeFilesField = new UploadField('Route Files', 'Upload GPX/KMZ/KML files for your proposed route.', $this->RouteFiles());
$routeFilesField->getValidator()->allowedExtensions = array('kmz', 'kml', 'gpx');
$fields->addFieldToTab('Root.Map', $routeFilesField);
I have added the extensions to the FilesMatch section of the /assets/.htaccess file.
When I upload a KMZ or KML file to the CMS I get the following message in the Upload Field:
File information not found
What else do I need to do to make this work?
Adding the following to my _config.php has worked for me:
File::$allowed_extensions[] = 'kmz';
File::$allowed_extensions[] = 'kml';
File::$allowed_extensions[] = 'gpx';
I did not have to do anything else to allow these files types. The upload field worked and linking to the file on the front end worked.
this message means the upload to the temporary upload folder failed. you should check for the folder being writable or change it, see http://www.php.net/manual/en/ini.core.php#ini.upload-tmp-dir

Codeigniter and uploading zip files

I am currently building a backend to a site using the codeigniter framework, I have hit a bit of a problem, I needing a way to allow the user to upload a zipped folder of images, on completing the form, zipped folder must be unzipped, and the files need to be moved to a folder else where on the server, have thumbnail version of each image created and have there file name add to the DB, and also if the images are for a content type that does not already exist then I need to make a directory with that content type.
I know there is an upload class in Codeigniter, but I am not sure that, that has the capabilities do what I need, I could really do with some advice please?
Thanks
As Jan pointed out, this is a broad question (like 3 or 4 questions). I'm not up to date with the CodeIgniter framework but to Unzip the files you can do something like this:
function Unzip($source, $destination)
{
if (extension_loaded('zip') === true)
{
if (file_exists($source) === true)
{
$zip = new ZipArchive();
if ($zip->open($source) === true)
{
$zip->extractTo($destination);
}
return $zip->close();
}
}
return false;
}
Unzip('/path/to/uploaded.zip', '/path/to/extract/');
You wont be able to do any of the image or file checking using the Upload class. The upload class will let you accept the file and check it is a ZIP but that's as far as it will go.
From there, unzip the file and just do some simple PHP on the files to check they are the right type and make your folders etc. I would put this logic in a new library to keep it separated correctly.

Categories