|
|
|
ขอถามเกี่ยวกับ การตั้งชื่อไฟล์คับ อยากให้ช่วยอธิบายหรือช่วยแนะนำโค้ดคับ เพราะไม่มีความรู้เรื่องนี้เลย |
|
|
|
|
|
|
|
คือได้โค็ดมาอัพโหลดรูปมา แต่ระบบเป็นการเปลี่ยนชื่อที่อัพเข้าไปแบบสุ่ม อยากจะให้ต่อท้ายด้วยชื่อของมันเองก่อนอัพโหลดเข้าไปคับ
เช่น http://localhost/hot/i/ud.jpg แต่อยากให้มันเป็น http://localhost/hot/i/ud/1234567.jpg
(1234567.jpg เป็นชื่อไฟล์ที่อัพเข้าไป) พอดีไม่มีความรู้เรื่องโค็ดเลยแต่งมเอา อยากให้ช่วยแนะนำโค็ดให้หน่อยคับ
Code (PHP)
<?
if (empty($_FILES))
exit('');
$tempfile = $_FILES['Filedata']['tmp_name'];
$targetpath = $base['dir'] . '/i/';
$fullname = addslashes($_FILES['Filedata']['name']);
$md5 = md5_file($tempfile);
$filesize = filesize($tempfile);
if($filesize >= $base['config']['maxfilesize'])
exit("alert('" . $base['lang']['error_file_too_big'] . "');");
$extension = strtolower(strrchr($_FILES['Filedata']['name'],'.'));
$exarray = array('.png','.jpeg','.jpg','.gif');
if(!in_array($extension, $exarray))
exit("alert('" . $base['lang']['error_file_type'] . "');");
$size = getimagesize($tempfile);
$sizew = $size[0];
$sizeh = $size[1];
$image = $db->first("SELECT * FROM {$base['config']['db']['pre']}images WHERE md5='{$md5}' AND size='{$filesize}'");
if($image){
$img = img($image['id'], $base['config']['url'].'/i/'.$image['filename'] ,$image['extension'],$image['isthumb']);
$sitename = $_SERVER['HTTP_HOST'];
$con1 = "$('#rezone').before('$img');";
$con2 = '[url=' . getlink('show', array($image['id'],'&')) . '][img]'.$base['config']['url'].'/i/'.$image['filename'].$image['extension'].'[/img][/url]\r\n';
$con2 = "bbfull+='{$con2}';";
$con3 = '<a href="' . getlink('show', array($image['id'],'&')) . '"><img src="'.$base['config']['url'].'/i/'.$image['filename'].$extension.'" alt="Host by ' . $sitename . '" /></a><br />\r\n';
$con3 = "htmlfull+='{$con3}';";
$con4 = '[url=' . getlink('show', array($image['id'],'&')) . '][img]'.$base['config']['url'].'/i/'.$image['filename'].($image['isthumb']?'.th':'').$extension.'[/img][/url]\r\n';
$con4 = "bbthumb+='{$con4}';";
$con5 = '<a href="' . getlink('show', array($image['id'],'&')) . '"><img src="'.$base['config']['url'].'/i/'.$image['filename'].($image['isthumb']?'.th':'').$extension.'" alt="Host by ' . $sitename . '" /></a><br />\r\n';
$con5 = "htmlthumb+='{$con5}';";
echo $con1.$con2.$con3.$con4.$con5;
exit();
}
$name = randomstr(1);
$targetfile = $targetpath.$name.$extension;
while(file_exists($targetfile)){
$name .= randomstr(1);
$targetfile = $targetpath.$name.$extension;
}
move_uploaded_file($tempfile,$targetfile);
$thumb = 0;
if($sizew>180||$sizeh>180){
thumbnails($targetfile,$sizew,$sizeh,$targetpath.$name.'.th'.$extension,$filesize,$extension);
$thumb = 1;
}
$now = time();
$db->query("INSERT INTO {$base['config']['db']['pre']}images (ip,time,md5,size,filename,originalname,extension,isthumb) VALUES ('{$_SERVER[REMOTE_ADDR]}','{$now}','{$md5}','{$filesize}','{$name}','{$fullname}','{$extension}','{$thumb}')");
$lastid = $db->last_id();
$img = img($lastid,$base['config']['url'].'/i/'.$name,$extension,$thumb);
$sitename = $_SERVER['HTTP_HOST'];
$con1 = "$('#rezone').before('$img');";
$con2 = '[img]'.$base['config']['url'].'/i/'.$name.$extension.'[/img]\r\n';
$con2 = "bbfull+='{$con2}';";
$con3 = '<a href="' . getlink('show', array($lastid,'&')) . '"><img src="'.$base['config']['url'].'/i/'.$name.$extension.'" alt="Host by ' . $sitename . '" /></a><br />\r\n';
$con3 = "htmlfull+='{$con3}';";
$con4 = '[url=' . getlink('show', array($lastid,'&')) . '][img]'.$base['config']['url'].'/i/'.$name.($thumb?'.th':'').$extension.'[/img][/url]\r\n';
$con4 = "bbthumb+='{$con4}';";
$con5 = '<a href="' . getlink('show', array($lastid,'&')) . '"><img src="'.$base['config']['url'].'/i/'.$name.($thumb?'.th':'').$extension.'" alt="Host by ' . $sitename . '" /></a><br />\r\n';
$con5 = "htmlthumb+='{$con5}';";
echo $con1.$con2.$con3.$con4.$con5;
?>
Code (PHP)
<?
function error_report($text,$title=''){
global $base;
if($title)
$base['title'] = $title;
elseif(strlen($text)>50)
$base['title'] = substr($text,0,50);
else
$base['title'] = $text;
$base['text'] = $text;
template('error');
exit();
}
function paddslashes($data) {
if(is_array($data)) {
foreach($data as $key => $val) {
$data[paddslashes($key)] = paddslashes($val);
}
} else {
$data = str_replace('\"','"',addslashes($data));
}
return $data;
}
function getlink($type, $parameter){
global $base;
switch ($type){
case 'show':
return !$base['config']['rewrite']['show'] ? $base['config']['url'] . '/index.php?mod=show'.$parameter[1].'id=' . $parameter[0] : $base['config']['url'] . '/show/' . $parameter[0];
break;
}
}
function template($name){
global $base;
require_once $base['dir'] . 'template/' . $name . '.temp.php';
}
function lang_set(){
global $all_lang;
echo '<ul id="language">';
foreach($all_lang as $lang=>$value){
echo '<li><a href="javascript:;" onClick="sl(\'' . $lang . '\')">' . $value . '</a></li>';
}
echo '</ul>';
}
function img($dirurl,$dirname,$ex,$thumb,$isadmin='',$ip='',$time='',$views=''){
global $base;
$sitename = $_SERVER['HTTP_HOST'];
$img = '<div class="showimage">';
$img .= '<div class="display">';
$img .= '<a href="' . getlink('show', array($dirurl,'&')) . '" target="_blank"><img src="' . $dirname . ($thumb?'.th':'') . $ex . '" alt="Host by ' . $sitename . '" /></a>';
if($isadmin){
$img .= '<br />IP: ' . $ip . '<br />' . convertdate($time) . ', ' . $base['lang']['image_views'] . ' ' . $views . ' <a href="javascript:;" id="dc' . $dirurl . '" onClick="di(' . $dirurl . ')">' . $base['lang']['delete'] . '</a>';
}
$img .= '</div>';
$img .= '<div class="imgcode"><span>' . $base['lang']['code_direct_url'] . '</span><br />';
$img .= '<input onmouseover="this.select();" name="" type="text" value="' . getlink('show', array($dirurl,'&')) . '" readonly="true" /><br />';
$img .= '<input onmouseover="this.select();" name="" type="text" value="' . $dirname . $ex . '" readonly="true" /><br />';
$img .= '<span>' . $base['lang']['code_bb_full'] . '</span><br />';
$img .= '<input onmouseover="this.select();" name="" type="text" value="" readonly="true" /><br />';
if($thumb){
$img .= '<span>' . $base['lang']['code_bb_thumb'] . '</span><br />';
$img .= '<input onmouseover="this.select();" name="" type="text" value="" readonly="true" /><br />';
}
$img .= '<span>' . $base['lang']['code_html_full'] . '</span><br />';
$img .= '<input onmouseover="this.select();" name="" type="text" value="<a href="' . getlink('show', array($dirurl,'&amp;')) . '" target="_blank"><img src="' . $dirname . $ex . '" alt="Host by ' . $sitename . '" /></a>" readonly="true" /><br />';
if($thumb){
$img .= '<span>' . $base['lang']['code_html_thumb'] . '</span><br />';
$img .= '<input onmouseover="this.select();" name="" type="text" value="<a href="' . getlink('show', array($dirurl,'&amp;')) . '" target="_blank"><img src="' . $dirname . '.th' . $ex . '" alt="Host by ' . $sitename . '" /></a>" readonly="true" />';
}
$img .= '</div>';
$img .= '</div>';
return $img;
}
function convertdate($timestamp){
global $base;
$timestamp2 = time() - $timestamp;
if($timestamp2 == 0){
return $base['lang']['time_now'];
}elseif($timestamp2 < 60){
return $timestamp2 . ' ' . $base['lang']['time_second'] . $base['lang']['time_ago'];
}elseif($timestamp2 < 3600){
return floor($timestamp2/60) . ' ' . $base['lang']['time_minutes'] . $base['lang']['time_ago'];
}elseif($timestamp2 < 86400){
return floor($timestamp2/3600) . ' ' . $base['lang']['time_hour'] . $base['lang']['time_ago'];
}elseif($timestamp2 < 604800){
return floor($timestamp2/86400) . ' ' . $base['lang']['time_day'] . $base['lang']['time_ago'] . ' ' . gmdate("H:i",$timestamp+$base['config']['timezone']);
}else{
return gmdate("d/m/y H:i",$timestamp+$base['config']['timezone']);
}
}
function page($item,$curpage,$perpage,$deurl,$pageurl){
$page = ceil($item/$perpage);
$re = '<ul class="page">';
if($page>$curpage){
$re .= '<li><a href="' . $deurl.str_replace('[page]',$curpage+1,$pageurl).'">»</a></li>';
}
if($page>1){
$re .= '<li><a href="' . $deurl.str_replace('[page]',$page,$pageurl).'">' . $page.'</a></li>';
}
$i = $curpage+3;
if($i>=$page){
$i=$page-1;
}
$max = $i-6;
if($curpage<6&&$page>6){
$i += 5-$curpage;
}
if($i>=$page){
$i=$page-1;
}
if($page-$curpage>4){
$re .= '<li>...</li>';
}
while(($i>=2)&&($i>=$max)){
$re .= '<li><a href="' . $deurl.str_replace('[page]',$i,$pageurl).'">' . $i.'</a></li>';
$i--;
}
if($curpage>5&&$page>9){
$re .= '<li>...</li>';
}
$re .= '<li><a href="' . $deurl.'">1</a></li>';
if($curpage>1){
if($curpage==2){
$re .= '<li><a href="' . $deurl.'">«</a></li>';
}else{
$re .= '<li><a href="' . $deurl.str_replace('[page]',$curpage-1,$pageurl).'">«</a></li>';
}
}
$re .= '</ul>';
return $re;
}
function randomstr($length)
{
$possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9876543210";
$str = "$imageoriginalname";
while(strlen($str) < $length)
{
$str = $str. substr($possible,(rand()%strlen($possible)),1);
}
return $str;
}
function thumbnails($img,$w,$h,$dir,$size,$ex)
{
global $base;
require_once $base['dir'] . 'source/thumb/ThumbLib.inc.php';
$thumb = PhpThumbFactory::create($img);
$thumb->resize(180,180);
$text = $w."x".$h." ".calculateSize($size)." ".substr($ex, 1).' ' . $base['config']['sitename'];
if($w>=$h){
$thumb->writestr($text,$h*$per/100);
}else{
$thumb->writestrup($text,$w*$per/100);
}
$thumb->save($dir);
chmod($dir,0777);
}
function calculateSize($size, $sep = '')
{
$units = array('B', 'K', 'M', 'G', 'T');
for($i = 0, $c = count($units); $i < $c; $i++)
{
if ($size > 1024)
{
$size = $size / 1024;
}
else
{
$unit = $units[$i];
break;
}
}
return round($size, 2).$sep.$unit;
}
?>
Tag : PHP
|
|
|
|
|
|
Date :
2012-04-25 22:13:39 |
By :
youscoms |
View :
1219 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้การสุ่มชื่อครับ หรือจะใช้วันเดือนปีชั่วโมงนาทีวินาทีก็ได้ครับ
Code (PHP)
$filename = date("YmdHis").".jpg";
|
|
|
|
|
Date :
2012-04-26 11:13:23 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
บางครั้งการตั้งชื่อไฟล์แบบนี้มันสามารถช่วยตรวจสอบอะไรได้หลาย ๆ อย่างครับ
|
|
|
|
|
Date :
2012-04-26 11:45:58 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ปัญหาก้อคือว่า เวลาเรา ต้องการใช้งานไฟล์จะหาไม่เจอเพราะชื่อมันแรนดอม สุ่มออกมาให้คับ เลยต้องการโค้ดทำให้ชื่อที่เราตั้งต่อท้ายชื่อที่สุ่มมาคับ ตัวสุ่มทำได้แล้วคับติดตรงชื่อที่เราตั้งอยากให้ต่อท้าย เพื่อจะได้หาง่ายๆ
เช่น http://localhost/hot/i/ud.jpg
http://localhost/hot/i/se.jpg
http://localhost/hot/i/0u.jpg
http://localhost/hot/i/u694.jpg
http://localhost/hot/i/fwe5.jpg
แต่อยากให้มันเป็น http://localhost/hot/i/ud/1234567.jpg
http://localhost/hot/i/se/scs12345678.jpg
http://localhost/hot/i/0u/sdf1234569.jpg
http://localhost/hot/i/u694/aa1234565.jpg
http://localhost/hot/i/fwe5/cc1234.jpg
ชื่อไฟล์ที่อัพเข้า
scs1234567.jpg
sdf12345678.jpg
aa1234569.jpg
bb1234565.jpg
cc1234.jpg
|
|
|
|
|
Date :
2012-04-26 14:45:05 |
By :
youscoms |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คนเว็บนี้เขาทำไม่ได้หรอกค่ะ
|
|
|
|
|
Date :
2012-04-27 21:42:50 |
By :
MomeHo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|