0

私は現在、写真のアップロード機能を必要とする大学の最終学年のプロジェクトのウェブサイトを作成しています。現在、ユーザーが写真をアップロードすると、その写真はリモート サーバーのフォルダーに保存されます。画像をデータベースに入れる必要があるので、これを行う方法と、アップロードされたコンテンツを次のコード内のデータベースに送信するコードをどこに配置するかについて、誰かがアドバイスを持っているかどうか疑問に思っていました。個々のユーザーが画像をアップロードすると、すべての画像が表示され、一度に 1 つの画像しか表示されず、ページが更新されると画像が消える現在のようではありません。すべてが理にかなっていることを願っています。どんな助けでも大歓迎です。ありがとうございました。

<?php include_once("home_start.php"); ?>
<h1>Upload your images here:</h1>
<div id="fileselect" style="border-bottom:thin #000000 solid; border-   collapse:collapse">
    <form id="frmSimple" action="home.php" method="post" enctype="multipart/form-data">
         Select file to upload:&nbsp;
    <input type="file" id="filename" name="filename" size="10" /><br />
    <input type="submit" id="submit" name="submit" value=" Upload " />                      
    </form>
</div>
<div id="feedback">
        <?php
          // Determine whether a file was uploaded
                 if ($_FILES) {            
                // Put file properties into variables
                $name = $_FILES['filename']['name'];
                $size = $_FILES['filename']['size'];
                $tmp_name = $_FILES['filename']['tmp_name'];                
            // Determine whether file is png, jpg or other
        switch($_FILES['filename']['type']) {
        case 'image/jpeg':  $ext = "jpg";  break;
                case 'image/png':  $ext = "png";  break;
                //default:  ext = '';  break;
            }
            //validate against file type
            //     if $ext is empty string (therefore null or false) image is not a jpg     or png
    if($ext){
                // validate against file size
                if($size < 1000000){
                     // Create a safe name for the file and store in a safe location
                     $n = "$name";  // Could add .$ext to enforce file type
                     $n = ereg_replace("[^A-Za-z0-9.]","",$n);  //  Remove all except   alphanumeric characters and

                     $n = strtolower($n); // Convert to lower case (platform  independence)
                        $n = "uploaded_images/$n"; // Add folder to force safe location
                        move_uploaded_file($tmp_name, $n); // Move to the safe location and give it the safe 

                    echo "<p>Uploaded image '$name' as '$n': </p>";
                     echo "<img src='$n' />";
                }
            else echo "<p>'$name' is too big - 50KB max (50000 bytes).</p>";
            }
            else echo "<p>'$name' is an invalid file - only jpg and png accepted.</p>";
        }
            else echo "<p>No image has been uploaded.</p>";

?>
</div>
<?php include_once("home_end.php"); ?>
4

2 に答える 2

0

ファイルが正常にアップロードされた後、アップロードされた画像のパスを取得します。

    $file_type='jpg';  //if you are using more than one type write a switch case
$file_size = filesize($file);
$fp = fopen($file,'r');
$content = fread($fp,$file_size);
$content = addslashes($content);   //content is a binary data of the image
fclose($fp);

画像をデータベースに保存します。$file_name、$file_type、$file_size、およびコンテンツが必要なフィールドを使用して、挿入クエリを作成します。データベースに正常に接続できると仮定しています。

mysql_query("INSERT INTO Images (id,file_name,file_type,file_size,content)
   VALUES ('bird', 'jpg',340kb,'binarydata')");
于 2013-02-07T18:32:05.927 に答える
0

私はそれに対して強くお勧めします。代わりに、写真をフォルダーに保存し、データベースからその場所を参照します (つまり、ファイル システム上の場所を指す文字列)。

ただし、データベースに保存したい場合は、次のことを行う必要があります。

  • アップロード後にファイルの内容を取得する
  • コンテンツに SQL と競合する文字が含まれていないことを確認します (簡単な解決策は、何らかの方法でエンコードすることです。おそらく base64 でしょうか?)
  • SQL挿入を実行します

繰り返しますが、これは悪い考えです。やらないでください - ファイルシステムに保存してください。

また、次の行:

move_uploaded_file($tmp_name, $n);

ファイルの種類やファイルの整合性をチェックしないと、シェルをボックスにアップロードするのが簡単になります。

于 2013-02-07T18:22:40.453 に答える