0

I'm not sure how to access the javascript object correctly I think. I'm trying to use ajax to upload a file to php and have the php file handle an unzip to specific directories.

I'm not sure how the unzip would go but I have have the following code so far:

HTML:

<form id="upload_header_modules_form1" enctype="multipart/form-data" method="post" action="" onsubmit="return false">
                <label for="themes_edit_file_id1" class="themes_edit_file">Upload .zip file</label>
                <input id="themes_edit_file_id1" style="visibility:hidden;"  onchange="setfilename1(this.value);" type="file">
                <label for="install1" class="normal_button" onclick="uploadZip()">Install</label>
                <input id="install1" style="visibility:hidden;" type="submit">
            </form>

AJAX:

function uploadZip() 
{
    formdata = new FormData(document.forms[0]);
    if (formdata) 
    {
        $.ajax
        (
            {
                url: "http://localhost/IG_API_2/pages/default_upload/upload_php.php",
                type: "POST",
                data: formdata,
                processData: false,
                contentType: false,
                success: function (data)
                {
                    $(".modal").css("opacity", "1");$(".modal").css("visibility", "visible");$(".modal_mid").html("<pre>" + data + "</pre>");
                    alert(data);                    
                }
            }
        );
    }
}

and PHP (I haven't checked the PHP much so don't pay too much attention to this):

if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];

    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        } 
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }

  /* PHP current path */
  $path = '../plugins/';  // absolute path to the directory where zipper.php is in
  $filenoext = basename ($filename, '.zip');  // absolute path to the directory where zipper.php is in (lowercase)
  $filenoext = basename ($filenoext, '.ZIP');  // absolute path to the directory where zipper.php is in (when uppercase)

  $targetdir = $path . $filenoext; // target directory
  $targetzip = $path . $filename; // target zip file

  /* create directory if not exists', otherwise overwrite */
  /* target directory is same as filename without extension */

  if (is_dir($targetdir))  rmdir_recursive ( $targetdir);


  mkdir($targetdir, 0777);


  /* here it is really happening */

    if(move_uploaded_file($source, $targetzip)) {
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);  // open the zip file to extract
        if ($x === true) {
            $zip->extractTo($targetdir); // place in the directory with same name  
            $zip->close();

            unlink($targetzip);
        }
        $message = "Your .zip file was uploaded and unpacked.";
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}

I'm just wanting to

a) get past the PHP error "Undefined Index zip_file in C:\etc"

b) extract the .ZIP file to location "temp/"

Thanks in advance.

Edit We know that the PHP error is an undefined index because the file type is not loading correctly in the $_FILES["zip_file"]. It's not getting the file because of ajax for some reason. I think it's to do with the formdata = new FormData(document.forms[0]); line.

ax.falcon
  • 43
  • 1
  • 10
  • 1
    use console.log() instead of alert(), then check the web dev toolbar console, you'll see so much more than a plain alert will give you – delboy1978uk Oct 11 '18 at 14:09
  • Also, check the response from the request. – Script47 Oct 11 '18 at 14:14
  • console.log's not showing anything. – ax.falcon Oct 11 '18 at 14:14
  • request header is showing `Accept-Encoding: gzip, deflate, br` and `Content-Type: text/html; charset=UTF-8` if that means anything? – ax.falcon Oct 11 '18 at 14:16
  • 1
    @ax.falcon With console.log() you need to open the developer console using F12 in most browsers. – Absorbing Oct 11 '18 at 14:46
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Oct 11 '18 at 14:50
  • Thanks. I know what undefined index is though, I just don't know how to get ajax to upload the actual file, and/or know how to make php get the zip file. – ax.falcon Oct 11 '18 at 15:03
  • Why is your contenttype false, it should be like form\multipart or whatever something like that. ContentType is very important for your server and how it will interpret your request. This is probably your issue. Also **I haven't checked the PHP much so don't pay too much attention to this** while it cotains the error you're getting is not really making me thinking of helping you. How can you say that while posting this question on here...? – Glubus Oct 11 '18 at 15:50
  • It's simply a script, and I just noted that viewers should not pick it apart like the ajax or html. The PHP isn't really the issue, it's the ajax. Thanks for your suggestion though, I had considered that. In searching google I found that contenttype had to be false in order for files to be "parsed" into php. – ax.falcon Oct 11 '18 at 16:07

0 Answers0