Web Analytics Made Easy -
StatCounter Image upload problem. - CodingForum

Announcement

Collapse
No announcement yet.

Image upload problem.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Image upload problem.

    Hi, I'm working on my very first PHP/MySQL website and I'm quite excited about it: http://swiftsnap.skiroid.com/

    However, one of my pages, front page, doesn't work as I'd like and am hoping one of you guys/gals could spot the fault within the code. I've checked it over numerous times but I just can't see what's going wrong.

    Basically, I'm making an image uploading site and on the font page is a quick upload which checks whether the file selected is an image or not. If it is then it goes ahead and uploads it. Although, it's saying that images are not images and won't upload images.

    I apologise if the code is messy or if there is unessential code but I am a noob when it comes to PHP.

    PHP Code:
    <?php
        $btnupload 
    $_POST['btnupload'];
        
    $btnchoose $_POST['btnchoose'];
        
    $date date("jS F Y, g:ia");
        if (
    $_SESSION['LoginEmail']) {
            if (isset(
    $btnupload)) {
                
    $name $_FILES['picture']['name'];
                
    $type $_FILES['picture']['type'];
                
    $tmpname $_FILES['picture']['tmp_name'];
                
    $ext substr($namestrrpos($name'.'));
                if (
    strstr($type"image")) {
                    
    $querysession mysql_query("SELECT * FROM users WHERE LoginEmail='$sessionemail'");
                    while (
    $row mysql_fetch_assoc($querysession)) {
                        
    $dbaccountid $row['AccountID'];
                    }
                    
    $queryalbum mysql_query("SELECT * FROM albums WHERE Name='Quick Upload' AND CreatedAccountID='$dbaccountid'");
                    while (
    $row mysql_fetch_assoc($queryalbum)) {
                        
    $dbalbumid $row['AlbumID'];
                    }
                    
    $charsetpictureid '0123456789';
                    for (
    $i 0$i <= 10$i++) {
                        
    $randpictureid rand() % strlen($charsetpictureid);
                        
    $tmppictureid substr($charsetpictureid$randpictureid1);
                        
    $codepictureid .= $tmppictureid;
                    }
                    while (
    $numrows != 0) {
                        for (
    $i 0$i <= 10$i++) {
                            
    $randpictureid rand() % strlen($charsetpictureid);
                            
    $tmppictureid substr($charsetpictureid$randpictureid1);
                            
    $codepictureid .= $tmppictureid;
                        }
                    }
                    
    $charsetname '0123456789';
                    for (
    $i 0$i <= 10$i++) {
                        
    $randname rand() % strlen($charsetname);
                        
    $tmpname substr($charsetname$randname1);
                        
    $codename .= $tmpname;
                    }
                    while (
    $numrows != 0) {
                        for (
    $i 0$i <= 10$i++) {
                            
    $randname rand() % strlen($charsetname);
                            
    $tmpname substr($charsetname$randname1);
                            
    $codename .= $tmpname;
                        }
                    }
                    
    mysql_query("INSERT INTO pictures VALUES ('$ccodepictureid','$dbalbumid','$codename','$name','$dbaccountid','$date')");
                    
    mkdir("album/$dbalbumid/");
                    
    move_uploaded_file($tmpname"album/$dbalbumid/".$codename.$ext);
                    echo 
    "
                    <div id='notification-yes'>
                        <div id='notification-yes-image'></div>
                        <h1>Your picture is ready to share!</h1>
                        <p>Your image has been uploaded to:<br /><a href='http://swiftsnap.skiroid.com/quickpic/
    $code$ext' target='_blank'>swiftsnap.skiroid.com/album/$dbalbumid/$codename$ext</a></p>
                    </div>
                    "
    ;
                }
                else
                    echo 
    "
                    <div id='notification-no'>
                        <div id='notification-no-image'></div>
                        <h1>Unable to upload your file.</h1>
                        <p>You have either chosen a file which is not an image or you haven't chosen a file at all. You can only upload images to Swift Snap.</p>
                    </div>
                    "
    ;
            }
            else
                echo 
    "
                <div id='notification-ok'>
                    <div id='notification-ok-image'></div>
                    <h1>Choose your image to upload.</h1>
                    <p>Swift Snap offers you a quick and easy service. All you have to do is choose an image you'd like to upload and you'll have the link to share it within seconds!</p>
                </div>
                "
    ;
        }
        else
            if (isset(
    $btnupload)) {
                echo 
    "
                <div id='notification-no'>
                    <div id='notification-no-image'></div>
                    <h1>You need to be logged in.</h1>
                    <p>You are required to log in to Swift Snap to start uploading your pictures. Registration is free and should take no longer than a minute.</p>
                </div>
                "
    ;
            }
            else
                echo 
    "
                <div id='notification-ok'>
                    <div id='notification-ok-image'></div>
                    <h1>Choose your image to upload.</h1>
                    <p>Swift Snap offers you a quick and easy service. All you have to do is choose an image you'd like to upload and you'll have the link to share it within seconds!</p>
                </div>
                "
    ;
    ?>
    Thanks.

  • #2
    Could you explain please? I'm not too good with PHP sorry.

    Comment


    • #3
      Have you checked your $_FILES array to make sure you are using the correct keys?

      If $type is correct (if it has the mime type of the uploaded file) then there isn't any reason I can see why this shouldn't work...

      In other words, if $type contains any reference to the term "image" then this should be working (or at least it should NOT be rejecting valid images).
      The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
      See Mediocrity in its Infancy
      It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
      Seek and you shall find... basically:
      validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

      Comment


      • #4
        Originally posted by Rowsdower! View Post
        Have you checked your $_FILES array to make sure you are using the correct keys?

        If $type is correct (if it has the mime type of the uploaded file) then there isn't any reason I can see why this shouldn't work...

        In other words, if $type contains any reference to the term "image" then this should be working (or at least it should NOT be rejecting valid images).
        $_FILES array?

        Comment


        • #5
          Add this to the top of the script, just under the "<?php" part:

          PHP Code:
          print_r($_FILES); 
          Then go to your main page and upload a test image. When the page loads you will see a bunch of text looking something like this (variable names and number of files will be different, but generally it will look like this):

          Code:
          Array
          (
              [download] => Array
                  (
                      [name] => Array
                          (
                              [file1] => MyFile.txt
                              [file2] => MyFile.jpg
                          )
          
                      [type] => Array
                          (
                              [file1] => text/plain
                              [file2] => image/jpeg
                          )
          
                      [tmp_name] => Array
                          (
                              [file1] => /tmp/php/php1h4j1o
                              [file2] => /tmp/php/php6hst32
                          )
          
                      [error] => Array
                          (
                              [file1] => UPLOAD_ERR_OK
                              [file2] => UPLOAD_ERR_OK
                          )
          
                      [size] => Array
                          (
                              [file1] => 123
                              [file2] => 98174
                          )
                  )
          )
          Copy what is there and paste it here for me to see...

          I suspect you have an error in your handling of the $_FILES array but I won't know how to tell you to fix it until I see what's there...
          The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
          See Mediocrity in its Infancy
          It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
          Seek and you shall find... basically:
          validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

          Comment


          • #6
            Originally posted by Rowsdower! View Post
            Add this to the top of the script, just under the "<?php" part:

            PHP Code:
            print_r($_FILES); 
            Then go to your main page and upload a test image. When the page loads you will see a bunch of text looking something like this (variable names and number of files will be different, but generally it will look like this):

            Code:
            Array
            (
                [download] => Array
                    (
                        [name] => Array
                            (
                                [file1] => MyFile.txt
                                [file2] => MyFile.jpg
                            )
            
                        [type] => Array
                            (
                                [file1] => text/plain
                                [file2] => image/jpeg
                            )
            
                        [tmp_name] => Array
                            (
                                [file1] => /tmp/php/php1h4j1o
                                [file2] => /tmp/php/php6hst32
                            )
            
                        [error] => Array
                            (
                                [file1] => UPLOAD_ERR_OK
                                [file2] => UPLOAD_ERR_OK
                            )
            
                        [size] => Array
                            (
                                [file1] => 123
                                [file2] => 98174
                            )
                    )
            )
            Copy what is there and paste it here for me to see...

            I suspect you have an error in your handling of the $_FILES array but I won't know how to tell you to fix it until I see what's there...
            => /tmp/phpXQmDm2 [error] => 0 [size] => 562204 ) )

            Comment


            • #7
              It used to work fine but I don't know what's causing it to go wrong now.

              Comment


              • #8
                Originally posted by Skiroid View Post
                => /tmp/phpXQmDm2 [error] => 0 [size] => 562204 ) )
                This is not complete. Can you please double check your output and make sure that the entire string is there?
                The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
                See Mediocrity in its Infancy
                It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
                Seek and you shall find... basically:
                validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

                Comment


                • #9
                  Originally posted by Rowsdower! View Post
                  This is not complete. Can you please double check your output and make sure that the entire string is there?
                  I've put it here. The PHP code has HTML around it but I don't know if that effects anything.

                  PHP Code:
                  <?php
                      print_r
                  ($_FILES);
                      
                  $btnupload $_POST['btnupload'];
                      
                  $btnchoose $_POST['btnchoose'];
                      
                  $date date("jS F Y, g:ia");
                      if (
                  $_SESSION['LoginEmail']) {
                          if (isset(
                  $btnupload)) {
                  Then it comes out with: => /tmp/phpUNIUMu [error] => 0 [size] => 562204 ) )

                  Comment


                  • #10
                    Sorry, I completely forgot that you gave us a link to the page. I ran one myself and this is what shows up in the source code:

                    Code:
                    Array
                    (
                        [btnchoose] => Array
                            (
                                [name] => xxxxxxxxxxxxxxxx.jpg
                                [type] => image/jpeg
                                [tmp_name] => /tmp/php96j3iW
                                [error] => 0
                                [size] => 52557
                            )
                    
                    )
                    Now, what we want to access will be under $_FILES['btnchoose']['type']

                    What you have in your code is $_FILES['picture']['type']

                    You need to change the word "picture" to "btnchoose" and this should work again. If you recently changed the name of your file input that's all it would take for this to have stopped working.
                    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
                    See Mediocrity in its Infancy
                    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
                    Seek and you shall find... basically:
                    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

                    Comment


                    • #11
                      Originally posted by Rowsdower! View Post
                      Sorry, I completely forgot that you gave us a link to the page. I ran one myself and this is what shows up in the source code:

                      Code:
                      Array
                      (
                          [btnchoose] => Array
                              (
                                  [name] => xxxxxxxxxxxxxxxx.jpg
                                  [type] => image/jpeg
                                  [tmp_name] => /tmp/php96j3iW
                                  [error] => 0
                                  [size] => 52557
                              )
                      
                      )
                      Now, what we want to access will be under $_FILES['btnchoose']['type']

                      What you have in your code is $_FILES['picture']['type']

                      You need to change the word "picture" to "btnchoose" and this should work again. If you recently changed the name of your file input that's all it would take for this to have stopped working.
                      Ahh, thanks. Yeh I tried cleaning the code up a bit thanks again!

                      Comment

                      Working...
                      X