2011-08-01 41 views
0

私は現在、複数のファイルをアップロードできるPHPアップロードフォームを用意しています。これは、ファイルをアップロードするバックエンドスクリプトです。複数のアップロードされたファイルをデータベースに保存するベストウェイ

while(list($key,$value) = each($_FILES[images][name])) 
{ 
if(!empty($value)){ // this will check if any blank field is entered 
$filename = $value; // filename stores the value 


$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line 


$file_ext = @strtolower(@strrchr($filename,".")); 
$file_ext = @substr($file_ext, 1); // remove dot 
$filename = md5(uniqid(rand(), true)) . '.' . $file_ext; 


$add = "/html/uploaded/$filename"; // upload directory path is set 
copy($_FILES[images][tmp_name][$key], $add);  // upload the file to the server 
chmod("$add",0777); 

} 
} 

このループの後、私はデータベースに情報を挿入するSQLクエリを実行します。最初の最初のファイルが$ file1、二番目のファイルが$ file2などとして保存されていることを確認するにはどうすればいいですか?

どの番号のファイルがどれであるかを調べるためにループを数えようとしました。

答えて

2

データベースにファイルを格納することは、一般的に悪い考えです。ファイルをサーバーのローカルディスクに置き、代わりにファイル名をデータベースに格納すると、より効果的です。

あなたのコードを見ると、あなたは実際にそのようにしようとしているように見えるので、あなたの質問のタイトルはちょっとした誤解です。

コードに構造的な問題がいくつかあります。このコードを試してみてください:

$errors = array(); 
$files = array(); 
foreach ($_FILES['images'] as $k=>$image) { 

    // handle upload errors 
    if ($image['error'] != 0 && $image['error'] != 4) {   
     switch ($image['error']) { 
      case '1': 
      case '2': 
       $err = 'The uploaded file exceeds the maximum file size.'; 
       break;     
      case '3': 
       $err = 'The upload was inturupted, transfer failed.'; 
       break; 
      case '6': 
      case '7': 
      case '8': 
       $err = 'Server error. Please try again later.'; 
       break; 
     } 
     // record error and move on 
     $errors[] = array('image'=>$k, 'error'=>$err); 
     continue; 
    } elseif ($image['error'] == 4) { 
     // error 4 means no image was sent 
     continue; 
    } 

    // determine the extension 
    $ext = explode('.', $image['name']); 
    if (count($ext) != 2) { 
     $errors[] = array('image'=>$k, 'error'=>'Could not determine file extension.'); 
     continue; 
    } else { 
     switch ($ext[1]) { 
      case 'jpg': 
      case 'jpeg': 
      case 'gif': 
      case 'png': 
       break; 
      default: 
       $errors[] = array('image'=>$k, 'error'=>'Unsupported file extension.'); 
       continue; 
       break; 
     } 
    } 

    // make a random-ish filename 
    $filename = time().uniqid(rand(), true) . '.' . $ext[1]; 
    $path = '/html/uploaded/'.$filename; // upload directory path is set 

    move_uploaded_file($image['tmp_name'], $path);  // upload the file to the server 
    // this is a bad idea right here! Use 775 at least, if possible 
    chmod($path,0777); 
    $files[] = array('name'=>$filename, 'path'=>$path); 
} 

// now loop the $files array and put the paths into the database 

// you also should do something with the errors listed in $errors 

EDIT

だからここデータベースにこれらのファイルを置くの間に合わせと-例です。 sizeextフィールドがあります。画像のファイルと拡張子を記録することになっている場合は、どちらですか?とにかく、他の変数を記入すると、以下のコードはファイル名をdbに入れます。

私はあなたにお勧めしたいと思います。このデータベースデザインには欠陥があります。 5枚以上の画像が必要な場合はどうすればよいですか? dbテーブルの構造を変更してコードを編集する必要があります。リレーショナルテーブル構造を作成し、各ファイルにIDを付けて1つの行を挿入すると、(つまりalbum_idの場合は、最上位の情報を持つalbumsというテーブルが作成されます)

// start building up the SQL query, start with 
// some fields that are straightforward 
$sql = ' 
    INSERT INTO table_name (
     `id`, 
     `status`, 
     `user`, 
     `title`,'; 

// now loop the list of files (5 only), 
// add each needed field 
for ($i=1; $i < count($files) && $i < 5; $i++) { 
    $sql .= '`file'.$i.'`,'; 
} 

// build out the rest of the query, add values 
// for the straightforward fields 
$sql .= ' 
    `size`, 
    `ext`, 
    `ip`, 
    `date` 
) VALUES (
    NULL, 
    "'.$status.'", 
    "'.$user.'", 
    "'.$title.'", 
'; 

// loop the files 
$ct = 1; 
foreach ($files as $f) { 
    $sql .= '"'.$f['name'].'",'; 
    // only allow 5 files 
    if ($ct == 5) 
     break; 
    $ct++; 
} 

// wrap up building the query 
$sql .= ' 
    "'.$size.'", 
    "'.$ext.'", 
    "'.$ip.'", 
    "'.$date.'" 
)'; 

mysql_query($sql); 
+0

おかげでクリスは、私は確かに私のサーバー上のファイルを保存し、ちょうど参照としてデータベースにファイル名を保管しております。 すべてが機能していましたが、2つのファイルをアップロードした場合は、最初のファイルをテーブルのfile1フィールドに保存し、2番目のファイルをfile2などに保存します。 – Jako

+0

これについて話すには、あなたのデータベース構造を見てください。しかし、単に '$ files'配列を私のサンプルコードからループすれば、それらはアップロード順になります。 –

+0

Chrisは、データベース構造がどのようになっているかを以下に示します。http://jako.in/db.jpg – Jako

1

まず、forループで$ _FILESのような連想配列にアクセスするときは、アポストロフィを使用する必要があります。

$filename = sprintf('%s%d.%s', md5(uniqid(rand(), true)), $key, $file_ext);のようなものを$filename = md5(uniqid(rand(), true)) . '.' . $file_ext;の代わりに使用しないでください。

関連する問題