Dan Temple
Dan Temple

Reputation: 1182

Uploadify - Rename File

I'm using uploadify, but I'm not too sure how to edit the php to rename the uploaded files.

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).

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?

Upvotes: 0

Views: 1786

Answers (3)

Here's the original 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.';
}
}
?>

I just do it like this.

$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = rtrim($targetPath,'/') . '/' .rand_string(20).'.'.$fileParts['extension'];

Where rand_string() is a function that generates random stings.

This one generates different names (random) each time you upload a file. Hope this helps!

Upvotes: 0

lje
lje

Reputation: 170

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'];

Upvotes: 1

Kale McNaney
Kale McNaney

Reputation: 457

Just change the $targetFile variable value and move the declaration & assignment of $fileParts like this:

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

Upvotes: 0

Related Questions