Search code examples
phphtmltarphar

PHP Tar Extract w/ Variable


Understanding that the following question is not best practices by any means. The tool i am building is for personal use and will not be accessible to the internet in general. With that being said here is the idea or outline of what i am trying to accomplish.

PHP:

define("UPLOAD_DIR", "/var/www/html/uploads/");

if (!empty($_FILES["SANUpload"])) {
$myFile = $_FILES["SANUpload"];

if ($myFile["error"] !== UPLOAD_ERR_OK) {
    echo "<p>An error occurred.</p>";
    exit;
}

// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
    $i++;
    $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}

// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
    UPLOAD_DIR . $name);
if (!$success) { 
    echo "<p>Unable to save file.</p>";
    exit;
}



// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);


$val = shell_exec("tar -xvf $parts -C /var/www/html/uploads 2>&1");
echo '$val';
}

The goal is once it gets uploaded I need to sort the files and put them in the correct spots. The upload will ALWAYS be a TAR file because the scripts that i built for it the ONLY output is a tar file.

My goal is to extract the tar file in uploads directory and then run move commands to move the files where they need to be. Then do a clean up and delete any files in the UPLOADS dir.

I found this example on here :

$val = shell_exec("tar -xvf $parts -C /var/www/html/uploads 2>&1");
echo '$val';

This is another component i am looking at as well:

try {
$phar = new PharData('myphar.tar');
$phar->extractTo('/full/path'); // extract all files
$phar->extractTo('/another/path', 'file.txt'); // extract only file.txt
$phar->extractTo('/this/path',
    array('file1.txt', 'file2.txt')); // extract 2 files only
$phar->extractTo('/third/path', null, true); // extract all files, and overwrite
} catch (Exception $e) {
 // handle errors
}

The problem I am having with PharData is that it doesn't like the $path or $name variables. The same can be said for the example above PharData. Since the upload script could have different names I need to have the ability to unzip the file as a variable or to use a variable in the php script.

Technically - the script I have would keep the tar name the same however I could see a use case where someone might change that. So want to make sure that I have the option to address that.

So question is how do i extract a tar file - utilizing a variable name rather than have it spelled out and the actual name of the file all in PHP?


Solution

  • Figured out the issue - i needed to utilize the UPLOAD_DIR . $name in the pharData.

    try {
    $phar = new PharData(UPLOAD_DIR . $name);
    $phar->extractTo(UPLOAD_DIR); // extract all files
    } catch (Exception $e) {
     // handle errors
    }