|
 |
|
upload pic จากกระทู้ เวบไทครีเอท PHP Upload and Resize Image |
|
 |
|
|
 |
 |
|
Code (PHP)
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input type="file" name="file_1" />
<br />
<input type="file" name="file_2"/>
<br />
<input type="submit" name="btnUpload" value="upload" />
</form>
<br />
<?
if($_POST[btnUpload]){
$path="file/"; // folder ที่อยู่ของไฟล์
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////// check file_1 //////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
if($_FILES[file_1][type]=="application/octet-stream"){ // check ว่าใช้นามสกุล .3gp
$newname_1=$_FILES[file_1][name];
}else if($_FILES[file_1][type]=="image/gif"){ // check ว่าใช้นามสกุล .gif
$newname_1=$_FILES[file_1][name];
}else if($_FILES[file_1][type]=="image/jpeg"){ // check ว่าใช้นามสกุล .่jpf
$newname_1=$_FILES[file_1][name];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////// check file_2 //////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
if($_FILES[file_2][type]=="application/octet-stream"){ // check ว่าใช้นามสกุล .3gp
$newname_2=$_FILES[file_2][name];
}else if($_FILES[file_2][type]=="image/gif"){ // check ว่าใช้นามสกุล .gif
$newname_2=$_FILES[file_2][name];
}else if($_FILES[file_2][type]=="image/jpeg"){ // check ว่าใช้นามสกุล .่jpf
$newname_2=$_FILES[file_2][name];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// Resize image file_1 //////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
if($newname_1!=""){
$file_1 = $_FILES["file_1"]["tmp_name"];
$new_images_1 = $newname_1;
//move_uploaded_file($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
//copy($_FILES['file_1']['tmp_name'],$path.$newname_1);
$width=280; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($file_1);
$height=300;
//$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($file_1);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"file/".$new_images_1);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// Resize image file_2 //////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
if($newname_2!=""){
$file_2 = $_FILES["file_2"]["tmp_name"];
$new_images_2 = $newname_2;
//move_uploaded_file($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
//copy($_FILES['file_1']['tmp_name'],$path.$newname_1);
$width=280; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($file_2);
$height=300;
//$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($file_2);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"file/".$new_images_2);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//if($newname_1!="" and $newname_2!=""){ // check file_1,file_2 มีค่า ให้ทำการ copy ไฟล์ไปเก็บไว้ใน folder ที่กำหนด
//copy($_FILES['file_1']['tmp_name'],$path.$newname_1);
//copy($_FILES['file_2']['tmp_name'],$path.$newname_2);
//}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////// ส่วนข้างล่างนี้เป็นการ check ว่าไฟล์มีค่าว่างหรือไม่ (ถ้าประเภทไฟล์ไม่ตรงตามเงื่อนไข ไฟล์จะเป็นค่าว่าง) ////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
if($newname_1 =="" and $newname_2 == ""){ // check file_1,file_2 เท่ากับค่าว่าง
echo "ประเภทของไฟล์ที่ 1,2 ไม่ถูกต้อง จะเป็นเป็น .pdf .gif .jpg เท่านั้น";
}else if($newname_1==""){ // check file_1 เท่ากับค่าว่าง
echo "ประเภทของไฟล์ที่ 1 ไม่ถูกต้อง จะเป็นเป็น .pdf .gif .jpg เท่านั้น";
}else if($newname_2==""){ // check file_2 เท่ากับค่าว่าง
echo "ประเภทของไฟล์ที่ 2 ไม่ถูกต้อง จะเป็นเป็น .pdf .gif .jpg เท่านั้น";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
if($newname_1 !="" and $newname_2 != ""){ // check file_1,file_2 ไม่เท่ากับค่าว่างให้ทำการ upload ไฟล์เข้าสู่ Database
if($_POST[btnUpload]){
include ("config.inc.php");
$sql = "INSERT INTO upload (file_id,file_name_1,file_name_2) VALUES ('','$newname_1','$newname_2')";
$result = mysql_query($sql);
echo "ไ้ด้ทำการอัพโหลดไฟล์เรียบร้อยแล้ว";
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// เครดิต By`SystemAnalysis //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
|
 |
 |
 |
 |
Date :
2011-07-19 15:31:16 |
By :
systemanalysis |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2011-07-19 18:21:17 |
By :
yag00za |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ Resize และ Upload ลงใน MySQL ครับ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<?
if(trim($_FILES["fileUpload"]["tmp_name"]) != "")
{
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_".$_FILES["fileUpload"]["name"];
move_uploaded_file($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
$width=100; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"MyResize/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Resize Successful.<br>";
//*** Insert Record ***//
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "INSERT INTO files ";
$strSQL .="(Thumbnails,FilesName) VALUES ('".$new_images."','".$_FILES["fileUpload"]["name"]."')";
$objQuery = mysql_query($strSQL);
}
?>
<a href="phpUploadResizeToMySQL3.php">View files</a>
</body>
</html>
Go to : PHP Resize Thumbnails Image and Insert to MySQL
|
 |
 |
 |
 |
Date :
2011-07-19 20:11:47 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณเฮียวินคับ และSystemAnalysis
|
 |
 |
 |
 |
Date :
2011-07-19 21:09:37 |
By :
yag00za |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error แบบนั้นเลยหรอครับ 
|
 |
 |
 |
 |
Date :
2011-07-20 00:44:06 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช่ครับ
|
 |
 |
 |
 |
Date :
2011-07-20 01:11:59 |
By :
yag00za |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมถึง error เป็น php tag มาครับ
|
 |
 |
 |
 |
Date :
2011-07-20 06:16:26 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 8 เขียนโดย : webmaster เมื่อวันที่ 2011-07-20 06:16:26
รายละเอียดของการตอบ ::
phpUploadResizeToMySQL1
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<form name="form1" method="post" action="phpUploadResizeToMySQL2.php" enctype="multipart/form-data">
<input type="file" name="fileUpload"><br>
<input name="btnSubmit" type="submit" value="Submit">
</form>
</body>
</html>
phpUploadResizeToMySQL2
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<?
if(trim($_FILES["fileUpload"]["tmp_name"]) != "")
{
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_".$_FILES["fileUpload"]["name"];
copy($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
$width=100; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($images);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,"MyResize/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Resize Successful.<br>";
//*** Insert Record ***//
$objConnect = mysql_connect("localhost","root","321654") or die("Error Connect to Database");
$objDB = mysql_select_db("files");
$strSQL = "INSERT INTO files ";
$strSQL .="(Thumbnails,FilesName) VALUES ('".$new_images."','".$_FILES["fileUpload"]["name"]."')";
$objQuery = mysql_query($strSQL);
}
?>
<a href="phpUploadResizeToMySQL3.php">View files</a>
</body>
</html>
phpUploadResizeToMySQL3
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP Upload Resize to MySQL</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","321654") or die("Error Connect to Database");
$objDB = mysql_select_db("files");
$strSQL = "SELECT * FROM files";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="200" border="1">
<tr>
<th width="50"> <div align="center">Files ID </div></th>
<th width="150"> <div align="center">Thumbnails </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["FilesID"];?></div></td>
<td><center><a href="MyResize/<?=$objResult["FilesName"];?>">
<img src="MyResize/<?=$objResult["Thumbnails"];?>" border="0"></a></center></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<br>
<a href="phpUploadResizeToMySQL1.html">Upload Images</a>
</body>
</html>
|
 |
 |
 |
 |
Date :
2011-07-20 13:44:14 |
By :
yag00za |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
php.ini -> short_open_tag = Off เปลี่ยนเป็น On ซะ
|
 |
 |
 |
 |
Date :
2011-07-20 13:48:33 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปรกติ โฮสที่พี่ใช้ เปน off /on คับ ของผมเปน off แบบนี้สคิปตัวนี้ก็เอาไปใช้จิงไม่ได้ใช่ไหมคับ ใช้ได้แต่ในเครื่องตัวเอง
มีวิธีแก้ไหมคับ เพราะเคยไปบอกทางโฮสให้แก้ เปน ออน แต่เค้าบอกให้ไปปรับแต่โค๊ดเอา
|
 |
 |
 |
 |
Date :
2011-07-20 17:00:41 |
By :
yag00za |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าไม่อยากมีปัญหา ใช้ <?php เสมอครับ อย่าขี้เกียจพิมพ์
|
 |
 |
 |
 |
Date :
2011-07-21 00:38:25 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมถึงรันแบบนั้นครับ ไม่ใช่ http://localhost เหรอครับ 
|
 |
 |
 |
 |
Date :
2011-07-21 06:19:54 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ mr.win อีกทีคับ ตัวอย่างนี้ชัดเจน
|
 |
 |
 |
 |
Date :
2011-08-06 01:48:10 |
By :
yag00za |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|