1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Issue with file

Discussion in 'PHP' started by Jeremy Benson, May 10, 2015.

  1. #1
    Hey,

    I'm having an issue with files. I'm using chip upload, and the rest is in regular php. I was able to get a file into the folder, but I made some changes to unlink the old file. Now I only get the file of the name changed in my db... The new file doesn't go into the profile folder, and the old file doesn't unlink.

    
    require('../data/sqldata.php');
    
    session_start();
    $db = new PDO($dsn, $dbUserName, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_SESSION['id']))
    {
       
        require('../classes/php/cUploadHelper.php');
        require('../classes/php/cUser.php');
        require('../lib/chip_upload/cChip.php');
               
        if($_FILES['image']['name'] != '')
        {
           $user = new cUser;
           $user->set_basicID($_SESSION['id']);
          
           $uploader = new cUploadHelper;
           $uploader->upload($_FILES, '../../../users/'.$user->return_username().'/profile/');
          
           $image = $uploader->return_images();
          
           if($uploader->uploaded())
           {
            
              //Send image to db
          
              try{
             
                $sqlUpdateImage = $db->prepare('UPDATE `profile` SET `image`= ? WHERE `userName` = ?');
                $sqlUpdateImage->execute(array($image[0],
                                               $user->return_username()));
    
              }catch(\PDOException $e){echo $e->getMessage();}
             
              // get rid of old profile image
             
              try{
             
                $retrievedImg = array();
                $sqlGetOldImg = $db->prepare('SELECT `image` FROM `profile` WHERE `userName` = ?');
                $sqlGetOldImg->execute(array($user->return_username()));
               
                $retrievedImg = $sqlGetOldImg->fetch();
               
                unlink('../../../users/'.$user->return_username().'/profile/'.$retrievedImg[0].'');
             
              }catch(\PDOException $e){}
          
           }
             
        }else{
        
         //  header('Location: ../../../dashboard.php?option="edit+profile"');
         //  exit;
          
        }
    }else{
        header('../../../dashboard.php');
        exit;
    }
    
    PHP:
    UploadHelper class

    class cUploadHelper{
     
      private $images = array();
      private $uploaded = false;
      function upload($fileDrop, $pathSet)
      {
     
            // test images for errors.
           // upload images.
        
           $upload_directory =  $pathSet;
           $upload = new cChip();
           $files = $upload->get_upload_var($fileDrop['image']);
       
        /*
        |-----------------
        | Upload File
        |------------------
        */
       
        foreach($files as $file ) {
       
            /*
            |---------------------------
            | Upload Inputs
            |---------------------------
            */
           
            $args = array(
                  'upload_file'            =>    $file,
                  'upload_directory'    =>    $upload_directory,
                  'allowed_size'        =>    512000,
                  'extension_check'        =>    TRUE,
                  'upload_overwrite'    =>    FALSE,
              );
             
            $allowed_extensions = array(
                /* Archives */
                            'zip'    => FALSE,
                            '7z'    => FALSE,
                       
                              /* Documents */
                              'txt'    => FALSE,
                            'pdf'    => FALSE,
                              'doc'     => FALSE,
                              'xls'    => FALSE,
                              'ppt'    => FALSE,
                         
                              /* Executables */
                              'exe'    => FALSE,
                       
                              /* Images */
                              'gif'    => TRUE,
                              'png'    => TRUE,
                              'jpg'    => TRUE,
                              'jpeg'    => TRUE,
                       
                              /* Audio */
                              'mp3'    => FALSE,
                              'wav'    => FALSE,
                       
                              /* Video */
                              'mpeg'    => FALSE,
                              'mpg'    => FALSE,
                              'mpe'    => FALSE,
                              'mov'    => FALSE,
                              'avi'    => FALSE
            );
           
            /*
            |---------------------------
            | Upload Hook
            |---------------------------
            */       
           
            $upload_hook = $upload->get_upload( $args, $allowed_extensions );       
               
            /*
            |---------------------------
            | Move File
            |---------------------------
            */
           
            if( $upload_hook['upload_move'] == TRUE ) {
               
                 // update images
                 $this->uploaded = true;
                 array_push($this->images, $file['name']);           
                /*
                |---------------------------
                | Move File
                |---------------------------
                */
               
                $upload_output[] = $upload->get_upload_move();
                //$object->chip_print( $upload_output );
              
            }
               
        } // foreach( $files as $file )
         
        // end upload function
      }
      function return_images()
      {
     
       return $this->images;
     
      }
     
      function uploaded()
      {
     
        if($this->uploaded)
        {
       
          return true;
       
        }else{
       
         return false;
       
        }
     
      }
     
      // end class
    }
    
    PHP:
     
    Jeremy Benson, May 10, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    Using an absolute path rather than relative path might be a good solution. You can also echo at different points throughout your script to see what it does and does not do, maybe even echo some variables or print some arrays to see what is going on. I try to start by echoing where I think the error is occuring and branch out from there.
     
    Anveto, May 10, 2015 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Seriously? You can't see what's wrong with that?
    First, you upload a new image, THEN you fetch the name of the old image (which is now the name of the NEW image you just uploaded) and unlink that... *headdesk*
     
    PoPSiCLe, May 10, 2015 IP
    Anveto likes this.
  4. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #4
    @Jeremy Benson If you are running into these issues every now and then you can use my method of checking yourself for expected order of events. Sometimes a pen and paper to scribble down all the steps is a nice way to review as well. This is something I still do when I run into more complicated issues I can't solve inside my head.
     
    Anveto, May 10, 2015 IP
  5. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #5
    Lol, thanks guys. Your right Popsicle, that was a very burnt thing to miss... also a good tip from Markus. I didn't even consider this as an order of operation error, lol.
     
    Jeremy Benson, May 10, 2015 IP
  6. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    43
    #6
    In general I would also suggest to load scripts and establish a connection to the database only when required...

    
    if($_FILES['image']['name'] != '')
    {
    require('../classes/php/cUploadHelper.php');
    require('../classes/php/cUser.php');
    require('../lib/chip_upload/cChip.php');
    
    Code (markup):
    instead of
    
    require('../classes/php/cUploadHelper.php');
    require('../classes/php/cUser.php');
    require('../lib/chip_upload/cChip.php');
    
    if($_FILES['image']['name'] != '')
    {
    
    Code (markup):
    Something I do , in order to keep the load as low as possible...
    Others might disagree :)
     
    freelanceDeveloper, May 11, 2015 IP