我不确定我认为如何正确访问javascript对象。我正在尝试使用ajax将文件上传到php,并让php文件处理到特定目录的解压缩。
我不确定解压缩的方式,但是到目前为止,我有以下代码:
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);
}
}
);
}
}
和PHP(我对PHP的检查不多,所以不要对此太在意):
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.";
}
}
我只是想
a)克服PHP错误“ C:\ etc中的未定义索引zip_file”
b)将.ZIP文件提取到位置“ temp /”
谢谢。
编辑我们知道PHP错误是未定义的索引,因为$ _FILES [“ zip_file”]中的文件类型未正确加载。由于某种原因,由于ajax未能获取文件。我认为这与formdata = new FormData(document.forms[0]);
行有关。