我有一个脚本,我试图用它来上传照片的 zip 文件。脚本应该按以下顺序执行这些操作:
- 输入位置、日期、主题和要使用的目录名称的 4 个变量
- 打开一个mysql连接
- 处理 zip 文件 1) 上传 zip 文件 2) 解压缩文件 3) 将文件中的照片保存到指定目录 4) 将每张照片的 url 以及有关照片的一些其他信息保存到 mysql 数据库。
- 关闭mysql连接
- 确认一切正常。
- 为用户提供上传另一个具有不同变量的单独 zip 文件的机会。
截至目前,我能够正确完成大部分工作。但是,我的代码结构有些问题,我不确定如何修改它。当我上传第一组时,它工作正常。但是,如果我尝试上传第二组,会发生一些事情。每次我尝试上传另一组照片时,到目前为止已上传的照片最终都会再次进入 mysql 数据库。
谁能告诉我如何纠正这个问题,以便将每个单独的图像插入数据库一次且仅一次?
<?php // actual code for upload
$dirname = $_REQUEST['dirname'];
$taken = $_REQUEST['taken'];
$location = $_REQUEST['location'];
$subject = $_REQUEST['subject'];
$urldirectory = $_REQUEST['urldirectory'];
if(!(file_exists($dirname) && is_dir($dirname))) { // confirm that the directory exists, and that it is a directory
mkdir($dirname, 0777);
echo "the directory will be called ".$dirname;
} else {
echo "directory: " . $dirname;
}
if($_FILES["zip_file"]["name"]) { // pull the nmae of the zip file from the upload
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename); //format the filename for a variable
$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; // let user know if the zip file has not been uploaded
if(!$continue) {
$message = "The file you are trying to upload is not a .zip file. Please try again.";
}
$target_path = $dirname."/".$name; // get the $target_path variable to for the move_uploaded_file() function.
if(move_uploaded_file($source, $target_path)) { // this block extracts the zip files and moves them to the $dirname directory
$zip = new ZipArchive();
$x = $zip->open($target_path);
if ($x === true) {
$zip->extractTo($dirname."/");
$zip->close();
unlink($target_path);
}
$message = "Your .zip file was uploaded and unpacked.";
//use glob to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
require_once 'connect.php';
echo '<html>';
echo '<html>';
$images = array(); // this clears the array by initializing between each reload of the page. Without this, each separate folder being uploaded would accumulate in the array and be uploaded multiple times.
$images = scandir($dirname); //use scandir to find all the files that have been unzipped into the directory, and then do a foreach loop that enters the image file locations into your mysql database
foreach ($images as $value) {
if ($value!='.' && $value!='..' && $subjecttest=$subject) {
/* $url = trim($urldirectory)."/".trim($value);*/
echo '<img src="http://www.example.com/temp/' . $dirname . '/' . $value . '"< /img>';
$url = trim('http://www.example.com/temp/') . trim($dirname) . '/' . trim($value);
$insert_sql = "INSERT INTO pics (taken, location, subject, url) VALUES ('$taken', '$location', '$subject' , '$url');";
if (mysql_query($insert_sql)) {
echo "$value"." inserted successfully!";
} else {
echo "$value"." not inserted";
echo $insert_sql . '<BR>' . mysql_error();
}
} else {
echo 'Please use unique info for each upload set'
}
unset($images); // destroys the $images variable, so it doesn't accumulate the next time you upload another folder.
}
}
echo '</html>';
} 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...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if($message) echo "<p>$message</p>";
if($taken) echo "<p>pictures taken on: " . $taken . "</p>";
if($subject) echo "<p>subject: " . $subject . "</p>";
if($location) echo "<p>location: " . $location . "</p>";
?>
<form enctype="multipart/form-data" method="post" action="upload2.php">
<label for="dirname">Directory to use?: </label> <input name="dirname" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="taken">When was this taken?:</label> <input name="taken" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="location">Where was this taken?</label> <input name="location" size="20" type="text" /><br />
<label for="subject">subject?</label> <input name="subject" size="20" type="text" /><br />
<input type=hidden name="urldirectory" value="<?php echo "http://www.example.com/temp/".'$dirname;' ?>" />
<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>