user1259962
user1259962

Reputation:

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.

Upvotes: 0

Views: 298

Answers (3)

StormByte
StormByte

Reputation: 1335

Reading the other answers, while time() approach seems good, it does not provide real unique ids, its accuracy is only 1 second.

But PHP provides a way for having real unique IDS which is more accurate and provide a better solution. You could use it like:

$filename=uniqid().$_FILES['media']['tmp_name'];

Of course changing $_FILES['media']['tmp_name'] for whatever you prefer.

Upvotes: 1

fire1
fire1

Reputation: 82

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'

Upvotes: 0

Krista K
Krista K

Reputation: 21851

You can add the time to the filename, so assuming you will always have a .jpg:

$filename=explode(".",$filename);
$filename[0].="_".time();
$filename=implode(".",$filename);

Upvotes: 0

Related Questions