How can I validate files in a zip file using finfo_open(FILEINFO_MIME_TYPE) before extracting them using extractTo?
I am using the ziparchive class: http://php.net/manual/en/class.ziparchive.php
$zip = new ZipArchive();
if ($zip->open($_FILES['upload_file']['tmp_name']) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$name=$zip->getNameIndex($i);
$pieces=explode(DS,$name);
if(substr($name, -1)==DS) {
//validate directory structure if desired
}
else {
//validate file
$mime=$finfo->buffer($zip->getFromIndex($i));
}
}
}
Related
i modified my upload code based on this post https://stackoverflow.com/a/30074716/14787718 (its working code)
but my code does not work, please help.
my full code here: https://sandbox.onlinephpfunctions.com/code/c62b6bdf6fadd5aff63b2e7e65e75c1075d1dbb0
<?php
if (isset($_POST['upload']) && $_FILES['image']['error']==0) {
$j = 0; //Variable for indexing uploaded image
for ($i = 0; $i < count($_FILES['image']['name']); $i++) {//loop to get individual element from the array
$allow_ext = array('png','jpg','gif','jpeg','bmp','tif');
$allow_type = array('image/png','image/gif','image/jpeg','image/bmp','image/tiff');
$image_name = $_FILES['image']['name'][$i];
$image_type = getimagesize($_FILES['image']['tmp_name'][$i]);
$image_name = explode('.',$image_name);
$ext = end($image_name);
$j = $j + 1;//increment the number of uploaded images according to the files in array
if(in_array($ext, $allow_ext) && in_array($image_type['mime'], $allow_type)){
list($width, $height, $mime) = getimagesize($_FILES['image']['tmp_name'][$i]);
if ($width>0 && $height>0) {
$upload = move_uploaded_file($_FILES['image']['tmp_name'][$i], "uploads/".$_FILES['image']['name'][$i]);
if ($upload) {
echo '<p>File Uploaded: View Image</p>';
}
} else {
echo 'Error: Only image is allowed!';
}
} else {
echo 'Error: Invalid File Type!';
}
}
}
?>
thank you!
first move the Jquery call to place it before bootstrap:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/js/bootstrap.min.js'></script>
And at your first test : if (isset($_POST['upload']) && $_FILES['image']['error']==0). $_FILES['image']['error'] is an array.
Delete this test to keep only if ( isset($_POST['upload']) )
Add this test in you for step:
for ($i = 0; $i < count($_FILES['image']['name']); $i++) {//loop to get individual element from the array
if ($_FILES['image']['error'][$i]==0) {
Tested, it works fine!
i am trying to make an array of files which are part of a .zip file.
In the .zip file are 2 files: image1.jpg and image2.jpg
$zip = new ZipArchive;
if ($zip->open($_POST['extractfile']) === TRUE) {
$unzipped = 0;
$fails = 0;
$total = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
$total ++;
echo $zip->getNameIndex($i);
The echo outputs only the first file: image1.jpg
How can i make an array of the files which are in the .zip file so that i can use a foreach loop like below:
foreach($extractfiles as $extractfile) {
echo $extractfile;
}
To the second part
<?php
$zip = new ZipArchive;
$extractfiles = [];
if ($zip->open($_POST['extractfile']) === TRUE) {
$unzipped = 0;
$fails = 0;
$total = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
$total ++;
echo $zip->getNameIndex($i);
$extractfiles[] = $zip->getNameIndex($i);
}
}
foreach($extractfiles as $extractfile) {
echo $extractfile . "<br>" . PHP_EOL;
}
In PHP I am uploading a multiple file. But I want upload each file in different folder.
I Upload 9 file at a time.
So, Current Scenario is:-
Ex:- Main folder -> pdf-files ->all uploaded pdf files
But I want as:-
Main Folder->pdf-files->New Folder->pdf-file-1.
Again,
Main Folder->pdf-files->New Folder->pdf-file-2.
Main Folder->pdf-files->New Folder->pdf-file-3.
.......
So On
Here is my upload.php file:-
$output_dir = "pdf-files/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$fileName = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
$ret[$fileName]= $output_dir.$fileName;
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$fileName = $_FILES["myfile"]["name"][$i];
$ret[$fileName]= $output_dir.$fileName;
$ret[$fileCount] = $fileCount;
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName );
}
}
}
echo json_encode($ret);
}
In each iteration of your for loop, add the index to the the output_dir as the name of the New Folder.that way you get a new folder for every file.
$output_dir = "pdf-files/";
if(isset($_FILES["myfile"]))
{
$ret = array();
$error =$_FILES["myfile"]["error"];
{
if(!is_array($_FILES["myfile"]['name'])) //single file
{
$fileName = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
$ret[$fileName]= $output_dir.$fileName;
}
else
{
$fileCount = count($_FILES["myfile"]['name']);
for($i=0; $i < $fileCount; $i++)
{
$fileName = $_FILES["myfile"]["name"][$i];
$ret[$fileName]= $output_dir.$fileCount."/".$fileName;
$ret[$fileCount] = $fileCount;
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileCount."/".$fileName );
}
}
}
echo json_encode($ret);
}
I want to read a file inside two zip folders...
My tree:
main_zip_folder
-second_zip_folder
--file_to_read.xml
It's simple to enter in the first_zip_folder and list all the files persent in this folder. I'm using this example:
//create a ZipArchive instance
$zip = new ZipArchive;
//open the archive
if ($zip->open('data/main_zip_folder.zip') === TRUE) {
//iterate the archive files array and display the filename or each one
for ($i = 0; $i < $zip->numFiles; $i++) {
echo 'Filename: ' . $zip->getNameIndex($i) . '<br />';
}
} else {
echo 'Failed to open the archive!';
}
My problem is how to enter in the second_zip_folder to list the files present on it, and open the file_to_read.xml?
I hope it can be help you, This code below help you for reading to zip files or more
<?php
$za = new ZipArchive();
$files = array('zip1.zip', 'zip2.zip');
//$za->open('zip1.zip', 'zip2.zip');
foreach ($files as $fil){
$za->open($fil);
for( $i = 0; $i < $za->numFiles; $i++ ){
$stat = $za->statIndex( $i );
print_r( basename( $stat['name'] ) . PHP_EOL . '<br>');
}
}
?>
i am using this piece of code to unzip a .zip file
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Lets say: there is a .php file in the .zip and i do not want a .php file to be extracted. How can i prevent that?
You can try something like this for PHP >= 5.5:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
if( pathinfo($zip->getNameIndex($i)['extension'] != "php")){
$zip->extractTo('/my/destination/dir/', $zip->getNameIndex($i));
}
}
$zip->close();
}
Or this for PHP < 5.5:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
if( $ext != "php")){
$zip->extractTo('/my/destination/dir/', $zip->getNameIndex($i));
}
}
$zip->close();
}
The only difference between the two is the pathinfo function. Both will loop all files inside the zip file and, if the file extension isn't php, extracts it to /my/destination/dir/.
$zip= new ZipArchive;
if($zip->open('test.zip') === TRUE){
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = pathinfo($zip->getNameIndex($i));
$fileinfo = $filename['extension'];
if($fileinfo!="php"){
$zip->extractTo('extract/',$zip->getNameIndex($i));
}
$zip->close();
}