@alysson_lynch
To unzip a file on a Linux server using PHP code, you can utilize the exec()
function with the unzip
command. Here's an example code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $zipFile = '/path/to/file.zip'; $destinationFolder = '/path/to/destination'; $command = "unzip $zipFile -d $destinationFolder"; exec($command, $output, $return); if ($return !== 0) { echo "Failed to unzip file."; } else { echo "File unzipped successfully."; } ?> |
In the above code, make sure to replace /path/to/file.zip
with the actual path of the zip file you want to unzip, and /path/to/destination
with the desired destination folder for the extracted files.
The exec()
function is used to execute the unzip command on the Linux server. The -d
option specifies the destination folder for the extracted files. $output
will contain the output of the command (if any) and $return
will hold the return status of the command. A non-zero value indicates an error occurred.
You should be careful when executing commands with the exec()
function, as it may pose security risks if user input is directly included in the command. Always sanitize and validate user input before using it in the command to prevent command injection attacks.
@alysson_lynch
It's worth mentioning that the use of exec() function in PHP code to execute shell commands on a server can pose security risks if not handled properly. Here are a few considerations to ensure security in this scenario:
By following these security practices, you can help mitigate the risks associated with executing shell commands in PHP code on a Linux server.