Search code examples
phpfile-uploadzipchmodmkdir

how can I extract zip files, which was uploaded by user?


So, I want to create a system where user uploads in a zip file the files of a 3d model and the model can be shown, stored, etc

So, I got the file, I place it into a folder, permanently, And unzip it into another temp folder, just to see if it is a 3d model.

I tried like this:

                  $target_dir = "upload/";
                  $targetfilename = rand().$_FILES['file']['name'];
                  move_uploaded_file($_FILES['file']['tmp_name'], $target_dir.$targetfilename);

                  //unzip the file into temp folder
                  $tmp_dir = $target_dir.rand();
                  mkdir($tmp_dir);  
                  chmod($tmp_dir, 0777);
                  //chmod($targetfilename, 0777); //this not working, maybe isn't the right way

                  $zip = new ZipArchive;
                  $res = $zip->open($targetfilename);
                  if ($res === TRUE) {
                    // extract it to the path we determined above
                    $zip->extractTo($tmp_dir);
                    $zip->close();
                    echo 'SUCCESS';
                  } else {
                    echo 'ERROR';
                  }

I do not get any errors, but the zip can't be unzipped. Any idea? How can I resolve this?


Solution

  • Isn't $targetfilename is in $target_dir folder?

    If so, changing $res = $zip->open($targetfilename); to

    $res = $zip->open($target_dir.$targetfilename); might solve your problem.