-1

I wish to check whether or not the $outZipPath is a valid one. When I have an incorrect drive, such as:

$outZipPath = 'k:/backup.zip'     // k drive does not exist

$opened is still true, hence 'Path created' is always echoed.

How can an incorrect drive/path be checked?

The code is shown below:

$z = new ZipArchive(); 
$opened = $z->open($outZipPath, ZIPARCHIVE::CREATE); 
if ($opened === true) 
    echo 'Path created';
else
    echo 'Not valid path';
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
havelly
  • 109
  • 1
  • 11
  • I can see a path not existing but are there instances where your script may choose a non-existant drive? You can use `is_dir()` to check a path exists. – Rasclatt Sep 24 '15 at 02:59
  • To check for a drive, you could suppress the warning from `disk_total_space()` like: `echo (@disk_total_space(":k"))? 'is drive':'is not drive';` I don't know if that is a lot of overhead though, the php manual doesn't say anything about performance for this function. – Rasclatt Sep 24 '15 at 03:08
  • is_dir() does allow checking for the drive. It begs the question though what is the point of having the if ($opened === true) condition. $z->open() opens/creates the path so how can it not give an error msg if it can not do so? – havelly Sep 24 '15 at 03:57
  • It's going to try and create a new archive likely in the same folder as the script is in, only in a folder named `:k`. If you `print_r($z);` you can see what it's trying to do and why you get `true` back. – Rasclatt Sep 24 '15 at 04:08
  • I should also mention, I am using linux, so it may not be the case in your's but see what printing the `$z` gets you. – Rasclatt Sep 24 '15 at 04:10

1 Answers1

0

You could use the touch function to try to create a file. Placing the @ in front of the function name prevents PHP from reporting an error.

<?php
$outZipPath = 'test/this';
$t = @touch($outZipPath);
if ($t === true) {
        $z = new ZipArchive();
        $opened = $z->open($outZipPath, ZipArchive::CREATE|ZipArchive::CHECKCONS);
        if ($opened === true)
            echo 'Path created';
        else
            echo 'Not valid path';
} else {
        echo 'No luck';
}
echo PHP_EOL;
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • Thanks for that. It does not solve the problem though why the if condition does not appear to picking up a non-existant drive in $outZipPath. – havelly Sep 24 '15 at 04:00