extract Dropbox Zip on server using php - php

I'm building a website for my band. However, as I don't want to be the only one maintaining the site, I want to allow the other members to contribute via a dropbox-folder.
I have received a direct download link from dropbox that zip's the whole folder.
this links looks something like this:
http://www.dropbox.com/sh/xxxxxxxxxxxxxxx/yyyyyyyyyyyyyyyyyyyyyyyyy?dl=1
A folder test with 777 rights is available on the server side.
However the dropbox zip-file won't extract, and windows-created zip-file does.
<?php
echo PHP_OS.' PHP:'.phpversion();
echo '<br>starting download<br>';
$dbLink = 'http://www.dropbox.com/sh/xxxxxxxxxxxxxxx/yyyyyyyyyyyyyyyyyyyyyyyyy?dl=1'
$success = file_put_contents("./test.zip", fopen($dbLink, 'r'));
if ($success === FALSE) {
echo 'error storing zip';
} else {
echo 'success storing zip';
}
echo '<br>';
$zip = new ZipArchive;
$result = $zip->open('./test.zip');
if ($result === TRUE) {
echo 'opened zip<br>';
$success = $zip->extractTo('./test/');
if ($success === TRUE) {
echo 'unzip complete';
} else {
echo 'unzip failed';
}
$zip->close();
} else {
echo 'unable to open zip';
}
?>
I'd like to hear your thought on this.

Related

Simple test for file_exist fails

Simplest bit of test code:-
<?php
if (file_exists('https://mywebsite/testarea/test.html')) {
echo 'File exists';
} else {
echo 'Not found';
}
?>
I run this test from my localhost (wamp). Why does this not find the file? I've double checked that it exists in the path specified. Help please.
You can use file_get_contents()
if (file_get_contents('https://mywebsite/testarea/test.html')) {
echo 'File exists';
} else {
echo 'Not found';
}
Or just built this with the help of How to check if a file exists from a url
$ch = curl_init('https://mywebsite/testarea/test.html');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) {
echo 'File exists';
} else {
echo 'Not found';
}
file_exists() checks whether a file or directory exists in same system where your script is running.
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
If you are look for a file in remote place via http than use get_header()
<?php
$url = 'https://mywebsite/testarea/test.html';
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
echo 'url exists';
} else {
echo 'url does not exist';
}
?>
You need to use Path to the file or directory . See file_exists manual : https://www.php.net/manual/en/function.file-exists.php
and for file functions wrapper support : https://www.php.net/manual/en/wrappers.php
For your case something like,
<?php
if (file_exists('./testarea/test.html')) {
echo 'File exists';
} else {
echo 'Not found';
}
?>
On windows, use //computername/share/filename or \computername\share\filename to check files on network shares.
You are passing a file's url that is hosted somewhere else on a remote server rather than on your computer that's why it's not able to locate it.
If you have the same file in your localhost folder or anywhere on your computer where wamp has read access, it will pass through the check smoothly.
However, if you want to check if a specific url exists, then you might want to have a look at get_headers function:
$headers = get_headers('https://mywebsite/testarea/test.html');
if($headers && strpos($headers[0], 200)) {
echo 'URL exists';
} else {
echo 'URL does not exist';
}

Unable to open zip with ZipArchive

I'm running a php script (like bash).
This script do this:
Download a zip file from FTP server
ftp_get($ftp, $myFile.zip, $file, FTP_BINARY);
When I have downloaded the file, I want to extract its content:
$zip = new ZipArchive;
$res = $zip->open($myFile);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
} else {
dump( 'error, code:' . $res);
die;
}
But, I get the error 19 who is: "Not a zip archive"
If I open the .zip, it is a compressed archive because I have the PK key...
Does anyone have a solution or idea ?
Regards
Solutions
I am using this code and it work fine.
$zip = new ZipArchive;
$myfileDir = './abc.zip';
$extractDir = './';
$res = $zip->open($myfileDir);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
echo 'Successfully extract the file';
} else {
echo ('error, code:' . $res);
die;
}
Question
I believe the problem is because of the zip file, would upload the the zip folder to google drive and let us download and test it locally?

Zip Not Being Created

I am trying to create a page that simply zips files, that's all there is to it. I added the check to make sure the method was actually being called, and it is, I am seeing the "ok" when the code is run, however, no ZIP file is being created.
Any help would be appreciated. I can confirm that those files exist. (They are in relation to where the php file is)
$zip = new ZipArchive;
if ($zip->open('/files/custom.zip', ZipArchive::CREATE) === TRUE) {
$zip->addFile('/files/textures/16/default/pack.mcmeta');
$zip->addFile('/files/textures/16/default/pack.png');
$zip->close();
echo "ok";
} else {
echo "failed";
}
try this
<?php
$zip = new ZipArchive;
if ($zip->open('/files/custom.zip') === TRUE) {
$zip->addFile('/files/textures/16/default/pack.mcmeta', 'pack.png');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

making a new folder using php code

I just wanna ask, what is the code needed in my code in order this warning will not show
Warning: mkdir() [function.mkdir]: File exists in
C:\xampp\htdocs\php-robert\dir\dir.php
also did my program is correct? what i want in my program is, if the folder is not exist, make a folder, if its existing just do nothing.. nothing to show, just nothing
dir.php
<?php
$var = "MyFolder";
$structure = "../../file/rep/$var";
if (!mkdir($structure, 0700)) {
}
else
{
echo"folder created";
}
?>
Try the following:
$folder = "folder_name";
// if folder does not exist or the name is used, just not for a folder
if (!file_exists($folder) || !is_dir($folder)) {
if (mkdir($folder, 0755)) {
echo 'Folder created';
} else {
echo 'Unable to create folder';
}
}
if (!is_dir($structure)) {
mkdir($structure);
}
else
{
echo "folder already exists";
}
if (is_dir($structure) == false and mkdir($structure, 0700) == false)
{
echo "error creating folder";
}
else
{
echo "folder exists or was created";
}
You could also test if a file exists, but it isn't a folder

Get ZIP file from generated URL

I have a URL that, when requested, start the download of a .zip file. The URL looks something like this:
http://website.nl/servlets/ogexport?koppeling=WEBSITE&user=&password=&og=BOG&kantoor=**
How can I make sure that the zip file that gets generated in that proces get used in my PHP unzip script?
$file = 'testing.zip';
$path = pathinfo(realpath($file), PATHINFO_DIRNAME).'/xml';
// unzippen
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo 'success';
// debug("ZIP bestand uitgepakt", 2);
} else {
// debug("ZIP niet uitgepakt", 2);
echo 'something went wrong.';
}
$xml_path = $path."/testing.xml";
echo $file.'<br>';
echo $xml_path;
Hoping for help!
I would recommend using php curl to download the remote file.Here is an example

Categories