0

Main goal is to upload files with a unique file_name every time. This file will sit on our webserver and the file_path will be mapped to our database. Then it will be displayed on a post from the file path stored in the database.

I am using the code from here (http://www.w3schools.com/php/php_file_upload.asp)

I need to rename the files that are being uploaded to a auto incremental integer. So the first file uploaded will be 1.file_extention, then the next will be 2.file_extention, and so...

What is the best way to do this?

3
  • Why you use auto incremental ? instead of datetime it uploaded ? you can use auto incremental just for same filename. Commented Apr 16, 2015 at 1:24
  • You need a little more than just datetime because it's possible for two items to be uploaded at exactly the same time but, yes, that's the right track. Commented Apr 16, 2015 at 1:27
  • I am fine with the datetime option as well. Any procedures on doing this with the code from W3? Commented Apr 16, 2015 at 2:31

2 Answers 2

1

If you are really set on using incremental numbers (probably not the best way) and you will be mapping these files in the db anyway, why not do the db insert first (I'm assuming your table has an auto-increment id field) and then use the id field from the row you inserted as the name of the file?

The caveat is that, if renaming the file using the id from the db fails for some reason, you'll need to remove that row but, any time you use such a mapping, you'll need to take that kind of care.

Sign up to request clarification or add additional context in comments.

4 Comments

with the db option, I think it will be a little tricky, by any chance do you know how to do it with the above comments using datetime?
How to do what? Name a file?
Unique file name every time they upload.
Well, if you want to use datetime, just use datetime. But, since it's theoretically possible for two files to be uploaded at exactly the same time (especially if your form has a multiple file input field) you'll need to append something more to the filename to make sure it's unique. Maybe generate a random number or string and append that.
0

Hi you can prefix time stamp using below method

<?php
$target_dir = "uploads/";
$target_file = $target_dir . time().basename($_FILES["fileToUpload"]["name"]); // NOTICE THAT I CONCORDINATED TIME STAMP BEFORE THE FILE NAME
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

If you want to concordinate the primary key you have to insert the record first then to upload the file. If file not uploaded then you have to throw a warning to user that the image not uploaded or have to delete the record.

<?php
    require 'dbConnection.php';

    $stmt = "INSERT INTO TABLENAME (id, name, created) VALUES (NULL, '$name', NOW())";
    $qry = mysql_query($stmt);

    $lastInsertedId = mysql_insert_id();


    $target_dir = "uploads/";
    $target_file = $target_dir . $lastInsertedId.'_'.basename($_FILES["fileToUpload"]["name"]); // NOTICE THAT I CONCORDINATED TIME STAMP BEFORE THE FILE NAME
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }

?>

If you are using PDO connection then you have to modify slightly.

6 Comments

This will add the time stamp to the DB, but it is not changing the file name. I need it to be a unique name every time incase people upload the same file name....
assume i am uploading an image 1.jpg, time stamp will be appened soe TIME()_1.jpg, again next one uploading 1.jpg it will come like time()_1.jpg, its universal truth that time will never repeat again, even though you add two image with same name no problem araise
Thanks the time() worked like a charm, except I dont know how to read this as time 1429475758_1.png, can you break this down for me?
you have to explode the field and date('Y-m-d', // YOUR TIME STAMP GOIES HERE)
How do I save the date and time in the file name? y-m-d-h-m-s
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.