#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    File uploading notices


    Hello guys. I am trying to upload images to my website. but i got notice saying:

    Notice: Undefined variable: image_temp in Z:\home\localhost\www\visual2\visual2\upload_image.php on line ( i put a comment where it shows a notice)

    Notice: Undefined variable: image_ext in Z:\home\localhost\www\visual2\visual2\upload_image.php on line ( i put a comment where it shows a notice)

    Notice: Undefined variable: album_id in Z:\home\localhost\www\visual2\visual2\upload_image.php on line ( i put a comment where it shows a notice)

    i can see i have files in my database but with no ''album_id '' and 'ext' tables filled in.

    Here is the code:

    PHP Code:
    <?php
      
    if(isset($_FILES['image']) && $_POST['album_id'] ) {
        
     
    $image_name $_FILES['image'] ['name'] ;
     
    $image_size $_FILES ['image'] ['size']; 
     
    $image_temp $_FILES ['image'] ['tmp_name'];

    $allowed_ext = array('jpeg','jpg','png','gif');
    $image_ext strtolower(end(explode('.'$image_name)));

    $album_id $_POST['album_id'];

    $errors = array();

    if(empty(
    $image_name) || empty($album_id)) {
    $errors [] = 'Something is missing';

    }else {

        if(
    in_array($image_ext$allowed_ext) ===false) {
                 
    $errors [] = 'File type isn\'t allowed';
           } 

           if(
    $image_size 2097152) {
              
    $errors [] = 'Maximum size is 2 mb';
            }

            if(
    $album_check($album_id) ===false) {
                
    $errors [] = 'Could not upload to that album';
            }
        }
    }

    if(!empty(
    $errors)) {
        foreach(
    $errors as $error) {
            echo 
    $error'<br />';
        }
        }else{

        
    upload_image($image_temp$image_ext$album_id); //shows that i have undefined veriables  here, why?
        
    }


    <
    form action="" method="POST" enctype ="multypart/form-data">

        <
    p>Choose a file : <input type="file" name="image" /></p>
        <
    p>
            
    Choose an album:</br

            <
    select name="album_id">
                <?
    php 
                
    foreach($albums as $album){
                echo 
    '<option value ="'.$album['id'].'">',$album['name'],'</option>';
    }
                
    ?>

            </select>
        </p>
        <p><input type="submit" value="Upload"/></p>

    </form>



    <?php    
    }
    ?>


    here is the function for it:

    <?php
    function upload_image($image_temp$image_ext$album_id) {

        
    $album_id = (int) $album_id;

        
    mysql_query("INSERT INTO images VALUES('','".$_SESSION['user_id']."', $album_id , UNIX_TIMESTAMP(), $image_ext)");

        
    $image_id mysql_insert_id();
        
    $image_file $image_id.'.'.$image_ext;
        
    move_uploaded_file ($image_temp'uploads/'.$album_id.'/'.$image_file);

    }

    ?>
    if you know what my mistake is, let me know. i just cant understand why the veriable are undefined.
    thanks for reading
    Last edited by requinix; November 8th, 2012 at 01:10 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    It is difficult to see since you didn't use [ PHP ] tags around your code or properly indent it (see ManiacDan's New User Guide) but it looks like your 'upload_image' statement is outside the 'if (isset' block. That means you can execute that code when the page is first loaded and before the form is submitted.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    depends on where your braces are.. and i dont wanna figure it out
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,294
    Rep Power
    9400
    In the interests of getting an answer,
    Originally Posted by gw1500se
    It is difficult to see since you didn't use [ PHP ] tags around your code or properly indent it (see ManiacDan's New User Guide)
    Originally Posted by paulh1983
    depends on where your braces are.. and i dont wanna figure it out
    Fixed.

    UkraineIsHere: please read the guide that GW linked to, and next time put &amp;#91;php][/php] tags around your PHP code.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    My original surmise is correct. You are executing 'upload_image' even if those variables have not been set yet. You might try this:
    PHP Code:
    if (isset($image_temp) && isset($image_ext) && isset($album_id)) {
       
    upload_image($image_temp$image_ext$album_id);

    Alternatively just move all that into the first 'if' block.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by gw1500se
    My original surmise is correct. You are executing 'upload_image' even if those variables have not been set yet. You might try this:
    PHP Code:
    if (isset($image_temp) && isset($image_ext) && isset($album_id)) {
       
    upload_image($image_temp$image_ext$album_id);

    Alternatively just move all that into the first 'if' block.
    Thank you very much for trying to help me. i dont get any errors now. I now can't upload files to the database. it worked before. even errors [] = ... don't work. i guess i do something completely wrong with my functions. WHat do you think?

    Thanks
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    All my code did was prevent calling 'upload_image' if all the variables were not set. If they are not set when they should be then that is a logic problem you need to resolve. I'm not sure what you mean by "can't upload files to the database." Surely you are not trying to put images into your database are you? In any case I will presume you mean your insert is not working. The first step is to output the generated query string an make sure it is what you expect. Then you should try to copy and paste that string into a command line to see if it works and make adjustments from there.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo