Skip to content
Advertisement

execute php page that unzip a file (manipulate files)

Im using a local (rhel 8) server to unzip a file using php (solution proposed in : https://stackoverflow.com/a/8889126/9583635)

<?php
// assuming file.zip is in the same directory as the executing script.
$file = 'temperatures.zip';

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
  // extract it to the path we determined above
  $zip->extractTo($path);
  $zip->close();
  echo "Success! $file extracted to $path";
} else {
  echo "Failled! I couldn't open $file";
}
?>

The problem is:

  • when I execute this code on the server using: php /var/www/html/unzip.php it works fine (success message) and I find the extracted file in the same directory.
  • but when I use browser (http://192.168.1.5/unzip.php) the success message is printed but I can’t find the unziped file in this directory.

note: I tried using wamp server and it works, so the problem may be in the server.

Advertisement

Answer

By the way all the php line codes that were manipulating files wasen’t taking affect. I solved this by allowing the Apache user (apache in rhel) to create files in the directory. This can be done by making Apache the owner of the directory running this cmd:

sudo chown apache /var/www/html/

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement