0

I have a already functioning form that INSERT data into a db, i also have 4x file inputs for images, the form then adds all data, uplaods images and renames images to match the next ID and adds a random end to the filename.

this all works as intented

but i now need the filename(s) to be added to the database, sometimes the form will have 1x imaghe sometimes 4x images but im not sure how to store the filenames in the db from the below code. can anyone help? i assume the array needs to be broken down to individual filenames but not sure how to do it.

    <?php
if(isset($_POST['submit'])){ 

// Variables for date&Time logs
$dateLog = date("y-m-d"); // DATE OF  ADDITION
$timeLog = date("H:i:s", time() - 3600);   // TIME OF ADDITION 

// INSERT QUERY
$sql="INSERT INTO $table1 (firstname, lastname, companyname, phone, email, name, make, serial, catagory, price, location, description, sold, operational, year, clear, rip, version, service, dock, loading, available, extras, dateadded, featured)
VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[companyname]','$dateLog','No')";


$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());

// start of image upload
$insert_id = mysql_insert_id() or die("Unable to get insert id for image name.<br>" . mysql_error());

extract($_POST);
    $error=array();
    $extension=array("jpeg","jpg","png","gif");
    foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
            {
                $file_name=$_FILES["files"]["name"][$key];
                $file_tmp=$_FILES["files"]["tmp_name"][$key];
                $ext=pathinfo($file_name,PATHINFO_EXTENSION);
                if(in_array($ext,$extension))
                {
                    if(!file_exists("../images/listings/".$txtGalleryName."/".$file_name))
                    {
                        $filename=basename($file_name,$ext);
                        $newFileName=$insert_id."_".mt_rand(1, 99999).".".$ext;
                        move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"../images/listings/".$txtGalleryName."/".$newFileName);
                    }
                    else
                    {
                        $filename=basename($file_name,$ext);
                        $newFileName=$filename.mt_rand(1, 99999).".".$ext;
                        move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"../images/listings/".$txtGalleryName."/".$newFileName);
                    }
                }
                else
                {
                    array_push($error,"$file_name, ");
                }
            }
// end of image upload

echo '<p>This item was added successfully</p>';

}
?>

and my form;

1: Upload : <input type="file" name="files[]"/><br />
2: Upload : <input type="file" name="files[]"/><br />
3: Upload : <input type="file" name="files[]"/><br />
4: Upload : <input type="file" name="files[]"/><br />

appreciate any help :)

1 Answer 1

1

you can use $rename_var = rand('111111','999999'); and prepend it before the file name like $new_changed_name = $rename_var.$_FILES['files']['name']; and then use this name while saving the file in folder like so .. move_uploaded_file('tmp_name','path/'.$new_changed_name); and insert this $new_changed_name new name into database.

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

6 Comments

would that do it for each file in the loop?
by doing this inside the loop, all your images will be renamed ... the new names will be like 6 random digits plus your files' original name ... if you upload abc.jpg , the new name will be something like 121122abc.jpg .. 6 random digits prepended.
and how would i add this into the db? as an array>?
Try something like $rename_var = rand('111111','999999'); $file_name= $rename_var.$_FILES["files"]["name"][$key]; and just use this as new name.. you wont have to check the image existence in the folder because the names will be prepended with the six random digits.And your image names won't conflict with newly uploaded image names.
You need to use insert image mysql query like so with the insert_id from your main query $insert_img_query = "insert into images(name,insert_id) values ('".$newFileName."','".$insert_id."')"; mysql_query($insert_img_query); under move_uploaded_file funtion, I am assuming images is the database table name.
|

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.