How to check content of zip folder before uploading? - php

I have gone through the various post regarding zip and to read zip that is extracting zip downloading it and then access it. But i want to check the format and name convention for the files enclosed in Zip file that is going to be uploaded. I am using drag drop feature here and planning that when user drop the zip file it should extract the file but don't store it any where and check that all the image uploaded in the zip are of same format and name convention are followed like img1.jpg, img2.jpg.
Any help is deeply appreciated. I am using codeignitor framework and is it possible to extract and manipulate zip using backbone or java script.

I don't think you can check the contents of the zip before uploading.. Here's a code , you can very well use to check if the content inside is legit. [In this example... Assuming img1.zip contains an image file img1.jpg]
Maybe a start
<?php
$zip = new ZipArchive;
$res = $zip->open('img1.zip');
if ($res) {
$legitImage=explode('.',$zip->statIndex(0)['name']);
if($legitImage[1]=='jpg')
{
echo "It's an image";
//do your operations
$zip->close();
}
else
{
unlink('img1.zip');//Delete the zip file since it does not contain image
}
}
?>

Related

Unable to extract zip file which generated using php

I have written the PHP script to generate the zip file. it's working fine when I use rar software to extract it but not getting extract with rar software. I can't ask to users to install rar software to extract downloaded zip file.
I don't know where i am commiting mistakes.
Here i attached error screen shot which i get when try to open zip file.
// Here is code snippet
$obj->create_zip($files_to_zip, $dir . '/download.zip');
// Code for create_zip function
//create the archive
$zip = new ZipArchive();
if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach ($valid_files as $file) {
$filearr = explode('/', $file);
$zip->addFile($file, end($filearr));
}
$zip->close();
If $valid_files is a glob'd array, use basename() instead of end(), your zip might not actually have added any files causing for it to be an invalid zip (however that would be visible in the size of the zip file).
Also try winrar/winzip/7zip and see what they return, microsoft's internal zip engine might not be up to date enough to open the zips.
I have also encountered this problem, using 7z solved the problem but we need to send the zip to somebody else so 7z is a nono.
I found that, in my case it is that the file path is too long:
When I use this:
$zip->addFile($files_path.'/people.txt');
And it generated a zip folder nested very deep e.g. ["/tmp/something/something1/something2/people.txt"]
So I need to use this instead
$zip->addFile($files_path.'/people.txt', 'people.txt');
Which generate a a zip folder with only 1 layer ["people.txt"], and Windows Zip read perfectly~
Hope this helps somebody that also have this problem!

Saving Archive Comment to ePub file

I am working on a PHP application which downloads an ePub file. As part of the code I am adding a unique identifier which can be used to trace the original download information in a database to combat piracy.
I am using PHP ZipArchive functions to add an archive comment to the file but it seems that Adobe Digital editions is unable to open the ePub file if there is a comment present. When the comment is removed it opens fine so I just want to see if anyone else has had this problem before and if there is a way to add a comment to the ePub file which will allow Adobe Digital Editions to open it. Below is the code I'm using:
public function secureEpub($file, $download_id, $filepath){
//GENERATE RANDOM FILE NAME
$newFile = uniqid().".epub";
//COPY EPUB AND WORK FROM THAT VERSION
copy($file, $filepath.$newFile);
//TREAT EPUB FILE AS ORDINARY ZIP ARCHIVE
//OPEN AND EXTRACT EPUB
$zip = new ZipArchive;
$res = $zip->open($filepath.$newFile);
if($res === TRUE){
//ZIP ARCHIVE HAS OPENED SUCCESSFULLY - SET ARCHIVE COMMENT TO DOWNLOAD ID
$zip->setArchiveComment($download_id);
$zip->close();
} else {
error_log('Unable to open ePub (' . $filepath.$newFile . ') as Directory');
return false;
exit;
}
//ONCE CHANGE HAS BEEN MADE CLOSE THE ARCHIVE AND RETURN TEMP FILE
return $filepath.$newFile;
}
As you can see it copies the original, creates an Archive comment then returns the file (which is later deleted). Is there a way of achieving this without corrupting the ePub (for ADE)?
Let me know if I've not made any sense :D
P.S: Just to clarify, the download works fine and the comment is applied correctly. It is just that ADE is unable to open it while other ePub readers manage fine. Am I going about it in the wrong way?
OK, I couldn't find a way around this so I kind of came up with a solution. You can add a new file or directory inside the ZIP file which had the details included.
So, you can use something like:
$zip->addFromString('test.txt', $download_id);
or create a directory named after the download like:
$zip->addEmptyDir($download_id);
Would probably recommend being a bit more obscure with naming though if you get stuck like I did :)
Thanks

PHP - upload and overwrite a file (or upload and rename it)?

I have searched far and wide on this one, but haven't really found a solution.
Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.
Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.
So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.
I am thinking I will need to use PHP to
1) upload the file
2) delete the original song.mp3
3) rename the new file upload to song.mp3
Does that seem right? Or is there a simpler way of doing this? Thanks in advance!
EDIT: I impimented UPLOADIFY and am able to use
'onAllComplete' : function(event,data) {
alert(data.filesUploaded + ' files uploaded successfully!');
}
I am just not sure how to point THAT to a PHP file....
'onAllComplete' : function() {
'aphpfile.php'
}
???? lol
a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.
then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory
<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name = $_FILES['myupload']['name'];
$type = $_FILES['myupload']['type'];
$size = $_FILES['myupload']['size'];
$tmp = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
// echo "The file $filename exists";
// This will overwrite even if the file exists
move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way
?>
try this piece of code for upload and replace file
if(file_exists($newfilename)){
unlink($newfilename);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename);

How we can read zip file and get information of files or folders contains without unzipping in PHP?

What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)

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