php unzip remote file - php

I need to unzip file, witch is in remote http server. And I have a code
<?php
$path = "http://mydomain.com/zipfile.zip";
$zip = new ZipArchive;
if ($zip->open($path, ZIPARCHIVE::CREATE) === TRUE)
{
$zip->extractTo('zip/');
$zip->close();
echo "ok';
}
else
{
echo 'failed';
}
?>
The result after execution is "ok", but file did'n extract. Where can be problem?
P.S. in archive is only one .csv file

It seems it unzipped the file in your local machine instead, because you're executing this php locally. You probably will find this file in your PHP temp folder.
If you want it working on server, you should place this file on the server (let's say you call it unzip_file.php), change the $path to absolute path (something like /my/path/zipfile.zip).
You could download it straightway using php headers:
header('Content-Disposition:attachment; filename="' . $sFilename . '"');
Call this program instead: http://mydomain.com/unzip_file.php

Related

Read a file from a ZIP file on FTP with PHP without downloading whole archive

I want to read a the .txt file content from a .zip file that contains many other .txt files. But hope do not need to download the .zip file to the local machine..
How can I use functions like zip_open/zip_read to do it?
$path = 'Data/2021';
ftp_login($conn,$user,$pass);
ftp_chdir($conn,$path);
ftp_pasv($conn,TRUE);
foreach (ftp_nlist($conn,".") as $Lot_Zip) {
$Lot_Zip = Trim($Lot_Zip);
$Lot_Zip_Name = substr($Lot_Zip,0,10);
if (preg_match("/$Lot_Zip_Name/i",$ParsedLot_String)){
echo "processing ", $Lot_Zip, " ...\n";
// $Lot_Zip = $path.'/'.$Lot_Zip also can't be zip_open
$zip = zip_open($Lot_Zip);
echo "ZIP: ", $zip ,"\n"; //return value is 11
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
echo "Name: " . zip_entry_name($zip_entry) . "\n";
}
}
}
}
Opening a connection to an FTP server won't make functions magically use files on the FTP server. The $Lot_Zip in zip_open($Lot_Zip) refers to the local file on the machine that runs your PHP script (webserver?), not files on the FTP server. With some PHP functions you can use ftp:// URL wrapper to refer to the files on the FTP server. But I believe that neither the deprecated zip_open, nor the up-to-date FtpArchive support the URL wrappers. They can work with local files only.
So the easiest solution is to download the archive from the FTP server to the local machine/webserver.
In general, it's rather complicated to work with ZIP files on an FTP server. Working with ZIP files requires a random access to the file contents, what is not easy to achieve with the FTP protocol.
See this related (Python) question that discusses the problem into more details:
Get files names inside a zip file on FTP server without downloading whole archive

How do I unzip a 5GB zip file on my web server without PHP timeout?

How do I unzip a 5GB zip file on my web server without PHP timeout?
I need to unzip a 5GB zip file on server.
When I run the unzip it times out because the file extraction takes too long.
I do not have access to shell console, all I have is a FTP and I can execute PHP by accessing the PHP file in my web browser.
How do I unzip the large file?
$dirName = $_POST["dirName"];
echo "$dirName<br>\n";
$dirName = htmlspecialchars($dirName);
echo "$dirName<br>\n";
flush();
$zipFileName = getcwd() . "/$dirName/aTest.zip";
$destDirName = getcwd() . "/$dirName/aTest";
echo "Zip file is $zipFileName<br>\n";
echo "Directory is $destDirName<br>\n";
flush();
// Unzip selected zip file
$zip = new ZipArchive;
$res = $zip->open($zipFileName);
if ($res === TRUE) {
// Extract file
$zip->extractTo($destDirName);
$zip->close();
}
echo "Successfull<br>\n";
flush();
I found an answer here but the link ("try PclZip") does not work.
following is the restrictions as per PHP Info
disable_functions
exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,
proc_open,ini_alter,dl,popen,popen,pcntl_exec,socket_accept,socket_bind,
socket_clear_error,socket_close,socket_connect,socket_create_listen,
socket_create_pair,socket_create,socket_get_option,socket_getpeername,
socket_getsockname,socket_last_error,socket_listen,socket_read,
socket_recv,socket_recvfrom,socket_select,socket_send,socket_sendto,
socket_set_block,socket_set_nonblock,socket_set_option,socket_shutdown,
socket_strerror,socket_write,stream_socket_server,pfsockopen,
disk_total_space,disk_free_space,chown,diskfreespace,getrusage,
get_current_user,set_time_limit,dl,leak,listen,chgrp,link,symlink,
dlopen,proc_nice,proc_get_stats,proc_terminate,shell_exec,sh2_exec,
posix_getpwuid,posix_getgrgid,posix_kill,ini_restore,mkfifo,dbmopen,
dbase_open,filepro,filepro_rowcount,posix_mkfifo,_xyec,mainwork,
get_num_redirects,putenv,geoip_open,sleep,imap_open
You can try to change max-execution-time value with
<?php
ini_set('max-execution-time', 600); // 600 seconds
If it's not working, you should ask to the administrator of the server.

ZipArchive is not creating - addFile() returns false

I want to zip a file using PHP. The following code works fine with XAMPP on my local machine, but not on hosting site.
First I thouught it was file permission problem, but I changed it to 777 and nothing changed.
Secondly, I read somewhere on Internet that zip extension may not be loaded but funtcion extension_loaded('zip') return true and there is no errors.
Thirdly, I have checked the result of zipArchive funtions - in my case, $zip->open() returns true, $zip->addFile() return false and $zip->close() return true. But zip file isn't created. It seems like permissions issue, but I have set 777 for both .php and .txt file which I want to add to a zip file.
PHP version on my local machine: 7.3.4
PHP version on my hosting provider: 7.3.1
$zip = new ZipArchive();
$DelFilePath="raport_X_.zip";
if(file_exists($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$DelFilePath)) {
unlink ($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$DelFilePath);
}
$openResult = $zip->open($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$DelFilePath, ZIPARCHIVE::CREATE);
$file='BranżowaSzkołaIstopniamechanikpojazdówsamochodowychGIMNAZJUM_RAPORT.txt';
$addResult = $zip->addFile($_SERVER['DOCUMENT_ROOT']."/www/Rekrutacja/raports/".$file,$file);
$closeResult = $zip->close();
$path = 'raports/raport_X_.zip';
$filename = "raport_X_.zip";
if(file_exists($path)){
header("Content-Type:application/zip");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-control: private");
header('X-Sendfile: '.$path);
readfile($path);
exit;
}
echo '<br>';
if(extension_loaded('zip')){
echo 'Zip extension is loaded <br>';
}
echo 'Open result:';
var_dump($openResult);
echo 'Add error:';
var_dump($addResult);
echo 'Close error:';
var_dump($closeResult);
phpinfo();
?>
I simply want to get a zip with a .txt file in it. I am getting no error, but zip file isn't created. It works on XAMPP, but not on hosting. I wasted a lot of time on it and now I got no idea.

How to open a ZIP using remote url?

I want to open a ZIP file by passing a remote URL (http://www.example.com/file.zip or http://localhost/wordpress/wp-content/uploads/file.zip) instead of a file location (C:\wamp\www\wordpress\wp-content\uploads\file.zip)
This constructor works fine for a file location but not for a remote url of a file. How does one open a file using a remote URL for this scenario?
public function __construct($file = false)
{
if ($file && is_file($file)) {
//$file="C:\wamp\www\wordpress\wp-content\uploads\file.zip" here
$this->open($file);
$this->fileName = basename($this->filePath = $file);
} else {
throw new \Exception($file . " not a regular file");
}
}
The safest way is to
download the file
This is super easy if allow_url_fopen is enabled: file_get_contents() accepts remote URLs. If that's not enabled, use cURL or a Wordpress HTTP helper to download it.
save it locally
Also super easy, with file_put_contents(). The /tmp folder is probably writable for you. On Windows, I don't know where the tmp folder lives.
open it like any other
As you would a local ZIP archive, with ZipArchive::open() or your nameless class
just use php fopen function
http://php.net/manual/en/function.fopen.php
$handle = fopen("http://www.example.com/", "r");
I have used this to get the contents of a webpage but not a zip file - not sure if that will work but it did well for me. $contents definitely worked for text.
// For PHP 5 and up
$handle = fopen("https://www.thesiteyouwant.com/the_target_file.ext", "r");
$contents = stream_get_contents($handle);
http://php.net/manual/en/function.stream-get-contents.php

How to execute .sh files one after another with php

I need to run a series of six .sh files on the server.
An example of one of the .sh files:
wget ftp://xxxxxx:xxxxxx#ftp.interhome.com/accommodation.xml.zip
unzip accommodation.xml.zip
php accommodation.php
rm -rf accommodation.xml.zip
rm -rf accommodation.xml
I tried running the following from a php file:
echo shell_exec('sh accomodation.sh');
Which was stupid because the file appears to execute repeatedly and I think I've just taken down the server. Whoops.
I've inherited this site and have never used .sh files before. I'm also a php novice.
How would I go about running the files only once and then running the next?
Many thanks
you can do all this from within PHP, you do not need any shell-script.
/* get the file via ftp */
// connect to server
$ftp = ftp_connect('ftp.interhome.com');
// login
$login = ftp_login($ftp,"username","password");
// download file to tmp.zip
$file = ftp_get($ftp, 'tmp.zip', 'accommodation.xml.zip', FTP_BINARY);
// disconnect from server
ftp_close($ftp);
/* unzip the file */
// new zip-instance
$zip = new ZipArchive;
// open downloaded file
$res = $zip->open(’tmp.zip’);
// check if file is readable
if ($res === TRUE) {
// extract to current directory
$zip->extractTo(’./’);
// close zip-file
$zip->close();
}
/* your code from accommodation.php goes here */
// delete files
unlink('tmp.zip');
unlink('accommodation.xml');
voila

Categories