<?php
$paths = "C://Documents and Settings//sk//Desktop//s//"
$d = dir($paths)
while (false !== ($entry = $d->read())) {
$table_change = array(' '=>'_')
$newName = strtr($entry,$table_change)
$newName = substr($newName, 0,-4)
rename($paths.$entry, $paths.$newName."_s.jpg")
}
$d->close()
echo "done"
?>
上传文件或图片进行重命名是我们在php上传中会碰到的一个问题了,在重命名无非就是对你上传的文件生成一个随机的字符串了,包括有Md5,随机,或按时间日期来重命名了,下面我来给大家介绍。
如我们在上传文件页面中如下 *** 作:
$name=$_FILES['userfile']['name'] //将所上传的文件名称赋予name然后我们要对它进行重命名
//文件名的自动生成list($usec, $new_name) = explode(" ", microtime())
if(@$extension_name){//限制文件类型为jpg、gif、png、bmp
move_uploaded_file($tmp_name,$new_dir.'/'.$new_name.'.'.$extension_name)//移动到目标文件夹
echo '上传成功'
这样我们就把文件名以时间日期生成了一个新的文件名了,例:
<form action="upload_file.php" method="post"enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
以下是我添加的将文件名重命名为系统时间的代码:
<?php$date = date(ymdhis)//echo($date)
if ((($_FILES["uppic"]["type"] == "image/gif")
|| ($_FILES["uppic"]["type"] == "image/jpeg")
|| ($_FILES["uppic"]["type"] == "image/pjpeg"))
&& ($_FILES["uppic"]["size"] < 2000000))
{
if ($_FILES["uppic"]["error"] > 0)
{
echo "Return Code: " . $_FILES["uppic"]["error"] . "<br />"
}
else
{
//echo "Upload: " . $_FILES["uppic"]["name"] . "<br />"
//echo "Type: " . $_FILES["uppic"]["type"] . "<br />"
//echo "Size: " . ($_FILES["uppic"]["size"] / 1024) . " Kb<br />"
//echo "Temp file: " . $_FILES["uppic"]["tmp_name"] . "<br />"
$uptype = explode(".", $_FILES["uppic"]["name"])
$newname = $date.".".$uptype[1]
//echo($newname)
$_FILES["uppic"]["name"] = $newname
if (file_exists("pics/" . $_FILES["uppic"]["name"]))
{
echo $_FILES["uppic"]["name"] . " already exists. "
}
else
{
$_FILES["uppic"]["name"] =
move_uploaded_file($_FILES["uppic"]["tmp_name"],
"pics/" . $_FILES["uppic"]["name"])
echo "<script>alert('上传成功!')</script>"
echo "<script>window.location.href='
}
}
}
else
{
echo "Invalid file"
}
?>
大家可以参考一下
//重命名与复制文件$new_name
= $new_path.$new_filename
if(file_exists($new_name)||!file_exists($old_name)){
echo
"目标文件已存在或原文件不存在。"
}else{
@rename($old_name,$new_name)?'成功':'失败'
}
//复制文件
$target_name
= $target_path.$target_filename
if(file_exists($target_name)||!file_exists($source_name)){
echo
"目标文件已经存在或者原始文件不存在。"
}else{
@copy($source_name,$target_name)?'成功':'失败'
}
//删除文件
if(!file_exists($del_name)){
echo
"要删除的文件不存在。"
}else{
unlink($del_name)?'成功':'失败'
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)