사용자가 여러 파일을 디렉토리에 업로드 할 수있는 양식을 만들었습니다. 이제 폼 입력 필드에서 파일의 이름을 가져 와서 데이터베이스에 저장하려고합니다.배열에서 데이터베이스에 양식 값 삽입
입력 필드의 배열 값을 가져 오려고합니다. 즉; [ 'file'] [ 'name'] 및 [ 'file'] [ 'tmp_name']을 입력하여 데이터베이스에 삽입하십시오. 아래 코드를 사용하여 파일 "array"라는 단어를 파일의 실제 이름이 아닌 데이터베이스에 삽입 할 수있었습니다.
foreach 루프를 사용하여 배열을 반복해야한다고 생각하지만 작성 방법과 코드에 삽입하는 방법은 확실하지 않습니다. 지금까지 코드는 다음과 같습니다.
#connect to the database
mysql_connect("localhost", "root", "");
mysql_select_db("masscic");
//Upload Handler to check image types
function is_image($file) {
$file_types = array('jpeg', 'gif', 'bmp'); //acceptable file types
if ($img = getimagesize($file)){
//echo '<pre>';
//print_r($_FILES); //will return an array of information for testing
//print_r($img); //will return an array of information for testing
if(in_array(str_replace('image/', '', $img['mime']), $file_types))
return $img;
}
return false;
}
//form submission handling
if(isset($_POST['submit'])) {
//file variables
$fname = $_FILES['files']['name'];
$ftype = $_FILES['files']['type'];
$fsize = $_FILES['files']['size'];
$tname = $_FILES['files']['tmp_name'];
$ferror = $_FILES['files']['error'];
$newDir = '../uploads/'; //relative to where this script file resides
for ($i = count($fname) -1; $i >=0; $i--) {
if ($ferror[$i] =='UPLOAD ERR OK' || $ferror[$i] ==0)
{
if(is_image($tname[$i]))
{
move_uploaded_file($tname[$i], ($newDir.time().$fname[$i]));
echo '<li><span class="success">'.$fname[$i].' -- image has been accepted<br></span></li>';
}else
echo '<li><span class="error">'.$fname[$i].' -- is not an accepted file type<br></span></li>';
}
}
}
$sqlInsert = mysql_query("INSERT INTO files (file_names) VALUES('$fname')") or die (mysql_error());
도움 주셔서 감사합니다.
안녕에 설명되어 있습니다. 이 페이지를 광범위하게 읽었지만 이해할 수는 없습니다. 이것이 내가 "foreach"루프를 생각한 이유입니다. 위의 루프를 for 루프로 대체해야한다고 말하고 있습니까? 그렇다면 for 루프는 구문 분석 오류를 발생시킵니다. –
구문 분석 오류가 수정되어 데이터베이스에 삽입되지만 세 개의 개별 레코드가 만들어집니다. 즉, 사용자가 3 개의 파일을 업로드하면 세 개의 개별 레코드가 데이터베이스에 작성됩니다. 데이터베이스의 "파일"이라는 이름의 하나의 데이터베이스 열에 세 개의 파일을 하나의 레코드로 집계하고 싶습니다. –