I have a site I am creating when a user signs up I unzip a file that holds the current folder structure for the users profile.
The folder structure is housed in the users directory as a zip /u/folders.zip
what I thought I was doing is creating the following structure:
-u
--folders.zip
--newamazingusername
---index.php
---folder1
---folder2
----subfolder1
----subfolder2
---folder3
What it is creating instead is this:
-u
--folders.zip
--newamazingusername
---folders
----index.php
----folder1
----folder2
-----subfolder1
-----subfolder2
----folder3
Processing file:
<?php
require_once( "../../assets/inc/connection.php" );
if ( !empty($_GET['email']) && !empty($_GET['username']) ):
$get_username = $_GET['username'];
$get_email = $_GET['email'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$hash = **[REDACTED CODE] Assume I am Hashing my password properly**
$sql = "UPDATE users SET username='" . $get_username . "' WHERE email='" . $get_email . "'";
// use exec() because no results are returned
$conn->exec($sql);
$zip = new ZipArchive;
$fileLocation = $_SERVER['DOCUMENT_ROOT'] . '/u/folders.zip';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username . '.zip';
if (!copy($fileLocation, $newfile)) {
echo "failed to copy";
header('Location: http://' . $_SERVER['HTTP_HOST'] . '?error=foldercopyfail');
exit();
}
$res = $zip->open($newfile);
if ($res === TRUE) {
$zip->extractTo($_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username);
$zip->close();
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/u/' . $get_username );
} else {
echo 'doh!';
}
unlink($newfile);
exit();
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
else :
header('Location: http://' . $_SERVER['HTTP_HOST'] . '?error=noaccess');
endif;
?>
I need to unzip the folders in the zip at the root of the user folder. I am pretty sure its an issue with the copy() if snippet.
Thank you for the help!
Try change this :
$res = $zip->open($newfile);
if ($res === TRUE) {
$zip->extractTo($_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username);
$zip->close();
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/u/' . $get_username );
} else {
echo 'doh!';
}
with this :
$res = $zip->open($newfile);
if ($res === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
copy("zip://".$newfile."#".$filename, $_SERVER['DOCUMENT_ROOT'] . '/u/' . $get_username . $fileinfo['basename']);
}
$zip->close();
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/u/' . $get_username );
} else {
echo 'doh!';
}
Please read the user contributed note on the php docs page of extractTo()
for the better explanation.