Uploadify - 重命名文件 [英] Uploadify - Rename File
本文介绍了Uploadify - 重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用uploadify,但我不太确定如何编辑php 以重命名上传的文件.
I'm using uploadify, but I'm not too sure how to edit the php to rename the uploaded files.
基本上,一个用户最多可以上传 4 个文件,它们应该命名为 1-img-1、1-img-2、1-img-3、1-img-4 - 第一个数字是用户 ID(可以通过 POST 访问).
Basically, a user can upload upto 4 files and they should be named something like 1-img-1, 1-img-2, 1-img-3, 1-img-4 - the first number being a user id (which can be accessed via the POST).
这是uploadify php脚本:
Here's the uploadify php script:
<?php
/*
UploadiFive
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
*/
// Set the uplaod directory
$uploadDir = '/img/listing_images/';
// Set the allowed file extensions
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) { $i++;
$tempFile = $_FILES['Filedata']['tmp_name'];
$uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
$targetFile = $uploadDir . $_FILES['Filedata']['name'];
// Validate the filetype
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array(strtolower($fileParts['extension']), $fileTypes)) {
// Save the file
move_uploaded_file($tempFile, $targetFile);
echo 1;
} else {
// The file type wasn't allowed
echo 'Invalid file type.';
}
}
?>
只是想知道是否有人可以帮助告诉我如何重命名上传的文件?
Just wondering if someone could help to show me how I would rename the uploaded files?
推荐答案
生成唯一键并使用该名称保护文件,未经测试的示例:
generate a unique key and safe the file with that name, untested example:
$fileParts = pathinfo($_FILES['Filedata']['name']);
$unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT);
$targetFile = $uploadDir . $unique_hash . $fileParts['extension'];
这篇关于Uploadify - 重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文