Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上传-重命名文件_Php_Javascript_Uploadify - Fatal编程技术网

Php 上传-重命名文件

Php 上传-重命名文件,php,javascript,uploadify,Php,Javascript,Uploadify,我正在使用uploadify,但我不太确定如何编辑php来重命名上传的文件 基本上,一个用户最多可以上传4个文件,它们的名字应该是1-img-1、1-img-2、1-img-3、1-img-4——第一个数字是用户id(可以通过POST访问) 以下是上载php脚本: <?php /* UploadiFive Copyright (c) 2012 Reactive Apps, Ronnie Garcia */ // Set the uplaod directory $uploadDir =

我正在使用uploadify,但我不太确定如何编辑php来重命名上传的文件

基本上,一个用户最多可以上传4个文件,它们的名字应该是1-img-1、1-img-2、1-img-3、1-img-4——第一个数字是用户id(可以通过POST访问)

以下是上载php脚本:

<?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.';

}
}
?>

只需更改$targetFile变量值,并移动$fileParts的声明和赋值,如下所示:

$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = $uploadDir . '1-img-' . $i . $fileParts['extension'];

生成唯一密钥并使用该名称保护文件,未测试示例:

$fileParts = pathinfo($_FILES['Filedata']['name']);
$unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT);
$targetFile = $uploadDir . $unique_hash . $fileParts['extension'];

这是uploadify.php的原始版本

<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination
$targetFolder = '/uploads'; // Relative to the root

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo '1';
} else {
    echo 'Invalid file type.';
}
}
?>
其中rand_string()是一个生成随机Sting的函数


每次你上传一个文件时,它都会生成不同的名称(随机)。希望这有帮助

太棒了。它正在重命名文件,虽然在第一次上传图像后似乎停止了?嗯。我猜每个文件都是在一个单独的请求中发布的,所以文件名都被设置为相同的值($I始终为1)。然后,每个文件都会覆盖上一个文件,看起来只有一个文件被上传。客户端需要发送一个计数器,然后代码可能看起来像
$counter=$\u FILES['Filedata']['counter']$targetFile=$uploadDir'1-img-'$柜台$fileParts['extension']Marc B是一个很好的观点,但安全性是一个单独的主题。您的脚本将允许您的服务器完全受损,尤其是当
$uploadDir
在您的文档根目录中时。你的“安全”根本不是。按用户提供的文件名筛选是不够的。
$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = rtrim($targetPath,'/') . '/' .rand_string(20).'.'.$fileParts['extension'];