0

I have code below and it is working well.It allows files to be uploaded to the database and at the same time moved to the designated folder.However,when i only upload one file, all the other three database columns values are inserted of this type . aw3er45tr56. without file extension .I need a kind person to assist me solve these.

if(!empty($DescAd)){
                        $fileData1 = pathinfo(basename($_FILES["AdImage1"]["name"]));
                        $fileData2 = pathinfo(basename($_FILES["AdImage2"]["name"]));
                        $fileData3 = pathinfo(basename($_FILES["AdImage3"]["name"]));
                        $fileData4 = pathinfo(basename($_FILES["AdImage4"]["name"]));

                            $fileName1 = uniqid() . '.' . $fileData1['extension'];
                            $fileName2 = uniqid() . '.' . $fileData2['extension'];
                            $fileName3 = uniqid() . '.' . $fileData3['extension'];
                            $fileName4 = uniqid() . '.' . $fileData4['extension'];

                            $target_path1 = 'BusinessAdsUploads/'. $fileName1;
                            $target_path2 = 'BusinessAdsUploads/'. $fileName2;
                            $target_path3 = 'BusinessAdsUploads/'. $fileName3;
                            $target_path4 = 'BusinessAdsUploads/'. $fileName4;

                            while(file_exists($target_path1,$target_path2,$target_path3,$target_path4))
                            {
                                $fileName1 = uniqid() . '.' . $fileData['extension'];
                                $fileName2 = uniqid() . '.' . $fileData['extension'];
                                $fileName3 = uniqid() . '.' . $fileData['extension'];
                                $fileName4 = uniqid() . '.' . $fileData['extension'];

                            $target_path1 = 'BusinessAdsUploads/'. $fileName1;
                            $target_path2 = 'BusinessAdsUploads/'. $fileName2;
                            $target_path3 = 'BusinessAdsUploads/'. $fileName3;
                            $target_path4 = 'BusinessAdsUploads/'. $fileName4;

                            }

                            move_uploaded_file($_FILES['AdImage1']['tmp_name'],$target_path1);
                            move_uploaded_file($_FILES['AdImage2']['tmp_name'],$target_path2);
                            move_uploaded_file($_FILES['AdImage3']['tmp_name'],$target_path3);
                            move_uploaded_file($_FILES['AdImage4']['tmp_name'],$target_path4);

                        $query="insert into `MyAds` values('','$bisnaId','$user_id','$Category','$subCategory','$fileName1','$fileName2','$fileName3','$fileName4','$ItemTitle','$DescAd','$AdPrice','Personal Business')";
                        if($query_run=mysql_query($query)){
                            header('location:PreviewBisnaAd.php');
                        }


                }
x tech
  • 53
  • 7
  • 2
    See all of that repeated code? That's a good sign that there's a better way to do it. – John Conde Apr 27 '15 at 15:54
  • 2
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 27 '15 at 15:56

1 Answers1

0

The first thing you will want to do is eliminate the redundant code for files 2,3 and 4, and try your code again with one upload. When you want to upload more than one, simply use your function multiple times.

The reason you are getting the value with no extension is because when you try to upload only one file for a function that takes 4, the other 3 are empty. Your program adds on uniqid() to each of them and inserts that into your database.

Zorgarath
  • 979
  • 12
  • 23