I am new to PHP and here is my dilemma. I am trying to add to my site a feature that allows users to select a set of images in Flash (AS3) click a button and have my server create a zip file of all the images for them to download.
I have already wrote my code in flash and crated my php script and tested it on my localhost and everything works great. But when I upload the same files to my sever no zip files are created. Below is the code for both my flash and for my php. Any help or tips would be appreciated I am struggling over her.
Localhost server is using 5.2.17 and so if my Server hosted on 1&1
Flash Code To Send Info to PHP
function MakeZip():void
{
var variables:URLVariables = new URLVariables();
//variables.Image1=img1;
variables.ImageList=OrderItems;
variables.Name= zipName_txt.text + ".zip";
variables.FileComplete="File Created";
var request:URLRequest = new URLRequest();
request.url="ArrayTest.php";
request.data=variables;
var loader:URLLoader = new URLLoader();
loader.load(request);//sends the request
//when the request is done loading, it goes to the completeWriting function
loader.addEventListener(Event.COMPLETE, completeWriting);
loader.addEventListener(IOErrorEvent.IO_ERROR, error);
function completeWriting(event:Event):void
{
info_txt.text = "File Created";
}
function error(e:IOErrorEvent):void
{
info_txt.text = "There was an error. Please try again later.";
}
}
PHP to create file
<?PHP
$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// add files
foreach ($fileList as $f) {
$zip->addFile($f) or die ("ERROR: Could not add file: $f");
print false;
}
// close and save archive
$zip->close();
//writeVariable( "ZipComplete", $ZipComplete);
echo "FileComple=" . $ZipComplete;
?>
Thank you all for your tips and hints. After hours of trying different things I found the mistake was in my flash code. On my localhost it ignores capitalization for file names. On my server however it does not ignore it. I had in my flash file image1.jpg but the file name is actually Image1.jpg. Glad I found it bit it is so frustrating how a little thing like that made it not work.
is there any error response returned from you php script?
please make sure the zip file save path on server is correct and have write access
This is because code is running as "nobody" on server. This user would not have write access to anywhere except /tmp .
Add following code, this should work:
<?PHP
$zipName= $_GET['Name'];
$Order= $_GET['ImageList'];
$fileList = explode('|',$Order);
$ZipComplete = $_GET['FileComplete'];
// create object
$zip = new ZipArchive();
$zipName = "/tmp/" + $zipName;
// open archive
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$zipName = "/tmp/" + $zipName; is the key part.
Related
I am trying to extract zip file in codeigniter 4... I have the code below
$file_name = session()->get('file_name');
// Zip file name
$filename = "/public/$file_name";
$zip = new \ZipArchive;
$res = $zip->open($filename);
if ($res === true) {
// Unzip path
$path = "/public";
// Extract file
$zip->extractTo($path);
$zip->close();
return 'Site Updated Successfully';
} else {
return "failed to update $filename!";
}
}
but I get this result: return "failed to update $filename!";
Please can someone help me fix the issue with the code.
This has nothing specifically to do with Codeigniter. It's just using PHP and ZipArchive. The code looks OK - be sure the file is really in the /public/ directory and that it is a zip archive. The PHP site has examples that are like $zip = new ZipArchive; (without the \ char); maybe try that.
I have a folder in my web server were I put zip files that I need to then unzip. I want to do that with php and this is what I have tried but it does not work:
<?php
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file+'.zip');
$zip->extractTo('./');
$zip->close();
?>
The zip files are in the same folder as the php file, but when I go to the php page it does nothing.
By doing some testing I have found out that the script dies on the $zip = new ZipArchive; line
How can I manage this to work?
<?php
$fileName = $_GET['file']; // get file name in the URL param "file"
if (isset($fileName)) { // if $fileName php variable is set than
$zip = new ZipArchive; // create object
$res = $zip->open($fileName); // open archive
if ($res === TRUE) {
$zip->extractTo('./'); // extract contents to destination directory
$zip->close(); //close the archieve
echo 'Extracted file "'.$fileName.'"';
} else {
echo 'Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))';
}
}
else {
echo 'Please set file name in the "file" param';
}
?>
Note:- For More Details Please refer https://www.php.net/manual/en/class.ziparchive.php
I have found the problem.
The code is fine, but the hosting service is not, and they do not have the ZIP extension available right now
Try this code. Also change $zip->open($file+".zip"); to $zip->open($file);.
+ (plus sign) is not concatenation operator in php
<?php
// $_GET["file"] is set to `a.zip`
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file);
$zip->extractTo('./');
$zip->close();
?>
Ok so this is making me crazy... I abstracted the code because it comes from a big project. But in my project I ended up commenting everything and only have this left which is still causing problems and I have no idea why.
$f = fopen('tmp/'.$name.'.zip', 'wb');
fwrite($f, $myzip);
fclose($f); //I can open this file manually and everything is fine
$zip = new ZipArchive;
$res = $zip->open('tmp/'.$name.'.zip'); //$res is "1"
$zip->extractTo("final/" . $unique);
$zip->close();
As you can see I write a zip file in /tmp, at this point, I can open the file manually and it contains all files with the correct size.
But after I extract it to /final, for some reason some files are empty...
Any idea what could cause this?
You can do this way by throwing Exception at zip archive open time,
function DecompressFile()
{
$zip = new ZipArchive;
if ($zip->open('tmp/'.$name.'.zip') === TRUE) {
$zip->extractTo("final/" . $hwidDir);
$zip->close();
return 'completed';
}
else {
throw new Exception ("Decompress operation from ZIP file failed.");
}
}
Imagine there is a picture at http://example.com/icon.jpg and I want to add it to a zip file on my own sever named "Stack.zip" using php. This is my code, but it doesn't work.
$url="http://example.com/icon.jpg"
$zip = new ZipArchive;
echo $zip->open("Stack.zip");
$zip->addFile($url);
$zip->close();
P.S. I was able to do it with local files, but I had no success on doing it with internet addresses. So that's why I asked this question.
From the PHP manual: http://php.net/manual/en/ziparchive.addfile.php
(PHP 5 >= 5.2.0, PHP 7, PECL zip >= 1.1.0)
This assumes you want to add to an existing zip file on your server.
$url = 'https://www.stackoverflowbusiness.com/hubfs/logo-so-color.png?t=1499443352566';
$local_path = '/your/local/folder/';
$img = 'icon.jpg';
file_put_contents($local_path.$img, file_get_contents($url));
$zip = new ZipArchive;
if ($zip->open($local_path.'Stack.zip') === TRUE) {
$zip->addFile($local_path.$img, $img);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
If you want to keep the same folder structure as the original source file, change this line ..
$zip->addFile($local_path.$img, $img);
...to this...
$zip->addFile($local_path.$img);
If you want the same script to create the zip, you can find a PHP function here: Add files to the zip
For some reason I can't seem to get the following code to work. I have a form which accesses it, and I've passed a .zip file through it but still no luck? Any clues?
<?php
$file = $_POST['zipped_file'];
$zip = new ZipArchive();
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
$zip->extractTo('../img/media');
$zip->close();
echo 'Archive extracted to directory';
?>