google chrome and php

doctorbill
doctorbill used Ask the Experts™
on
I have a very odd problemkj which has surfaced since installing the latest google chrome browser

I am running php version  5.2.5 and mysql client / server version 5.0.37
Internet explorer version 8 and chrome version 19, 0, 1084, 46 m

I have been using a php script for uploading files for years now - in Internet explorer version 8 and google chrome - with no problems. If I now upload a file to the computer using google chrome and the upload php page, the file is  uploaded but is corrupted. The file cannot be opened by google chrome OR internet explorer (chrome tells me there is an error opening the pdf and internet explore tells me there is no %pdf at start of file) - opening the uploaded file with a hex editor shows no data, or rather just a load of squares
.
If I replace the uploaded file with the original version copied to the upload location, it still cannot be opened by google chrome, but is opened by internet explorer with no problem.
If I upload / reupload the file with internet explorer version 8, the file is ok and it opens with both internet explorer AND google chrome.

Any ideas greatly appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
how are you uploading the file? Source code?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
For us to help you, you need to give us enough information to examine the problem.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
This is a test upload using Chrome 19 and it seems to work fine.
WebSiteDesignOutline.pdf
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Ignore - misread above - how do you delete a comment?

Author

Commented:
I will post the source code as soon as I can

Author

Commented:
Here is the code:
-------------------------------------
//upload the file if someone selects a file to upload
$form1file = "";

if($_FILES['form1_file'] && $_FILES['form1_file']['size'] > 0){
      $uploaddir = "ticktockInt/documents/Purchases/";  //folder in which to put the file
      $filename = $_FILES['form1_file']['name'];
      $uploadfile = $uploaddir . $_FILES['form1_file']['name'];
      move_uploaded_file($_FILES['form1_file']['tmp_name'], $uploadfile);
      $form1file = $uploaddir.$filename;
      chmod($uploadfile,0777);
}
//
---------------------------------------
Please note the following:
1. Please re-read my notes at the top of the page - these are very relevant
2. This code works with Internet explore version 8
3. I am using Chrome 19.0.1084.46 m
4. This code has been working for years - it has only caused this issue in the last two weeks (after upgrading Chrome I think but I can't be sure on this)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Ok - but we need the client side code as well....

Author

Commented:
This is on my local computer. I just use a browser box to submit the file to this code
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I am confused - please explain the steps you go through to upload your code - the server side code does not really help on its own.

When you say "I just use a browser box" - what do you mean exactly?

Author

Commented:
The web page has the following code to load a file:
<input name="form1_file" type="file" class="body_bold_blue_10" id="form1_file" />

When the submit button is clicked on the same page, this value is submitted to the script above (on the same page)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Sorry but this is becoming a little bit frustrating

This would usually be encapsulated in a <form></form> element

<input name="form1_file" type="file" class="body_bold_blue_10" id="form1_file" />

Can you post ALL the code for the client side so we can see how the form is defined ...

Author

Commented:
Sorry for the delay
I have attached the complete form

To help you with the navigation and to save time:
Lines 642 - 798 contains the first form on the page, along with the submit button
Once submitted to the same page, the form variables are sent to the scripts on lines 62 - 107, including the file upload script (one of which is shown below and above):
//upload the file if someone selects a file to upload
$form1file = "";

if($_FILES['form1_file'] && $_FILES['form1_file']['size'] > 0){
      $uploaddir = "ticktockInt/documents/Purchases/";  //folder in which to put the file
      $filename = $_FILES['form1_file']['name'];
      $uploadfile = $uploaddir . $_FILES['form1_file']['name'];
      move_uploaded_file($_FILES['form1_file']['tmp_name'], $uploadfile);
      $form1file = $uploaddir.$filename;
      chmod($uploadfile,0777);
}
//

Hope this is clear
database-records-entry.php
Most Valuable Expert 2011
Top Expert 2016
Commented:
Here is my recommendation.  Create a small test PDF file.  Create a stripped down version of a file-upload script.  Test in the simplified environment.  Maybe this script can be helpful.  My guess is that if you can get the same failure with your version of this overly simplified script, you will eventually find that the browser plug-ins for processing PDF files are at a back-level relative to the level of the PDF files.  That is just a guess, but a fairly well educated guess.
<?php // RAY_upload_example.php
error_reporting(E_ALL);


// MANUAL REFERENCE PAGES YOU MUST UNDERSTAND TO UPLOAD FILES
// http://php.net/manual/en/features.file-upload.php
// http://php.net/manual/en/features.file-upload.common-pitfalls.php
// http://php.net/manual/en/function.move-uploaded-file.php
// http://php.net/manual/en/reserved.variables.files.php

// MANUAL PAGES THAT ARE IMPORTANT IF YOU ARE DEALING WITH LARGE FILES
// http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
// http://php.net/manual/en/ini.core.php#ini.post-max-size
// http://php.net/manual/en/info.configuration.php#ini.max-input-time


// PHP 5.1+  SEE http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');

// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY (MUST ALREADY EXIST)
$uploads = 'RAY_junk';

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT - ABOUT 8 MB
$max_file_size = '8000000';

// ESTABLISH THE MAXIMUM NUMBER OF FILES WE CAN UPLOAD
$nf = 3;

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
$file_exts = array
( 'jpg'
, 'gif'
, 'png'
, 'txt'
, 'pdf'
, 'mp3'
)
;

// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;




// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
    ?>
    <h2>Upload <?php echo $nf; ?> file(s)</h2>

    <!--
        SOME THINGS TO NOTE ABOUT THIS FORM...
        ENCTYPE IN THE HTML <FORM> STATEMENT
        MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD
        INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY
        ABSENCE OF ACTION= ATTRIBUTE IN FORM TAG CAUSES POST TO SAME SCRIPT
    -->

    <form name="UploadForm" enctype="multipart/form-data" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
    <p>
    Find the file(s) you want to upload and click the "Upload" button below.
    </p>

    <?php // CREATE INPUT STATEMENTS FOR UP TO $n FILE NAMES
    for ($n = 0; $n < $nf; $n++)
    {
        echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
    }
    ?>

    <br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <strong>overwrite</strong> existing files.
    <input type="submit" value="Upload" />
    </form>
    <?php
    die();
}
// END OF THE FORM SCRIPT



// WE HAVE GOT SOMETHING IN $_POST - RUN THE ACTION SCRIPT
else
{
    // THERE IS POST DATA - PROCESS IT
    echo "<h2>Results: File Upload</h2>\n";

    // ACTIVATE THIS TO SEE WHAT IS COMING THROUGH
    // echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";

    // ITERATE OVER THE CONTENTS OF $_FILES
    foreach ($_FILES as $my_uploaded_file)
    {
        // SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
        $error_code = $my_uploaded_file["error"];
        if ($error_code == 4) continue;

        // SYNTHESIZE THE NEW FILE NAME
        $f_type    = trim(strtolower(end    (explode( '.', basename($my_uploaded_file['name'] )))));
        $f_name    = trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
        $my_new_file
        = getcwd()
        . DIRECTORY_SEPARATOR
        . $uploads
        . DIRECTORY_SEPARATOR
        . $f_name
        . '.'
        . $f_type
        ;
        $my_file
        = $uploads
        . DIRECTORY_SEPARATOR
        . $f_name
        . '.'
        . $f_type;

        // OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
        if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");

        // IF THERE ARE ERRORS
        if ($error_code != 0)
        {
            $error_message = $errors[$error_code];
            die("Sorry, Upload Error Code: $error_code: $error_message");
        }

        // GET THE FILE SIZE
        $file_size = number_format($my_uploaded_file["size"]);

        // IF THE FILE IS NEW (DOES NOT EXIST)
        if (!file_exists($my_new_file))
        {
            // IF THE MOVE FUNCTION WORKED CORRECTLY
            if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
            {
                $upload_success = 1;
            }
            // IF THE MOVE FUNCTION FAILED
            else
            {
                $upload_success = -1;
            }
        }

        // IF THE FILE ALREADY EXISTS
        else
        {
            echo "<br/><b><i>$my_file</i></b> already exists.\n";

            // SHOULD WE OVERWRITE THE FILE? IF NOT
            if (empty($_POST["overwrite"]))
            {
                $upload_success = 0;
            }
            // IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
            else
            {
                $now    = date('Y-m-d');
                $my_bak = $my_new_file . '.' . $now . '.bak';
                if (!copy($my_new_file, $my_bak))
                {
                    echo "<br/><strong>Attempted Backup Failed!</strong>\n";
                }
                if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
                {
                    $upload_success = 2;
                }
                else
                {
                    $upload_success = -1;
                }
            }
        }

        // REPORT OUR SUCCESS OR FAILURE
        if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
        if ($upload_success == 1) { echo "<br/><strong>$my_file</strong> has been saved.\n"; }
        if ($upload_success == 0) { echo "<br/><strong>It was NOT overwritten.</strong>\n"; }
        if ($upload_success < 0)  { echo "<br/><strong>ERROR: $my_file NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</strong>\n"; }
        if ($upload_success > 0)
        {
            echo "$file_size bytes uploaded.\n";
            if (!chmod ($my_new_file, 0755))
            {
                echo "<br/>chmod(0755) FAILED: fileperms() = ";
                echo substr(sprintf('%o', fileperms($my_new_file)), -4);
            }
            echo "<br/><a target=\"_blank\" href=\"$my_file\">See the file $my_file</a>\n";
        }
    // END FOREACH ITERATOR - EACH ITERATION PROCESSES ONE FILE
    }
}

Open in new window


HTH, ~Ray
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
In the file that you posted, lines 1 and 2 are duplicates and the "GetSQLValueString" section is duplicated.  These should be causing errors.  You should turn on error reporting at the top of the file to see what is going on.
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

Open in new window

http://us3.php.net/manual/en/function.error-reporting.php

Author

Commented:
1. Why is it that uploading in  internet explorer is ok
2. If I replace the corrupted file with the original file in the saved location, Chrome still does not accept it (after closing and opening  the browser) when I click on the link to the file, but internet explorer does
3. If I reupload the file using internet explorer and then open chrome, the link to the file works
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
First, IE8 and Chrome19 work fine for uploading and opening files almost everywhere else.  The question really is... What is peculiar about your situation?  As best I can tell from your statements, you are the only one having the problem so it could easily be that your copy of Chrome or IE is corrupted.

Did you fix the errors in your page that I mentioned?

Author

Commented:
I will try to do this tonight
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
To cut out some of the variance in this - clear the cache in all browsers before doing a test. It might be that when you are copying the file up manually the file date is not being updated so the cached version is being used but when you go through IE it actually creates a new file - file date changes - so cache does not come into play.

Not saying it is cache - but clear cache after each Upload / copy - just to see if what you observe is actually what is happening and not being altered by cached copies.
As of last monday (May, 21st), I started to experience the same problem. When uploading any kinds (jpg, PDF, doc, xls, zip etc.) of files via HTTPS, it succeeds. Even the files sizes are equal with the original file. But, when downloading back the same file, it's just a corrupted file. When, I've opened those corrupted files, they are just full with (#) hashes. The same thing works fine with IE and Firefox. Also, when uploading from HTTP, it works as well. It only happens via HTTPS connections.
Commented:
Thanks very much for all the input. I managed to solve the problem by doing a reinstall of the Google chrome browser.
I noticed that when going into the "About Google Chrome" in the previous version the update text was in error. After reinstalling Chrome, the update text shows version 19.0.1084.52. So in addition to the update working, the version is a newer version than the one I had before (see top of this dialogue).
I usually find the typical installation of Chrome does not work - the installer fails due to it thinking an installation may be in progress. This may well be because of the failed update status.
I use the following link which installs the Chrome.exe application into c:\ Program Files ......:
http://www.google.com/chrome/eula.html?system=true

Sometimes a registry edit is needed after this to change paths to the chrome.exe from Documents and Settings.... to c:\Program Files ......

Author

Commented:
Very fast response and very relevant answers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial