October 21, 2024
Chicago 12, Melborne City, USA
PHP

restrict user uploading image by 3 files only


im looking

if(empty($id)){
            $save = $this->db->query("INSERT INTO posts set $data");
            if($save && isset($img)){
                $id= $this->db->insert_id;
                mkdir('assets/uploads/'.$id);
                for($i = 0 ; $i< count($img);$i++){
                    list($type, $img[$i]) = explode(';', $img[$i]);
                    list(, $img[$i])      = explode(',', $img[$i]);
                    $img[$i] = str_replace(' ', '+', $img[$i]);
                    $img[$i] = base64_decode($img[$i]);
                    $fname = strtotime(date('Y-m-d H:i'))."_".$imgName[$i];
                    $upload = file_put_contents('assets/uploads/'.$id.'/'.$fname,$img[$i]);
                    $data = " file_path="".$fname."" ";
                }
            }
        }else{
            $save = $this->db->query("UPDATE posts set $data where id = $id");
            if($save){
                if(is_dir('assets/uploads/'.$id)){
                    $gal = scandir('assets/uploads/'.$id);
                    unset($gal[0]);
                    unset($gal[1]);
                    foreach($gal as $k=>$v){
                        unlink('assets/uploads/'.$id.'/'.$v);
                    }
                    rmdir('assets/uploads/'.$id);
                }
                if(isset($img)){
                    mkdir('assets/uploads/'.$id);
                    for($i = 0 ; $i< count($img);$i++){
                        list($type, $img[$i]) = explode(';', $img[$i]);
                        list(, $img[$i])      = explode(',', $img[$i]);
                        $img[$i] = str_replace(' ', '+', $img[$i]);
                        $img[$i] = base64_decode($img[$i]);
                        $fname = strtotime(date('Y-m-d H:i'))."_".$imgName[$i];
                        $upload = file_put_contents('assets/uploads/'.$id.'/'.$fname,$img[$i]);
                        $data = " file_path="".$fname."" ";
                    }
                }
            }
        }
        if($save){
            return 1;
        }

im looking for a solution by posting maximum by 3 image uploads., if ever where do i put count < 3 in the code.

im confuse by putting else where, when i put 3 like this.

for($i = 0 ; $i<3 count($img);$i++){

my uploading is loading only and not posting



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video