PHP zip Function issue - php

Hello I am usign the below code to zip a package on upload:
$nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './CopyrightFiles/';
$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/>";
echo "yess !! Success!!!! ";
}
else echo " Oh No! Error";
It seems to work ok.
But there are two issues.
First issue it saves the file also with the original extension, something like : image.JPG.zip
Also when I then move the zip package to my local computer (Mac) and I open the ZIP inside I can find only a tmp folder with a binary file inside and NOT the image or the file that should be there!
What the hell is going on?
Please advise
Thank you

That's because your "binary" file is just the temporary name that PHP used to temporarily stored the uploaded file as. That's why it's "tmp_name" in the $_FILES array.
Try this:
$zip->addFile($tmpName, $nameFile);
The second parameter lets you specify what the file's name should be within the zip.
To prevent 'file.jpg.zip', try:
$fileconpress = $download_folder . pathinfo($_FILES['file']['name'], PATHINFO_FILENAME) . ".zip";

Issue #1:
You need to manually remove the extension:
$filename = explode('.', $_FILES['file']['name']);
$filename = $filename[0];
Issue #2:
$tmpName does not contain the filename of the file. You need to pass the $localname parameter in addFile(). See http://php.net/manual/en/function.ziparchive-addfile.php.

Related

how can I extract zip files, which was uploaded by user?

So, I want to create a system where user uploads in a zip file the files of a 3d model and the model can be shown, stored, etc
So, I got the file, I place it into a folder, permanently, And unzip it into another temp folder, just to see if it is a 3d model.
I tried like this:
$target_dir = "upload/";
$targetfilename = rand().$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $target_dir.$targetfilename);
//unzip the file into temp folder
$tmp_dir = $target_dir.rand();
mkdir($tmp_dir);
chmod($tmp_dir, 0777);
//chmod($targetfilename, 0777); //this not working, maybe isn't the right way
$zip = new ZipArchive;
$res = $zip->open($targetfilename);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($tmp_dir);
$zip->close();
echo 'SUCCESS';
} else {
echo 'ERROR';
}
I do not get any errors, but the zip can't be unzipped. Any idea? How can I resolve this?
Isn't $targetfilename is in $target_dir folder?
If so, changing
$res = $zip->open($targetfilename); to
$res = $zip->open($target_dir.$targetfilename); might solve your problem.

How do I correctly display a file extension from an HTML POST file using PHP?

I am attempting to echo the extension of an uploaded tax document. The document can be any image or a pdf. I plan on using the extension to rename the file with its proper extension later in the code.
Currently it does not echo the extension.
I have attempted a few different methods and looked at documentation and forums. I seem to still be doing something wrong.
My HTML:
<input type="file" class="" id="TaxFile" name="TaxFile" accept="image/*,application/pdf" ><br /><br />
My PHP:
$taxpath = pathinfo($_FILES["TaxFile"]["tmp_name"]);
echo $taxpath['extension'] . "\n123";
echos '123'
My Alternative PHP:
$taxpath = $_FILES["TaxFile"]["tmp_name"];
$ext = pathinfo($taxpath, PATHINFO_EXTENSION);
echo $ext;
echos nothing
I expected to see the echo:
"pdf
123"
and "pdf"
*My later code does in fact save the file to its destination using move_uploaded_file($_FILES["TaxFile"]["tmp_name"], $target_file). It's just that $target_file wont include the proper extension.
This should work for you if you use name instead of tmp_name
$path_parts = pathinfo($_FILES["TaxFile"]['name']);
$extension = $path_parts['extension'];
echo $extension;
tmp_name is the temporary name/location of the file on disk.
Try using $_FILES["TaxFile"]["name"] to get the original filename.
Otherwise, your method for grabbing the extension with pathinfo() and PATHINFO_EXTENSION was correct.
This will solve
$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
so in your file concatenate the variables
echo $path.'.'.$ext;

How do i change my file name after i have removed all '?

SORRY CAUGHT MY OWN ERROR AFTER POST
I have this code that i am running after a HTML from uploads a file to my .uploadAdmin.php section. Im having a lot of trouble with this.
$clientname = $_SESSION['MM_USERNAME'];
$extension = end(explode(".", $_FILES["file"]["name"]));
$myText = (string)$_FILES["file"]["name"];
$myText = str_replace("'", "", $myText);
print $myText; // This does come out with no '
My question is how do i change to file name after i have a new file name with no ' I have tried a few things including
$_FILES["file"]["name"] = $myText;
I had a comparison operator == instead of =,
Thanks Amy :)
Once you've uploaded your file you need to move it from the temporary directory to the place you want to store it. This is a security feature. The uploaded file will have a temporary file name. You can change the real filename as part of the move_uploaded_file() operation.
// where to store file
$uploads_dir = '/uploads';
// name of temporary file after upload.
$tmp_name = $_FILES["file"]["tmp_name"];
// original name of file, with apostrophes replaced
$name = str_replace("'", "", $_FILES["file"]["name"]);
// move the file
if (move_uploaded_file($tmp_name, "$uploads_dir/$name") === false) {
// do something if the file isn't an uploaded file.
}
Read the PHP reference Handling File Uploads for the complete reference.

uploadifive - change filename on upload

I am using uploadifive to upload images - however I want to change the name of the images to "newimage" I am not sure where or how to do this with what is provided - this is the last modification I need to deploy.
You're going to need to do the name change in the PHP script that handles the uploading (I'm assuming your using PHP since that's uplidfy's standard). It can be a bit tricky because you have to separate the incoming file name from it's extension. This should be enough to get you started.
$theFile = $_FILES['file_upl']['name'];
$tempFile = $_FILES['file_upl']['tmp_name'];
$newName = "newname";
$saveDir = $_SERVER['DOCUMENT_ROOT']."/images/";
//Function returns the extension from the '.' on
function get_ext($from_file) {
$ext = strtolower(strrchr($from_file,'.'));
return $ext;
}
//This will rename the file to $newname + the extension
$theFile = $newname.get_ext($theFile);
//Save the file
move_uploaded_file($tempFile,$saveDir.$theFile);
$file = 'uploaded_img.jpg';
$remote_file = 'Saved.txt';
ftp_put($conn_id, $remote_file, $file, FTP_ASCII)
just change the name of the remote file

PHP change file extension

I am trying to change a file exenstion, but whenever I do the file seems to corrupt.
$oldFileName = $targetDir . DIRECTORY_SEPARATOR . $fileName;
$newString = preg_replace('"\.tmp$"', '.jpg', $oldFileName);
rename($oldFileName, $newString);
The code works and changes the extension, but yet the file when downloaded, comes up as being corrupt.
The exension is .tmp and I am trying to change it to .jpg.
If I download the .tmp and manually change it to a .jpg it works, but not when the PHP does it.
Anyone know why this may be happening?
Thanks!
try this
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak'; //new file with extension
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>

Categories