Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Location
    UK
    Posts
    709

    Question Renaming an Uploaded File in PHP

    Hi all,

    For my new website which allows people to upload their custom spraylogos for CS:S, i need to allow the upload of files.

    I've got it working fine whereby people can only upload certain file extensions below a certain file size...

    But, i need to make sure every spray logo submitted, has it's file renamed to it's unique "spray_ID"...

    I read a tutorial on uploading, and it bascially just tells you how to copy the uploaded file from the temp directory, to the "uploads" directory, giving it the same name as the name it had on the source machine

    This is the code they gave:

    //Copy temp uploaded file to the uploads folder and give it the original name it had on the source machine

    if(copy($_FILES['zipfile']['tmp_name'],$store_dir.$_FILES['zipfile']['name']))
    {
    echo("<br>Uploaded ".$_FILES['zipfile']['name']." successfully.");
    }
    NOW, what ive already tried doing is to replace :

    $store_dir.$_FILES['zipfile']['name']))

    WITH:

    $store_dir.$_FILES['zipfile'][$tempsprayID]))

    and this doesnt work, i get an error.

    PLEASE help!

    Thanks

    Mat...

  2. #2
    Join Date
    May 2004
    Location
    Colorado
    Posts
    948
    well you could do this by storing the last unique value into a file, then openingng that file and getting the integer and incrementing it by one for each new uploaded file, then writing the new incremented value into the file

    that would work i think.. you could try it, or just use a database table to store the number and path for the image, as you migh need to do

    hope that helps you a bit


    Absolute Power, its the only way to roll!

  3. #3
    Join Date
    May 2004
    Location
    UK
    Posts
    709
    I was already storing the path, but i want the file to be renamed so that there can NEVER be two files with the same name...

    i guess the way around it would be to create a new folder for each spraylogo's file... i think thats what i will do in fact

    Mat...

  4. #4
    Join Date
    Oct 2004
    Posts
    2
    You could use the 'uniqid' function, and just test to see if the file exists. There's a good chance that you'd never repeat the unique id, but you could always throw it into a while loop just in case. Such as:

    PHP Code:
    <?php

        define
    ('IMAGE_DIR'$_SERVER['DOCUMENT_ROOT'] . '/sprays/');

        if (!isset(
    $_FILES) || empty($_FILES)) {
            echo 
    '<form action="" method="post" enctype="multipart/form-data">';
            echo 
    'Select your image type to upload<br />';
            echo 
    '<input type="file" name="image"> <input type="submit" value="upload"><br />';
            echo 
    '</form>';
        } else {
            
    $filename 'spray_' uniqid('');
            while (
    file_exists(IMAGE_DIR $filename)) {
                
    $filename 'spray_' uniqid('');
            }
            if (!
    move_uploaded_file($_FILES['image']['tmp_name'], IMAGE_DIR $filename)) {
                echo 
    'There was an error uploading that file.';
                
    print_r($_FILES);
            }
        }

    ?>
    (untested and prone to typos, but you should get the idea.)

    Andy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •