Jump to content

Recommended Posts

Hello,

 

I've looked everywhere for help, is there any chance anyone here that knows of PHP can help me out?

 

I have this snippet of code for uploading files. I really want it to rename the files with a date and/or time or anything random at the end of the image's name as it uploads. Would be an amazing help!! Thanks!

 

Here is the code.

 

<?php
// Set the uplaod directory
$uploadDir = '/images/';

if (!empty($_FILES)) {
    $tempFile   = $_FILES['Filedata']['tmp_name'][0];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
    $targetFile = $uploadDir . $_FILES['Filedata']['name'][0];

    // Validate the file type
    $fileTypes = array('jpg'); // Allowed file extensions
    $fileParts = pathinfo($_FILES['Filedata']['name'][0]);

    // Validate the filetype
    if (in_array($fileParts['extension'], $fileTypes)) {

        // Save the file
        move_uploaded_file($tempFile,$targetFile);
        echo 1;

    } else {

        // The file type wasn't allowed
        echo 'Invalid file type.';

    }
}
?>

Vinlock ment $targetFile, I guess.

 

If you want to alter the filename (witout extension) you first have to devide the filename from extension. For example with:

$FileNameComplete = explode('.', $_FILES['Filedata']['name'][0]);

$FileName = $FileNameComplete[count($FileNameComplete)-1];

$FileExt = $FileNameComplete[count($FileNameComplete)];

 

Then you can puzzle the new Filename you want the uploaded file to have, i.e.

$targetFile = $uploadDir . $FileName . "_" .date('U') . "." . $FileExt;

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.