zip file created but not moving to folder in php - php

files are being uploaded and added to zip the only problem is to move the zip to desired location.the permission is OK i have moved single file. Now i want to create a zip containing multiple files and then moving that zip to folder.
$uploaddir = 'upload/page/';
if($_FILES['image_file_holder']['name'] != '')
{
$total = count($_FILES['image_file_holder']['name']);
echo $total;
$imagarr = explode(".", "myserver.zip");
$newimgfile = $imagarr[0]."_".mt_rand().'.'.$imagarr[1];
$zipname = $newimgfile;
$upload = $uploaddir . $newimgfile;
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['image_file_holder']['name'][$i];
if ($tmpFilePath != ""){
$zip->addFile($tmpFilePath);
}
}
$zip->close();
echo "moving";
if(move_uploaded_file($zipname,$upload))
{
echo "done";
}
}

Related

uploading file and moving it to a folder

I have a FPDF script that submits a PDF and moves it to a folder on the server.
I have a upload field in the form before the FPDF script is run named "file"
i am trying to move it to the same folder as the generated PDF.
below is my code: (the PDF is generated and moved to the folder but nothing happens with the uploaded file)
mkdir("claims/$name", 0777);
$move = "claims/$name";
foreach ($_FILES["files"]["tmp_name"] as $key => $value)
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name2 = $move ."\\".basename($_FILES["files"]["name"][$key]);
move_uploaded_file($tmp_name, $name2);
}
$filename = "claims/$name/$name.pdf";
$pdf->Output($filename, 'F');
header('Location: home.php');
I was able to get this working with a few changes to my code. I also got it to work with multiple uploads.
// make the directory
mkdir("claims/$name", 0777);
$total = count($_FILES['files']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "claims/$name/" . $_FILES['files']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}

PHP Multiple file upload then attach inline to email via PHPMailer

I'm having a spot of bother with PHPMailer and have searched high and low for this information but nada.
Basically I have a form where the user can upload multiple images which are then saved to a folder on my server, then I need to send an email with the images attached inline at the bottom of the email via PHPMailer.
I can get it to put the first image as inline but the rest of the images do not appear..
Some code:
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = "../reports/".$id."/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
$mail->AddEmbeddedImage($filePath, $shortname, $shortname);
$atts = '<img src="cid:'.$shortname.'">';
}
}
}
}
$mail->Body .= $atts;
Solution:
$dir2 = opendir('../reports/'.$id); // Open the directory containing the uploaded files.
$files = array();
while ($files[] = readdir($dir2));
rsort($files);
closedir($dir2);
foreach ($files as $file) {
if ($file != "." && $file != ".." && $file != 'resources' ){
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$url = '../reports/'.$id.'/'.$file;
$mail->AddEmbeddedImage($url, $withoutExt);
$mail->Body .= '<img src="cid:'.$withoutExt.'">';
}
}

upload file then zip and put it for download

I want let user upload their resume on the server,these file can be every suffix,then i will zip these file and put them for other users to download by a link,is it safe enough???!!!
here is my code
$nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = "/download/";
$zip = new ZipArchive;
$fileconpress = $download_folder.$nameFile.".zip";
$conpress = $zip->open($fileconpress, ZIPARCHIVE::CREATE);
if ($conpress === true)
{
$zip->addFile($tmpName);
$zip->close();
echo $fileconpress."<br/>";exit();
//return $name;
}
else echo " Oh No! Error";
and above code apparently does not work,please help me to have safe app,thanks

php zip archive is adding the files with no size

I got this working but when i look into the zip folder all the files size is 0. I tryed not adding the files to the zip and that worked, but when i try to add them to a zip the size goes to 0. Why is this.
here is my php
if(isset($_FILES['file'])){
$file_folder = "uploads/";
$zip = new ZipArchive();
$zip_name = time().".zip";
$open = $zip->open("zip/".$zip_name, ZipArchive::CREATE);
if($open === true){
for($i = 0; $i < count($_FILES['file']['name']); $i++)
{
$filename = $_FILES['file']['name'][$i];
$tmpname = $_FILES['file']['tmp_name'][$i];
move_uploaded_file($tmpname, "uploads/".$filename);
$zip->addFile($file_folder, $filename);
}
$zip->close();
if(file_exists("zip/".$zip_name)){
// zip is in there, delete the temp files
echo "Works";
for($i = 0; $i < count($_FILES['file']['name']); $i++)
{
$filenameu = $_FILES['file']['name'][$i];
unlink("uploads/".$filenameu);
}
} else {
// zip not created, give error
echo "something went wrong, try again";
}
}
}
Your problem lies with this line: $zip->addFile($file_folder, $filename);
Currently that passes a path to the /uploads/ directory as the first argument.
According to Zip::addFile documentation you should be passing the path to the file to add (this includes the file and extension).
So change your code to include the file name (you already have it as a variable $filename which is handy).
$zip->addFile($file_folder.$filename, $filename);

getting specific type of file and adding them to an existing zip file

$zip = new ZipArchive();
$filename = "image/files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFile("$File", basename($File));
$zip->close();
I have added one file already in the zip, which is in variable '$File'. That file exists in the same folder (images). I need to add images in the same zip file. Those images are located inside 'images' folder. I guess I need to scan the images and add them into to same zip. But I'm not sure how to do that.
How can I get specific type(jpeg, png, gif) of files added into an existing zip file or a new one? I have to add images in the root of my zip file, not inside any folder within the zip file. The images are inside one directory('images') and I need to add those into my zip file, which is also saved inside the same directory.
$zip = new ZipArchive;
if ($zip->open('source.zip') === TRUE)
{
for($i = 0; $i < $zip->numFiles; $i++)
{
$fp = $zip->getStream($zip->getNameIndex($i));
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents = fread($fp, 8192);
// explode for extension and do what ever you want
}
fclose($fp);
}
}
else
{
echo 'Error reading zip-archive!';
}
This is how I solved it.
$dir = './property_image';
$file1 = scandir($dir);
foreach($file1 as $feel){
$userfile_extn = substr($feel, strrpos($feel, '.')+1);
if($userfile_extn === 'jpg' || $userfile_extn === 'png' || $userfile_extn === 'gif' || $userfile_extn === 'jpeg'){
if(in_array($feel, $prop_image)){
//echo $feel;
$zip->addFile("./property_image/$feel", basename($feel));
}
} // end of if($extension ===
} // end of foreach

Categories