Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts

    Unhappy File not uploading

    Hello,

    I had my site working perfectly and decided to upload it to my free domain to test it out. Had issues with the file size's. I took it offline and carried on working on it.

    There seems to be issues with the code now. The information is being sent to the database. But the filesize is saying 0kb and nothing is being uploaded to the 'files' directory. I didn't change anything with regards to the directory destination when moving the file. I've been sat here for about 4 hours now trying different things and trying to figure it out on my own but I just cannot.

    Looking for a bit of guidance from you guys.

    Here is my index.php:
    PHP Code:
    <?php
    ini_set
    ('post_max_size''20M');
    ini_set('upload_max_filesize''20M');
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <html>
    <head>
         <title>Deus Ex Demo Uploader</title>
         <link rel="stylesheet" type="text/css" href="styles/styling.css" />
         <script type="text/javascript" src="scripts/lab.js"></script>
    </head>

    <body>
    <div id="container">
         <div id="headerDiv">
             <img src="images/dxmp111.png" alt="dxdu logo" title="DXDU Logo" id="logo" border="0" />
         </div>
         <h3>Welcome to the Deus Ex Demo Upload/Download Site. This is was designed to allow the players of Deus Ex to upload and download their demos
             in hope people will be interested in looking at other peoples styles.</h3>
         
         <h2 class="floatL">UPLOAD FORM:</h2>
         <div id="formDiv">
         <?php
         $form 
    "<form action='index.php' method='POST' enctype='multipart/form-data'>
                     <table>
                         <tr>
                             <th>Demo Title:</th>
                             <td><input type='text' name='title' /><input type='hidden' name='MAX_FILE_SIZE' value='10240' /></td>
                         </tr>
                         <tr>
                             <th>Description:</th>
                             <td><textarea name='description' cols='28' rows='5' maxlength='170'></textarea></td>
                         </tr>
                         <tr>
                             <th>Browse:</th>
                             <td><input type='file' name='myfile' /></td>
                         </tr>
                         <tr>
                             <th>Upload:</th>
                             <td><input type='submit' name='submitbutton' value='Submit' /></td>
                         </tr>
                     
                     </table>
                 </form>"
    ;
                  
                 function 
    check_input($data) {
                     
    $illegalChars = array('!','@','#','$','%','^','&','*','(',')','+','=','-','[',']','.',';',',','/','{','}','|','"',':','<','>','?'); 
                     
    $data str_replace($illegalChars,'',$data);
                 
                     
    $data trim($data);
                     
    $data stripslashes($data);
                     
    $data htmlspecialchars($dataENT_QUOTES);
                     
    $data mysql_real_escape_string($data);
                     return 
    $data;
                 }  
                  
                 if(isset(
    $_POST['submitbutton'])){
                     
                     
    $title check_input($_POST['title']);
                     
    $description check_input($_POST['description']);
                     
    $hidden $_POST['MAX_FILE_SIZE'];
                     
                     
    $name $_FILES['myfile']['name'];
                     
    $type $_FILES['myfile']['type'];
                     
    $size $_FILES['myfile']['size'];
                     
    $tmpname $_FILES['myfile']['tmp_name'];
                     
    $date date("d/m/Y");
                     
    $ext substr($namestrpos($name'.'));
                     
                     if(
    $ext != ".dem"){
                         echo 
    "$form.<span class='marginL'>You cannot upload that file type...Please try again.</span>";
                     }
                     else{
                         if(
    $name){
                     
                             if(
    $title && $description){

                                 if(
    move_uploaded_file($tmpname"files/"."$name")){
                                     
                                     require(
    "connect.php");
                                     
    $query mysql_query("INSERT INTO demos VALUES ('', '$name','$title', '$description','$size', '$date')");
                                     echo 
    "$form.<span class='marginL'>Your file has been successfully uploaded to the database.</span>";
                                     
                                 }
                                 else{
                                     echo 
    "$form.<span class='marginL'>Your file was not uploaded to the database.</span>";
                                 }
                             }
                             else{
                                 echo 
    "$form.<span class='marginL'>You did not fill in the form completely...Please try again.</span>";
                             }
                         }
                         else{
                             echo 
    "$form.<span class='marginL'>You did not select a file...</span>";
                         }
                     }
                 } 
                 else{
                     echo 
    "$form";
                 }
    ?>
                 </div>
                 <iframe width="450" height="260" id="vid" src="http://www.youtube.com/embed/xP4_0z2M85Q" frameborder="0" allowfullscreen></iframe>
         <hr id="hrule" size="5" noshade="noshade" />
         
         <h2>DOWNLOADS:</h2>
         
         <div id="results">
             <table id="resultsTable">
                 <tr>
                     <th>Demo Name:</th>
                     <th>Description:</th>
                     <th>File Size:</th>
                     <th>Upload Date:</th>
                 </tr>
                 <?php 
                     
    require("connect.php");
                     
    $qry mysql_query("SELECT * FROM demos");
             
                     while(
    $row mysql_fetch_array($qry)){
                         echo 
    '<tr><td><a href="download.php?id='.urlencode($row['id']).'&name='.urlencode($row['name']).'">'.$row['name'].'</a></td><td> '.$row['description'].'</td><td> '.$row['size'].'kb</td><td> '.$row['date'].'</td></tr>';  
                     }
                 
    ?>
             </table>
         </div>
         
         <h4>Author:<span>Labtec</span></h4>
         <div id="clanDiv">
             <a href="http://z13.invisionfree.com/datacollective/index.php?" id="clan" target="_blank">[DC]Data Collectives Clan Forum</a>
         </div>
    </div>

    </body>
    </html>
    Here is my connect.php:
    PHP Code:
    <?php

    $con 
    mysql_connect("localhost""root""");
         if(!
    $con){
             die (
    "<span class='marginL'>Error: Could not connect to server.</span>");
             
    header("Location: index.php");
         }
         
    $db mysql_select_db("files");
         if(!
    $db){
             die(
    "<span class='marginL'>Error: Could not connect to database.</span>");
             
    header("Location: index.php");
         }
    ?>
    I believe the issue lies somewhere either with the $_FILES global because the move_uploaded_file() function within the if statement never returns true.

    Thanks in advance for any help.

    Regards,

    LC.
    Last edited by LearningCoder; 06-21-2012 at 05:39 PM. Reason: changed parts

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You have not done anything to check for a valid upload. You need to determine if its suffered an error (expand switch as necessary to show the error you want using this guide: http://ca.php.net/manual/en/features...d.errors.php):
    PHP Code:
    switch ($_FILES['myfile']['error'])
    {
        case 
    UPLOAD_ERR_OK:
            
    // This is the only valid condition.
            
    break;
        default:
            print 
    'An error has occurred during the upload.';

    Error MUST be checked before moving anything on an uploaded file. The link I have describes each error condition, and you can look into that to find out which one it is. Must be 0 to be valid.
    Could be that max input size. The HTML is constraining it as 10KB which isn't very big when compared to your post file size which is 20MB.

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,178
    Thanks
    15
    Thanked 253 Times in 253 Posts
    First, make sure your files folder has complete read/write permission. Second, if you're working on file uploading, I would suggest that you always have a big fat 'print_r($_FILES);' statement right below the isset statement. Also, there is a big discrepancy between your file size statements.

  • #4
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by Fou-Lu View Post
    You have not done anything to check for a valid upload. You need to determine if its suffered an error (expand switch as necessary to show the error you want using this guide: http://ca.php.net/manual/en/features...d.errors.php):
    PHP Code:
    switch ($_FILES['myfile']['error'])
    {
        case 
    UPLOAD_ERR_OK:
            
    // This is the only valid condition.
            
    break;
        default:
            print 
    'An error has occurred during the upload.';

    Error MUST be checked before moving anything on an uploaded file. The link I have describes each error condition, and you can look into that to find out which one it is. Must be 0 to be valid.
    Could be that max input size. The HTML is constraining it as 10KB which isn't very big when compared to your post file size which is 20MB.
    The link you gave doesn't work bud.

    So within the switch statement, if the condition returns 0, that means the file is....the correct file size, or the correct type etc?

    switch statements are neater to me, could you elaborate a little on what you mean with checking for all errors?

    Regards,

    LC.

  • #5
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by DrDOS View Post
    First, make sure your files folder has complete read/write permission. Second, if you're working on file uploading, I would suggest that you always have a big fat 'print_r($_FILES);' statement right below the isset statement. Also, there is a big discrepancy between your file size statements.
    Which file size statements? Between the ini_set() statements at the very beginning and the hidden <input> tag within the form?

    Regards,

    LC.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The forums didn't interpret the characters properly for the url match and replace. Remove the ending ): from it.

    0 means that the file uploaded without error. It contains no other information.

  • #7
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Would I make cases for all these possible errors?

    Regards,

    LC.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If you want to display specific errors, then yep. I put the default case in there as a generic one.

  • #9
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    I think it would be good at the moment to make cases for all to see exactly what's going on.

    Where would I put this statement, before I do $name = $_FILES['myfile']['name']; etc etc, or do I do it just before?

    Thanks for the help.

    Regards,

    LC.

  • #10
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Ok, managed to get it working. It's returning 2, which means:

    Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

    I have changed the value to 163840(kb) which is 20MB. It doesn't seem to be solving the problem and the switch statement is still returning number 2.

    Regards,

    LC.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I would also recommend all cases.
    There are a few different paths you can choose. Given the nature of the check, it should be the first thing you do. There is no point in evaluating anything else within it if the end result is you can't move it anyway. You can choose from there if you want to block everything within the case statement, or if you would like to assign a variable and use it outside of the switch.
    PHP Code:
    switch ($_FILES['uploadfile']['error'])
    {
        case 
    UPLOAD_ERR_OK:
            
    // do all stuff here
            
    break;

    For an example. I'd probably assign a variable or call a function and trigger a warning instead as I like to keep switches as slim as possible.

    Edit:
    As for your last post, that's still not right. Its in bytes, not bits, so max file size of 20MB would be 20971520. Best I know it doesn't deal with shorthand so you have to put the full value in.
    Last edited by Fou-Lu; 06-22-2012 at 12:00 AM.

  • Users who have thanked Fou-Lu for this post:

    LearningCoder (06-21-2012)

  • #12
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    So for instance I could do:

    PHP Code:
    switch($_FILES['myfile']['error']){
         case 
    0$var getFileInfo(); break;
         return 
    $var;

    PHP Code:
    function getFileInfo(){
          
    $name $_FILES['myfile']['name'];
          
    $size $_FILES['myfile']['size'];
        
    //etc etc etc

    Is that what you mean?

    Regards,

    LC.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $_FILES is superglobal, but I'd never write a function to utilize it. I'd instead accept the array parameter and use that. This is mostly due to how I write generic code though, so its certainly not required.
    The idea is right, but the implementation wrong. You'd have to return something from getFileInfo of course, but the switch shouldn't issue a return.

  • #14
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    What do you mean by accepting the array parameter? So with the switch I use it to check for any errors, if no errors, I should do all the stuff that my getFileInfo() function would do within case 0?

    Really trying to grasp what you mean, sorry if I still haven't.


    Edit: when you say accept the array parameter, you mean like set a variable like this:
    PHP Code:
    $var $_FILES
    ?


    Regards,

    LC.
    Last edited by LearningCoder; 06-22-2012 at 12:29 AM.

  • #15
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Has my 'MAX_FILE_SIZE' input value have to be the same as post_max_size and upload_max_filesize in my php.ini?

    Does the hidden input value automatically reject any files above that value or do I still have to get the value for example:

    PHP Code:
    if($size $_POST['MAX_FILE_SIZE']){
         
    //reject file?

    Regards,

    LC.
    Last edited by LearningCoder; 06-22-2012 at 02:42 AM. Reason: deleted a question.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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