Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming an image during the uploading PHP Mysql

Using the following code could someone explain to me how I can rename the image files to a misc name during the upload process?

Here is what I'm working with.

uploader.php

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileName = basename($_FILES["image"]["name"]);
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/".$fileName);
if (file_exists($target_path))
{
    echo "An image with that file name already exists.";
}
elseif (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
    // The file is in the images/gallery folder. Insert record into database by
    // executing the following query:
     $sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
     $retval = mysql_query($sql);

    echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";


}
else
{
    echo "There was an error uploading the file, please try again!";
}
?>

Then here is my code for uploading the image to the gallery.


<form enctype="multipart/form-data" action="uploader.php" method="POST">


        Category: <select class="text" name="dataType">
        <option value="treeremoval" selected="selected">treeremoval</option>
        <option value="treetrimming" >treetrimming</option>
        <option value="treebracing" >treebracing</option>
        <option value="stumpgrinding" >stumpgrinding</option>
        <option value="firewood" >firewood</option>
        <option value="cleanup" >cleanup</option>
        </select><br />
<br />




    Caption: <input type="text" name="title"><br />
<br />

    Image to upload: <input type="file" name="image"><br />
<br />




    <input type="submit" value="Upload">
</form>

I am very new to using php and mysql so any help would be appreciated. I have some other questions too, but I guess I should ask one at a time. =)

Thank You!

like image 887
daugaard47 Avatar asked Jul 11 '13 20:07

daugaard47


People also ask

How to change image name while uploading in PHP?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file . $temp = explode(".", $_FILES["file"]["name"]); $newfilename = round(microtime(true)) . '.

How do you rename the file if already exists in PHP?

The rename() function in PHP is an inbuilt function which is used to rename a file or directory.

Can we upload image in MySQL database?

You will use a MySQL database to demonstrate image upload in PHP. The following steps need to be followed to upload an image and display it on a website using PHP: Create a form using HTML for uploading the image files. Connect with the database to insert the image file.


1 Answers

I would try something like this, you will create a unique id and append the extension of the file to it, if that name exists you loop until you have one that doesn't, then you move the file.

<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");

$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);

$fileData = pathinfo(basename($_FILES["image"]["name"]));

$fileName = uniqid() . '.' . $fileData['extension'];

$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);

while(file_exists($target_path))
{
    $fileName = uniqid() . '.' . $fileData['extension'];
    $target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
}

if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
    // The file is in the images/gallery folder. Insert record into database by
    // executing the following query:
     $sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
     $retval = mysql_query($sql);

    echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";


}
else
{
    echo "There was an error uploading the file, please try again!";
}

?>

like image 181
Chris Morrissey Avatar answered Nov 03 '22 18:11

Chris Morrissey