PDA

View Full Version : Làm sao để giữ tên file ảnh khi upload?



mrb00mb00m
21-04-2014, 21:08
Mình có hàm này


function upload_image($input, $image=null, $type='team', $scale=false) {
$year = date('Y'); $day = date('md'); $n = time().rand(1000,9999).'.jpg';
$z = $_FILES[$input];
if ($z && strpos($z['type'], 'image')===0 && $z['error']==0) {
if (!$image) {
RecursiveMkdir( IMG_ROOT . '/' . "{$type}/{$year}/{$day}" );
$image = "{$type}/{$year}/{$day}/{$n}";
$path = IMG_ROOT . '/' . $image;
} else {
RecursiveMkdir( dirname(IMG_ROOT .'/' .$image) );
$path = IMG_ROOT . '/' .$image;
}
if ($type=='user') {
Image::Convert($z['tmp_name'], $path, 60, 60, Image::MODE_CUT);
}
else if ($type=='bank'){
Image::Convert($z['tmp_name'], $path, 120, 50, Image::MODE_CUT);
}
else if($type=='team') {
move_uploaded_file($z['tmp_name'], $path);
}
else if($type=='news') {
move_uploaded_file($z['tmp_name'], $path);
}
else if($type=='slider') {
move_uploaded_file($z['tmp_name'], $path);
}
if($type=='team' && $scale) {
$npath = preg_replace('#(\d+)\.(\w+)$#', "\\1_index.\\2", $path);
Image::Convert($path, $npath, 200, 120, Image::MODE_CUT);
}
return $image;
}
return $image;
}

function user_image($image=null) {
global $INI;
if (!$image) {
return $INI['system']['imgprefix'] . '/static/img/user-no-avatar.gif';
}
return $INI['system']['imgprefix'] . '/static/' .$image;
}

function team_image($image=null, $index=false) {
global $INI;
if (!$image) return null;
if ($index) {
$path = WWW_ROOT . '/static/' . $image;
$image = preg_replace('#(\d+)\.(\w+)$#', "\\1_index.\\2", $image);
$dest = WWW_ROOT . '/static/' . $image;
if (!file_exists($dest) && file_exists($path) ) {
Image::Convert($path, $dest, 210, 135, Image::MODE_SCALE);
}
}
return $INI['system']['imgprefix'] . '/static/' .$image;
}



với


<div class="field">

<label>Ảnh sản phẩm</label>

<input type="file" size="30" name="upload_image" id="team-create-image" class="f-input" />

<?php if($team['image']){?><span class="hint"><?php echo team_image($team['image']); ?></span><?php }?>

</div>

<div class="field">

<label>Ảnh sản phẩm 1</label>

<input type="file" size="30" name="upload_image1" id="team-create-image1" class="f-input" />

<?php if($team['image1']){?><span class="hint" id="team_image_1"><?php echo team_image($team['image1']); ?>&nbsp;&nbsp;<a href="javascript:;" onclick="X.team.imageremove(<?php echo $team['id']; ?>, 1);">delete</a></span><?php }?>

</div>

Mình thấy $n = time().rand(1000,9999).'.jpg'; là nó dùng time để đổi tên file, giờ mình muốn vừa giữ tên file gốc khi upload lên, để tiện cho seo, mình sửa hoài ko đc, cao thủ nào giúp mình với nhé.