zerey
zerey

Reputation: 871

rename image before uploading to directory

.I have created an image upload form using php where the image will be uploaded to the server directory folder and the name of the picture on the database.. so that when i have to use the images and call it in my browser i will only use a select statement to retrieve the image from the database using also an id. i also included a restriction code where if the user uploads images with the same name an error message will be prompted and the name will not be saved on the database.

what i want to do is to add a renaming function where the images will be renamed before being saved on the directory. can anyone please guide me on the right approach on this. Thanks in advance!

Upvotes: 2

Views: 7128

Answers (4)

rhboss
rhboss

Reputation: 1

hey here is a solution you can try use the rename function to name after it has been sent to the directory and append an identifier to the new name

                //upload_image.php
                <?php
                $target_dir = "user_images/";
                $target_file = $target_dir . basename($_FILES["appphoto1"]["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["appphoto1"]["tmp_name"]);
                    if($check !== false) {
                        echo "File is an image - " . $check["mime"] . ".";
                        $uploadOk = 1;
                    } else {
                        echo "File is not an image.";
                        $uploadOk = 0;
                    }
                }
                // Check if file already exists
                if (file_exists($target_file)) {
                    echo "Sorry, file already exists.";
                    $uploadOk = 0;
                }
                // Check file size
                if ($_FILES["appphoto1"]["size"] > 500000) {
                    echo "Sorry, your file is too large.";
                    $uploadOk = 0;
                }
                // Allow certain file formats
                if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                && $imageFileType != "gif" ) {
                    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                    $uploadOk = 0;
                }
                // Check if $uploadOk is set to 0 by an error
                if ($uploadOk == 0) {
                    echo "Sorry, your file was not uploaded.";
                // if everything is ok, try to upload file
                } else {
                    if (move_uploaded_file($_FILES["appphoto1"]["tmp_name"], $target_file)) {
                        echo "The file ". basename( $_FILES["appphoto1"]["name"]). " has been uploaded.";
                    } else {
                        echo "Sorry, there was an error uploading your file.";
                    }
                }
                ?>
                // database insert page

                include 'upload_image.php';
                   $old = $_FILES['appphoto1']['name'];
                   $new ="appphoto$user_id";
                   **rename** ("user_images/$old", "user_images/$new.$imageFileType");

Upvotes: -2

Wireblue
Wireblue

Reputation: 1509

I often rename the image to a random filename, something that is unique and often won't conflict with other image names. Then, as you say save that random filename in the database along with a human friendly name/title (eg. "My Holiday Pic"). A simple query using the image ID will get the filename and human friendly name.

Use code like this when you're uploading the image to create a unique filename:

var $original_filename = 'my_image.jpg';
var $folder = '/test/images/';
var $file_extension = pathinfo($original_filename, PATHINFO_EXTENSION);

do {
    $filename = substr(sha1(mt_rand().time()), 0, 10).$file_extension;
} while (is_file($folder.$filename));

// New, Unique Filename Ready
echo $filename;

Alternatively you can also swap-out the line the creates the filename and use "uniqid" as fredley suggested.

Upvotes: 0

fredley
fredley

Reputation: 33891

You want PHP's uniqid function, which generates unique ids for exactly this kind of purpose:

$imgname = uniqid("img_",true) . ".jpg";

Upvotes: 1

Stephen Walcher
Stephen Walcher

Reputation: 2575

The easiest way I've found is to place a prefix on all filenames using the time() function. Been using it for years with no overwrite problems.

Example: move_uploaded_file($_FILES['name']['tmp'], time() . "_{$newfilename}");

Upvotes: 5

Related Questions