Search code examples
phpimageuploadrackspace

Eliminate overwriting of files


I have the following code which uploads images to a rackspace cdn account.

                    error_reporting(E_ALL);
                    ini_set('display_errors', 1);

                    // include the API
                    require("cloudfiles.php") ;

                    // cloud info
                    $username = ''; // username
                    $key ='' ; // api key
                    $container = ''; // container name

                    ob_start();


                    $localfile = $_FILES['media']['tmp_name'];
                    $filename = $_FILES['media']['name'];



                    ob_flush();

                    // Connect to Rackspace
                    $auth = new CF_Authentication($username, $key);
                    $auth->authenticate();
                    $conn = new CF_Connection($auth);

                    // Get the container we want to use
                    $container = $conn->create_container($container);

                    // store file information

                    ob_flush();

                    // upload file to Rackspace
                    $object = $container->create_object($filename);
                    $object->load_from_filename($localfile);

                    $uri = $container->make_public();
                    //print "Your URL is: " . $object->public_uri();

                    $imagePageUrl = $object->public_uri();

                    //echo '<mediaurl>' . $imagePageUrl . '</mediaurl>';


                    ob_end_flush();


                    //echo '<mediaurl>' . $imagePageUrl . '</mediaurl>';

                    echo '<mediaurl>http://url.com/'.$filename.'</mediaurl>';

So each time i am uploading an image, say, image.jpg, it is overwriting the previous image.jpg in the container. I want to prevent that. Even if the filename is the same, is there a way to convert the filename to a random name, characters and then upload it?

Help plz.


Solution

  • ok ... some code is missing ... but you can try to give the new filename (uploaded image)... one name that is unique simple example:

     $filename = time()."-".$_FILES['media']['tmp_name'];
    

    like this way your images will be named like '1333221458-my_image.jpg'