重命名上传文件并显示它 [英] Renaming an upload file and display it

查看:154
本文介绍了重命名上传文件并显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的每一本书和文章都建议我们为安全目的重命名上传文件。假设我有这样的代码:

$ $ p $ $ code if(!isset($ error)){

$ file = $ _FILES ['images'];
$ real_pic_name = $ _FILES ['images'] ['name'] ;;
//为文件创建一个tmp_name:
$ tmp_name = sha1($ file ['name'])。 uniqid(,TRUE);

//将文件移动到正确的文件夹,但添加_tmp,以防万一:
$ dest = PDFS_DIR。 $ tmp_name。 _TMP;

if(move_uploaded_file($ file ['tmp_name'],$ dest)){

//用准备语句插入数据库
$ stmt = $ (真实皮皮名称,tmp_name,dateAdded)VALUES(:real_pic_name,:tmp_pic_name,now())');
$ stmt-> execute(array(
':real_pic_name'=> $ real_pic_name,
':tmp_pic_name'=> $ tmp_name
));

//重命名临时文件:
$ original = PDFS_DIR。 $ tmp_name。 _TMP;
$ dest = PDFS_DIR。 $ tmp_name的值;
重命名($ original,$ dest);

//打印一条消息:
echo'< div class =alert alert-success>< h3>文件已上传!< / h3>< / DIV>';
$ b} else {
trigger_error('文件无法移动');
unlink($ file ['tmp_name']);
}

}

问题是我迷惑关于什么是更好的方式来显示图像文件,因为我不认为重命名文件不是好主意。我想在我的MySQL中添加一些额外的列来保存扩展名,然后可能在会话中调用图像文件,我不知道。任何人都可以给我一些解决方案或示例代码,可以帮助我,因为大部分教程,我只读了解释上传和重命名文件名,而不是文件扩展名既不显示上传文件。感谢。

解决方案

尝试使用此代码

  $ temp = explode(。,$ _ FILES [file] [name]); 
$ newfilename = rand(1,99999)。 '。'.end($ temp);
move_uploaded_file($ _ FILES [file] [tmp_name],../img/imageDirectory/。$ newfilename;


every book and article that i've read recommend that we should rename upload file for security purpose. let say i have a code like this:

if (!isset($error)) {

$file = $_FILES['images'];
$real_pic_name = $_FILES['images']['name'];;
// Create a tmp_name for the file:
$tmp_name = sha1($file['name']) . uniqid('',true);

// Move the file to its proper folder but add _tmp, just in case:
$dest = PDFS_DIR . $tmp_name . '_tmp';

if (move_uploaded_file($file['tmp_name'], $dest)) {

//insert into database with a prepared statement
$stmt = $pdo->prepare('INSERT INTO users (real_pic_name, tmp_name, dateAdded) VALUES (:real_pic_name, :tmp_pic_name, now())');
$stmt->execute(array(
':real_pic_name' => $real_pic_name,
':tmp_pic_name' => $tmp_name
));

// Rename the temporary file:
$original =  PDFS_DIR . $tmp_name . '_tmp';
$dest =  PDFS_DIR . $tmp_name;
rename($original, $dest);

// Print a message:
echo '<div class="alert alert-success"><h3>The file has been uploaded!</h3></div>';

} else {
trigger_error('The file could not be moved.');
unlink ($file['tmp_name']);
}

}

the problem is i'm confuse about what is a better way to display the images files because i don't think that renaming the file isn't good idea. I'm thinking about add some extra column in my mysql to save the extension and then maybe put in in session to call the images file, i don't know. can anyone give me some solution or sample code that can help me since most of tutorial that i've read only explain about uploading and renaming file name and not the file extension neither display the upload files. thanks.

解决方案

Try to use this code

$temp = explode(".",$_FILES["file"]["name"]);
$newfilename = rand(1,99999) . '.' .end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename;

这篇关于重命名上传文件并显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆