SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Struggling to Locate File Upload Error

    I think the error is possibly the size of the upload. It's 15MB.

    I'm running Windows Plesk, not a great deal of support available.

    When I upload a small PDF file, say 1MB it uploads correctly, but uploading larger PDF files just gives me a 500 Internal Server Error. There's nothing in the log that indicates what the problem is either.

    Here's my code:

    PHP Code:
    if(isset($_POST['addissue']))  {



        if (
    $_FILES['pdf']['type'] != "application/pdf") {

    // Type is not PDF display error

        
    echo '<h2><strong>Error</strong> We can only accept PDF files at this time.</h2>';

    } else {




        
    // use timestamp to create unique title

        
    $random_digit=time();

        
    // declare where to upload the file

         
    $targeta="../publication/" $userid "/$_GET[id]/".$random_digit."_".$_FILES['pdf']['name'];

        
    // move the file

        
    if(move_uploaded_file($_FILES['pdf']['tmp_name'],$targeta)){

        
    // use timestamp to create unique title

        
    $allowedExts = array("jpg""jpeg""gif""png");
    $extension end(explode("."$_FILES["file"]["name"]));
    if (((
    $_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/png")
    || (
    $_FILES["file"]["type"] == "image/pjpeg"))
    && (
    $_FILES["file"]["size"] < 60000)
    && 
    in_array($extension$allowedExts))
      {

    // Type is not PDF display error

        
    echo '<h2><strong>Error</strong> We can only accept PDF files at this time.</h2>';

    } else {

        
    // declare where to upload the file

         
    $target="../publication/" $userid "/$_GET[id]/".$random_digit."_".$_FILES['img']['name'];

        
    // move the file
        
    if(move_uploaded_file($_FILES['img']['tmp_name'],$target)){



        
    // file has been uploaded, set message
            
    $dialog '<h2><strong>Success</strong> Your publication has been uploaded and is now awaiting moderation.</h2>';

        
    // get string version of UK date
        
    $date date('m-d-Y');

        
    // insert info to the db
        
    $sth $dbconn->prepare("
        INSERT INTO publication_issue
        (user_id, publication_id, issue_number, img, smallimg, pdf, upload_date, cat, viewcount, status, mod_status)
        VALUES     (:user_id,:publication_id,:issue_number,:img,:smallimg,:pdf,:upload_date,:cat,:viewcount,:status,:mod_status)
        
        "
    );


        
        
    $params = array(
        
    user_id => $userid
        
    publication_id => $_GET[id], 
        
    issue_number => $_POST[issue_number], 
        
    img => $target
        
    smallimg => 0,
        
    pdf => $targeta
        
    upload_date => $date
        
    cat => $_POST[category], 
        
    viewcount => 0,
        
    status => 1,
        
    mod_status => 2
        
    );
        
    $sth->execute($params);
        
            
    $result $sth->fetch(); 
           
    //echo $dbconn->lastInsertId();
           
    echo $newid;
           
    $ex true;

     }
    else{
        
    //There's an error
            
    echo '<h2><strong>Error</strong> There was a problem uploading your publication.</h2>';

        }
    }
        if (
    $ex true){
          
    //Everything inserted correctly, add image and update db

        
    }}} 

    It's not hitting any of the errors I have in place leading me to believe there's nothing wrong with the code and I can't think of any other reasons other than the size of the publication or the execution time.

    I have tried to change this in the PHP INI file but to no avail.

    PHP.INI:
    Code:
    max_execution_time	600
    memory_limit	128M
    upload_max_filesize	52428800
    Are there any other things I can do to try and resolve this issue?

    Thanks

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Before processing (moving) the uploaded file, check to see if an error occurred. Use:
    PHP Code:
    $_FILES['pdf']['error'
    Error Messages Explained
    Denny Schlesinger
    web services

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but no output.

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    There's one more ini setting that has to be set to the size you want to allow: post_max_size

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, changed it to 50M but still throws out 500 Internal Server Error. Uploading smaller files work fine.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just an update, it appears I wasn't logging errors so I've changed the value in PHPINI and now have:

    error_log D:\Plesk\Additional\PleskPHP5\phperror.txt
    error_reporting 2048
    log_errors On
    log_errors_max_len 1024
    file_uploads On
    upload_max_filesize 50M
    upload_tmp_dir no value
    post_max_size 50M
    Not sure why there's no tmp dir, but I can upload smaller PDF files so don't think it's this. Having added the error log, I tried to upload again and got the expected 500 error so I navigated to the log and it's empty. Not sure what else I can do?

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by scottlpool2003 View Post
    Not sure what else I can do?
    Like Sherlock Holmes, track down the bug.

    Have you added the error checking as I suggested above? Have your script print out the error. The error 500 will prevent it from being output so kill the script right after:
    PHP Code:
    $error $_FILES['pdf']['error'];
    echo 
    "File error code= ".$error;
    die(); 
    Another way to discover bug evidence is to print out the $_FILES variable:
    PHP Code:
    print_r $_FILES['pdf'];
    die(); 
    This code should be the first thing in the script. If the error 500 persists you know it's the upload and not your code. If the error message prints out you know it's not the upload but something later on in the script that's causing the error 500. In the first case you talk to the web host about it. In the second, you keep on debugging until you find and solve the problem.
    Denny Schlesinger
    web services

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Where's your form code?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Where's your form code?
    Sorry for the delayed reply.

    PHP Code:
    <label>PDF File</label> <input type="file" name="pdf" onchange="checkName(this, 'sname', 'check')"><br />
    <
    input type="text" disabled="disabled" value="" name="denumire1" id="sname" style="display:none;"/>
    <
    label>Cover Image</label> <input type="file" name="img" onchange="checkName(this, 'fname', 'check')" ><br />

    <
    br />
     <
    input type="submit" id="check" name="addissue" value="Submit" disabled="disabled" />
    </
    fieldset>
    <
    input type="text" value="" name="denumire2" id="fname" style="display:none;"/><br />
    </
    form>
    <
    script type="text/javascript">
    var 
    ar_ext = ['pdf''gif''jpeg''jpg''png'];        // array with allowed extensions

    function checkName(eltosbm) {
    // - coursesweb.net
      // get the file name and split it to separe the extension
      
    var name el.value;
      var 
    ar_name name.split('.');

      
    // for IE - separe dir paths (\) from name
      
    var ar_nm ar_name[0].split('\\');
      for(var 
    i=0i<ar_nm.lengthi++) var nm ar_nm[i];

      
    // add the name in 'to'
      
    document.getElementById(to).value nm;

      
    // check the file extension
      
    var re 0;
      for(var 
    i=0i<ar_ext.lengthi++) {
        if(
    ar_ext[i] == ar_name[1]) {
          
    re 1;
          break;
        }
      }

      
    // if re is 1, the extension is in the allowed list
      
    if(re==1) {
        
    // enable submit
        
    document.getElementById(sbm).disabled false;
      }
      else {
        
    // delete the file name, disable Submit, Alert message
        
    el.value '';
        
    document.getElementById(sbm).disabled true;
        
    alert('".'ar_name[1]+ '" is not an file type allowed for upload');
      }
    }
    </script> 

  10. #10
    SitePoint Member
    Join Date
    May 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing your max_execution_time in php.ini
    Maybe your script is taking too long to upload bigger files

  11. #11
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Unless you have changed this

    PHP Code:
    // declare where to upload the file 

         
    $targeta="../publication/" $userid "/$_GET[id]/".$random_digit."_".$_FILES['pdf']['name']; 

        
    // move the file 

        
    if(move_uploaded_file($_FILES['pdf']['tmp_name'],$targeta)){ 
    you are moving the file before checking for errors. That could well cause the 500 error
    Denny Schlesinger
    web services


Bookmarks

Posting Permissions

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