データベースにファイルを格納することは、一般的に悪い考えです。ファイルをサーバーのローカルディスクに置き、代わりにファイル名をデータベースに格納すると、より効果的です。
あなたのコードを見ると、あなたは実際にそのようにしようとしているように見えるので、あなたの質問のタイトルはちょっとした誤解です。
コードに構造的な問題がいくつかあります。このコードを試してみてください:
$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
だからここデータベースにこれらのファイルを置くの間に合わせと-例です。 size
とext
フィールドがあります。画像のファイルと拡張子を記録することになっている場合は、どちらですか?とにかく、他の変数を記入すると、以下のコードはファイル名を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);
おかげでクリスは、私は確かに私のサーバー上のファイルを保存し、ちょうど参照としてデータベースにファイル名を保管しております。 すべてが機能していましたが、2つのファイルをアップロードした場合は、最初のファイルをテーブルのfile1フィールドに保存し、2番目のファイルをfile2などに保存します。 – Jako
これについて話すには、あなたのデータベース構造を見てください。しかし、単に '$ files'配列を私のサンプルコードからループすれば、それらはアップロード順になります。 –
Chrisは、データベース構造がどのようになっているかを以下に示します。http://jako.in/db.jpg – Jako