www.webdeveloper.com
Results 1 to 8 of 8

Thread: problem in uploading image

  1. #1
    Join Date
    May 2014
    Posts
    26

    Lightbulb problem in uploading image

    Good Day,

    There's a sticky that TUT about uploading image but i want the restrictions to be in this manner. But my problem here is no notice, no errors but the image file won't upload eventhough the other data has been updated, Here, check my codes. this quite long to be detailed.

    PHP Code:
    <?php
    error_reporting
    (E_ALL E_NOTICE);
    ini_set('display_errors''1');
        
    session_start();
        
    $session_id $_SESSION['user_id'];
        if(
    $session_id == null){
           
    header("location:Student_Edit.php");
           die();
        }
        include 
    'Connect.php';
        
    $flag "";
        
    $student_id $_POST['student_id'];
        
    $first_name $_POST['first_name'];
        
    $last_name $_POST['last_name'];
        
    $gender $_POST['gender'];
        
    $date_of_birth date("Y-m-d",strtotime($_POST['date_of_birth']));
        
    $contact_no $_POST['contact_no'];
        
    $grade  $_POST['grade'];
        
    $section $_POST['section'];
        
    $LRN $_POST['LRN'];
        
    $email1 $_POST['email1'];
        
    $email2 $_POST['email2'];
        
    $address $_POST['address'];
        
    $description $_POST['description'];
        
    $query "UPDATE student_information SET first_name='$first_name',last_name='$last_name',";    
        
    $query .= "gender='$gender',date_of_birth='$date_of_birth',contact_no='$contact_no',grade='$grade',section='$section',";
        
    $query .= "LRN='$LRN',email1='$email1',email2='$email2',address='$address',description='$description'";     
        
    $query .= " WHERE student_id='{$_SESSION['user_id']}'";      
        
    $result mysql_query($query$link_id);
            if(
    mysql_error() != null){
            die(
    mysql_error());
        }
        if(
    $_FILES['image']['name'] != ""){
                    
    //$filename = $_FILES['image']['name'];
                    
    $ext strrchr($filename,".");
                    
    $imagename $student_id;
                    
    $imagename .="_"$filename
                    if(
    $ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
                        
    $size $_FILES['image']['size'];
                        if(
    $size && $size 1000000){
                            
    $archive_dir "images/".$student_id;
                            
    $userfile_tmp_name $_FILES['image']['tmp_name'];
                            if(
    move_uploaded_file($userfile_tmp_name$archive_dir)){
           
    mysql_query("update student_information set image='$imagename' where student_id='{$_SESSION['user_id']}'"$link_id); 
                                
    $flag "success"
                                if(
    mysql_error()!=null){
                                    die(
    mysql_error());
                                }
                            }
                            else{
                                if(
    file_exists('images/' $imagename)) {
                                    
    unlink('images/' $imagename); 
                                }
                                
    rollbackData();
                            }
                        }
                        else{
                            if(
    file_exists('images/' $imagename)) {
                                
    unlink('images/' $imagename); 
                            }
                            
    rollbackData();
                            die(
    "You can upload image of 1 MB size only. Please, try again.");
                        }
                    }           
                    else{
                        if(
    file_exists('images/' $imagename)) {
                            
    unlink('images/' $imagename); 
                        }
                        
    rollbackData();
                        die(
    "You can upload images of .jpg, .jpeg, .gif extensions only. Please, try again. ");
                    }
                  }
                  if(
    $result){ 
            
    $flag "success";
        }
        else{
             
    $flag "error"
        }
        
    header("location:Student_Edit.php?flag=$flag&student_id='{$_SESSION['user_id']}'");   
    ?>
    Please let me know the lines where i get i wrong along with your reccomendation.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    875
    The var '$filename' is not defined at this point:
    Code:
    $ext = strrchr($filename,".");
    Of course you would have an error for undefined var, so it must be set to something previously (somewhere) but is not set by your visible code here and therefore we can't predict what is happening.
    Last edited by ginerjm; 08-05-2014 at 11:57 AM.

  3. #3
    Join Date
    May 2014
    Posts
    26
    @ginerjm:

    what i posted is the whole handler of the page except for the html.
    please teach me how to set the variable $filename and from what line i must have to write it based on my codes. please again

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Just to remind you that for the file uploads you have to set the enctype form's attribute to the multipart/form-data.

    You should also try to change the following lines of code:

    PHP Code:
        if($_FILES['image']['name'] != ""){ 
                    
    //$filename = $_FILES['image']['name']; 
                    
    $ext strrchr($filename,"."); 
                    
    $imagename $student_id
                    
    $imagename .="_"$filename;  
                    if(
    $ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){ 
                        
    $size $_FILES['image']['size']; 
                        if(
    $size && $size 1000000){ 
                            
    $archive_dir "images/".$student_id
                            
    $userfile_tmp_name $_FILES['image']['tmp_name']; 
                            if(
    move_uploaded_file($userfile_tmp_name$archive_dir)) 
    Into something like this:

    PHP Code:
     if (isset($_FILES['image']) and $_FILES['image']['error'] === UPLOAD_ERR_OK) {
            
    $allowedExt = array('image/jpeg''image/gif');
            
    $size $_FILES['image']['size'];
            
            if (!(
    in_array($_FILES['image']['type'], $allowedExt))) {
               
    //do something
            
    } elseif (!($size && $size 1000000)) {
               
    //do something
            
    } else {
               
    $imageName $student_id "_" basename($_FILES['image']['name']);
               
               if (!(
    move_uploaded_file($_FILES['image']['tmp_name'], /* Path to upload + imageName */))) {
                  
    //do something
               
    } else {
                  echo 
    "File uploaded successfully!";
               }
            }
         } 
    Although I'm not sure if the above code is going to solve your problems it's less prone to errors. Also note that I haven't tested the code so I'm not sure if there's any syntax error or something like that...

    BTW - another thing to warn you of is the way you're writing your data to a database. You're not using the mysqli function nor the PDO object. Your're also not applying any filter function, regex... to the user-input data prior to writing it to a database. YOU SHOULDN'T DO THAT!!!
    Last edited by tech_soul8; 08-06-2014 at 06:32 AM.

  5. #5
    Join Date
    May 2014
    Posts
    26
    thank you for that complete advise tech_soul8, i have started this project on mysql as my thesis and my professor just allow me for the sake of the subject but im planning to covert this to mysqli after i have solve this uploading image problem.
    I am just starting to learn php language and not fully understood the regex maybe i must get focus that later after i solve this case so i can focus one thing at a time.

    i am glad for that you edited my codes and this is my current focus now. I just want to clarify something about the modification of codes.
    1. If ill be replacing my codes with your suggested codes do i still need to include codes below in it like the QUERY UPDATED and the rest below?

  6. #6
    Join Date
    May 2014
    Posts
    26

    Exclamation solved notices and warnings

    Hello tech soul,

    i've got brand new codes now and it solves now the 3 errors. i am so delightful and inspired by your last step by step tutorial in helping me undisplaying those errors. I have also modified some codes in HTML page.
    No notices, no warnings, no errors all other data are updating, the only fault is the image won't upload.

    PHP Code:
    <?php
    error_reporting
    (E_ALL E_NOTICE);
    ini_set('display_errors''1');
        
    session_start();
        
    $session_id $_SESSION['user_id'];
        if(
    $session_id == null){
           
    header("location:Student_Edit.php");
           die();
        }
        include 
    'Connect.php';
        
    $flag "success";
        function 
    rollbackData(){
            
    mysql_query(" ROLLBACK ");
            global 
    $flag
            
    $flag "error";
            if(
    mysql_error() != null){
                die(
    mysql_error());
            }
        }
        
    $flag "";
        
    $student_id $_POST['student_id'];
        
    $first_name $_POST['first_name'];
        
    $last_name $_POST['last_name'];
        
    $gender $_POST['gender'];
        
    $date_of_birth date("Y-m-d",strtotime($_POST['date_of_birth']));
        
    $contact_no $_POST['contact_no'];
        
    $grade    $_POST['grade'];
        
    $section $_POST['section'];
        
    $LRN $_POST['LRN'];
        
    $email1 $_POST['email1'];
        
    $email2 $_POST['email2'];
        
    $address $_POST['address'];
        
    $description $_POST['description'];
        
    $imagename "";
        
          
    $query "UPDATE student_information SET first_name='$first_name',last_name='$last_name',";    
        
    $query .= "gender='$gender',date_of_birth='$date_of_birth',contact_no='$contact_no',grade='$grade',section='$section',";
        
    $query .= "LRN='$LRN',email1='$email1',email2='$email2',address='$address',description='$description'";      
        
    $query .= " WHERE student_id='{$_SESSION['user_id']}'";      
        
    $result mysql_query($query$link_id);
            if(
    mysql_error() != null){
            die(
    mysql_error());
        }
        if(
    $_FILES['image']['name'] != ""){
                    
    $filename $_FILES['image']['name'];
                    
    $ext strrchr($filename,".");
                    
    $imagename $student_id;
                    
    $imagename .="_"$filename
                    if(
    $ext ==".jpg" || $ext ==".jpeg" || $ext ==".JPG" || $ext ==".JPEG" || $ext ==".gif" || $ext ==".GIF"){
                        
    $size $_FILES['image']['size'];
                        if(
    $size && $size 1000000){
                            
    $archive_dir "images/".$student_id;
                            
    $userfile_tmp_name $_FILES['image']['tmp_name'];
                            if(
    move_uploaded_file($userfile_tmp_name$archive_dir)){
           
    mysql_query("update student_information set image='$imagename' where student_id='{$_SESSION['user_id']}'"$link_id); 
                                
    $flag "success"
                                if(
    mysql_error()!=null){
                                    die(
    mysql_error());
                                }
                            }
                            else{
                                if(
    file_exists('images/' $imagename)) {
                                    
    unlink('images/' $imagename); 
                                }
                                
    rollbackData();
                            }
                        }
                        else{
                            if(
    file_exists('images/' $imagename)) {
                                
    unlink('images/' $imagename); 
                            }
                            
    rollbackData();
                            die(
    "You can upload image of 1 MB size only. Please, try again.");
                        }
                    }           
                    else{
                        if(
    file_exists('images/' $imagename)) {
                            
    unlink('images/' $imagename); 
                        }
                        
    rollbackData();
                        die(
    "You can upload images of .jpg, .jpeg, .gif extensions only. Please, try again. ");
                    }
                  }
                  if(
    $result){ 
            
    $flag "success";
        }
        else{
             
    $flag "error"
          }
        
    header("location:Student_Edit.php?flag=$flag&student_id='{$_SESSION['user_id']}'");      
    ?>
    These are my brand new codes. Please advise

  7. #7
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Hi hanspeare,

    are you sure you have the appropriate permissions on a server?

    Take a look at this line of code:

    PHP Code:
    if($size && $size 1000000){ 
        
    $archive_dir "images/".$student_id
        
    $userfile_tmp_name $_FILES['image']['tmp_name']; 
        
        if(
    move_uploaded_file($userfile_tmp_name$archive_dir)) 
    notice anything? You're trying to move your uploaded file to a destination folder images/student_id but there's no actual file to move.... you're only specifying the folder path, without the actual file name. That should raise a warning message!

    It should look something like this:

    PHP Code:
    if(move_uploaded_file($userfile_tmp_name$archive_dir '/' basename($_FILES['image']['name'])); 
    The logic you're using is also a good candidate for a discussion. First you're trying to upload your file to a server and then you're checking if the destination folder exists etc... It should be done conversely. The actual action of moving a file to a destination folder should be performed at the end, after all the checking have been made (file type, size, destination folder exists...).

    TIP - If you're referencing a variable just once there's no need to create another variable that will hold its value. It bloats your code.

    PHP Code:
     $userfile_tmp_name $_FILES['image']['tmp_name']; 
                            if(
    move_uploaded_file($userfile_tmp_name$archive_dir)) 
    you could just as easily write it like this:

    PHP Code:
    if(move_uploaded_file($_FILES['image']['tmp_name'], $archive_dir)) 
    so, there's no need for that $userfile_tmp_name variable at all...

    Hope it helps...
    Last edited by tech_soul8; 08-09-2014 at 07:33 AM.

  8. #8
    Join Date
    May 2014
    Posts
    26

    Exclamation profle photo problem

    Hello tech soul,

    i realize this is a profile photo adding problem. I choose to remodify my codes for clean and better understanding. Let me give sometime to recode it and i hope you will still be there to guide when i repost it.

    Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles