ファイルをPHPスクリプトに送信し、ファイルの名前をZipに変更し、フォルダー(ランダム名)に保存してから、そのファイルを抽出するフォーム(HTML)があります。
ファイルがアップロードされます。フォルダが正しく作成されます。ファイルの名前が正しく変更されます。
Zipの抽出は失敗します。
これが私のフォームです:
<form action="up.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
Select file
<input name="ufile" type="file" id="ufile" size="50" />
<input type="submit" name="Submit" value="Upload" />
</form>
これがPHPスクリプト-up.php
$file_name = $HTTP_POST_FILES['ufile']['name'];
$random_digit=Rand(0000,9999);
$new_file_name=$random_digit.".Zip";
mkdir($random_digit, 0777, true);
$path= $random_digit.'/'.$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "The upload is successful<BR/>";
echo "File Renamed to: ".$new_file_name." for processing.<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "<strong><a style='color:#6A8DBC; text-decoration:none' href='".$link_address."'>Proceed to the next phase of the importation of data to the system</a></strong></br>";
}
else
{
echo "Error";
}
}
$Zip = new ZipArchive;
$res = $Zip->open($new_file_name);
if ($res === TRUE) {
$Zip->extractTo($random_digit.'/');
$Zip->close();
echo 'extraction successful';
} else {
echo 'extraction error';
}
抽出を妨げるのはフォルダのモードですか?私が見る限り、構文エラーはありません。
試してみてください
$res = $Zip->open($path)
ファイルを$path
に移動したら、$path
でファイルを操作する必要があります。
<?php
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}
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 = dirname(__FILE__).'/'; // 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.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unzip a Zip file to the webserver</title>
</head>
<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a Zip file to upload: <input type="file" name="Zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>