0

写真のzipファイルをアップロードするために使用しようとしているスクリプトがあります。スクリプトは、次のことを次の順序で実行する必要があります。

  1. 場所、日付、件名、使用するディレクトリ名の 4 つの変数を入力します
  2. mysql 接続を開く
  3. zip ファイルを処理する 1) zip ファイルをアップロードする 2) ファイルを解凍する 3) ファイル内の写真を指定されたディレクトリに保存する 4) 各写真の URL と写真に関するその他の情報を mysql データベースに保存します。
  4. mysql 接続を閉じる
  5. すべてが正しく機能していることを確認します。
  6. 異なる変数を持つ別の個別の zip ファイルをアップロードする機会をユーザーに提示します。

現時点では、これのほとんどを正しく行うことができます。しかし、私のコードの構造に何か問題があり、それを修正する方法がわかりません。最初のセットをアップロードすると、正常に動作します。ただし、2 番目のセットをアップロードしようとすると、いくつかのことが起こります。これまでにアップロードされた写真は、別のセットをアップロードしようとするたびに、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>
4

2 に答える 2

0

私はあなたの問題の原因がここにあると信じています(私のカウントでは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

を使用するscandirと、アップロードしたばかりの画像だけでなく、以前にアップロードしたすべての画像も取得できます。代わりに、次のように、zipアーカイブからファイル名を直接読み取ることができます。

$new_names = array();
for ($i=0; $i<$zip->numFiles; $i++) {
    $new_names[] = $zip->getNameIndex($i);
}
于 2012-12-14T08:27:13.913 に答える
0

scandirは、現在のzipファイルから抽出された新しいファイルと、他の抽出でアップロードされた以前のファイルの両方を含む、ターゲットディレクトリ内のすべてのファイルを取得します。むしろ、zip内のファイルの名前を具体的に収集し、それらの値のみを挿入する必要があります。

于 2012-12-14T08:24:59.300 に答える