简体   繁体   English

mysql在php中插入的结构

[英]structure of mysql insert in php

I have a script that I'm trying to use to upload zip files of photos. 我有一个脚本试图用于上传照片的zip文件。 The script should do these things, in this order: 脚本应按以下顺序执行这些操作:

  1. Take an input of 4 variables for location, date, subject, and name of directory to use 输入要使用的目录的位置,日期,主题和名称的4个变量
  2. Open a mysql connection 打开一个mysql连接
  3. Handle a zip file 1) Upload the zip file 2) Unzip the file 3) Save the photos in the file to the specified directory 4) Save the url of each photo, as well some other info about the photo, to a mysql database. 处理一个zip文件1)上传zip文件2)解压缩文件3)将照片保存在文件中到指定目录4)将每张照片的URL以及有关照片的其他信息保存到mysql数据库。
  4. Close the mysql connection 关闭mysql连接
  5. Confirm that everything functioned correctly. 确认所有功能均正常运行。
  6. Present the user the opportunity to upload another separate zip file with different variables. 向用户提供上载具有不同变量的另一个单独的zip文件的机会。

As of right now, I'm able to get most of this done correctly. 截至目前,我已经能够正确完成大部分任务。 However, something is off about the structure of my code, and I'm not sure how to modify it. 但是,我的代码结构有些问题,我不确定如何修改它。 When I upload the first set, it works fine. 当我上传第一组时,它工作正常。 However, if I try to upload a second set, a couple of things happen. 但是,如果我尝试上传第二组,则会发生一些事情。 The photos that have been uploaded so far end up getting entered into the mysql database again for each time that I try to upload another set. 每次我尝试上传另一组照片时,到目前为止已上传的照片最终都会再次输入mysql数据库。

Can anyone tell me how to correct this so that each individual image is inserted into the database once and only once? 谁能告诉我如何更正此问题,以便将每个单独的图像插入数据库一次且仅一次?

<?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>

scandir will get all the files in your target directory, both the new ones extracted from the current zip file, and any previous files uploaded with other extractions as well. scandir将获取目标目录中的所有文件,包括从当前zip文件中提取的新文件,以及通过其他提取文件上传的任何先前文件。 You should rather collect the names of the files in your zip specifically, and only insert values for them. 您应该专门在zip中收集文件的名称,并且只为它们插入值。

I believe the source of your problem is here (line 53 and 54 by my count): 我相信您的问题出处在这里(以我的观点,第53和54行):

$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

By using scandir , you retrieve not only the images you've just uploaded, but all previously uploaded images as well. 通过使用scandir ,您不仅可以检索刚刚上传的图像,还可以检索所有先前上传的图像。 Instead, you can read the filenames directly from the zip archive, in a manner similar to this: 相反,您可以通过类似于以下方式直接从zip存档中读取文件名:

$new_names = array();
for ($i=0; $i<$zip->numFiles; $i++) {
    $new_names[] = $zip->getNameIndex($i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM