php로 넘어오는 get, post, REQUEST 값들 전부 출력 소스
echo "<pre>"; print_r($_POST); echo "</pre>";
echo "<pre>"; print_r($_GET); echo "</pre>";
echo "<pre>"; print_r($_REQUEST); echo "</pre>";
전역변수로 $name = $_POST['name']; 이렇게 해도 함수 부분에서는 파라미터를 넘겨주지않는 이상 공백으로 처리해버림.
ex) function($files, $_POST['name']) 같이 넘겨줘야 넘어감..
업로드 페이지 up.php
<html>
<body>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8;" />
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file[]"/>
<br />
<input type="file" name="file[]"/>
<br />
<input type="text" name="te" />
<p>이름 : <input type="text" name="name" /></p>
<p>연령 : <input type="text" name="age" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
이미지 저장 페이지 upload_file.php
<?php
header("Content-Type: text/html; charset=utf-8");
$files=$_FILES["file"];
$post_name = $_POST['name'];
$n_name = $_REQUEST['name'];
images_check($files);
store_image($files, $post_name);
// 변수 체크 이름, 연령
function images_check($files) {
//카운트 수
$count=count($files["name"]);
//이미지 파일
for ($i = 0; $i < $count; $i++) {
//빈 파일 체크 if문 start
if($files["name"][$i]!="" || $files["name"][$i]!=null)
{
//파일 업로드시 에러
if ($files["error"][$i] > 0)
{
$msg = "Error: " . $files["error"][$i] . "<br />";
$url = "history.go(-1)";
echo "<script language='xxxxjavascript'>alert('$msg');$url;</script>";
exit;
}
//파일 이미지 체크
$image_check=false;
if (($files["type"][$i] == "image/gif")|| ($files["type"][$i] == "image/jpeg")|| ($files["type"][$i] == "image/pjpeg"))
{
$image_check=true;
}//if문
if ($image_check=false) {
$url = "history.go(-1)";
echo "<script language='xxxxjavascript'>alert('확장자 올바르지 않습니다.');$url;</script>";
exit;
}
//파일 사이즈 체크 5M 제한
if ($files["size"][$i] > 5242880)
{
$url = "history.go(-1)";
echo "<script language='xxxxjavascript'>alert('파일 용량 올바르지 않습니다.');$url;</script>";
exit;
}//if문
}//빈 파일 체크 if문 end
}//for문
}//이미지 확인 함수 끝
function store_image($files, $post_name) {
//@mysqli_query("SET NAMES utf8");
$mysql = mysql_connect("localhost", "id", "비번", "db명");
mysql_select_db("db명") or die("Unable to select Database : " . mysql_error());
mysql_query("set session character_set_connection=utf8;");
//db 저장시 character_set_connection=utf8 로 한글 저장
mysql_query("set session character_set_results=utf8;");
mysql_query("set session character_set_client=utf8;");
// $db = @new mysqli('localhost','root','gksrlf1.','meetingful');
//if(mysqli_connect_errno()) {
// exit("DB연결오류");
//}//if
$count=count($files["name"]);
for ($i = 0; $i < $count; $i++) {
$saveName = time(). $_FILES["file"]["name"][$i];
if (file_exists("upload/" . $saveName ))
{
echo $_FILES["file"]["name"][$i] . " already exists. ";
$url = "history.go(-1)";
echo "<script language='xxxxjavascript'>alert('같은이름의 파일이 존재합니다.');$url;</script>";
exit;
}
else
{
$sql = "insert into file_list (upload_filename, db_filename) values('$post_name', '$saveName')";
$res = mysql_query($sql);
/*
$query = sprintf("INSERT INTO file_list
(upload_filename, db_filename)
VALUES ('%s', '%s')",
$n_name, $saveName);
$db->query($query);
*/
move_uploaded_file($_FILES["file"]["tmp_name"][$i], "upload/". $saveName );
$url = "history.go(-1)";
}
}
// echo "<script language='xxxxjavascript'>alert('파일이 저장 되었습니다.');$url;</script>";
}//이미지 저장 함수 끝
?>