PHP图像上传到mysql问题 - 仅显示部分图像

时间:2016-03-15 12:17:36

标签: php mysql image upload xampp

我非常确定我的代码是正确的。我的问题是,在查看图像时,只显示了一些图像。我认为它与我上传的数据库有关,但我无法弄清楚

这是我的代码:

上传表格

// Identifier for the permission request
private static final int WRITE_CONTACTS_PERMISSIONS_REQUEST = 9;
........

@Override
public void onCreate(Bundle savedInstanceState) {
....
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP_MR1)
    {
        sharedPreferencesEditor.putBoolean(getString(R.string.ALLOW_ACCESS_PHONEBOOK), true);
        sharedPreferencesEditor.commit();
    }
    else {
        getPermissionToReadUserContacts();
    }
....

}


// Called when the user is performing an action which requires the app to read the
// user's contacts
public void getPermissionToReadUserContacts() {
    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
    // checking the build version since Context.checkSelfPermission(...) is only available
    // in Marshmallow
    // 2) Always check for permission (even if permission has already been granted)
    // since the user can revoke permissions at any time through Settings
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

        // The permission is NOT already granted.
        // Check if the user has been asked about this permission already and denied
        // it. If so, we want to give more explanation about why the permission is needed.
        if (shouldShowRequestPermissionRationale(
                Manifest.permission.WRITE_CONTACTS)) {
            // Show our own UI to explain to the user why we need to read the contacts
            // before actually requesting the permission and showing the default UI
        }

        // Fire off an async request to actually get the permission
        // This will show the standard permission request dialog UI
        requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
                WRITE_CONTACTS_PERMISSIONS_REQUEST);
    }
}

// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[],
                                       @NonNull int[] grantResults) {
    // Make sure it's our original READ_CONTACTS request
    if (requestCode == WRITE_CONTACTS_PERMISSIONS_REQUEST) {
        if (grantResults.length == 1 &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Write Contacts permission granted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Write Contacts permission denied", Toast.LENGTH_SHORT).show();
        }
    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

插入图片

    <form id="form2" method="post" name="postForm" action="post.php" enctype="multipart/form-data">
<h2>Post News here</h2>
<p>Title:</p>
<input type="text" required placeholder="Enter a title" maxlength="30" name="postTitle">
<p>Body:</p>
<input type="textarea" required placeholder="Enter a body" maxlength="250" name="postBody">
<p>Link/ Reference:</p>
<input type="text" required placeholder="Enter a link or reference" maxlength="100" name="postLinkRef">
<p>Image to upload:</p>
<input type="file" name="imageTest">
<p><input type="submit" value="Submit" name="submit"></p>
</form>

显示图片

<?php
$host = "localhost";
$userName = "root";
$password = "password";
$db = "userdata";

$connect = mysqli_connect($host,$userName,$password, $db);


if($connect)
{
    if(isset($_POST['submit']) && isset($_FILES['imageTest']))
        {
        $postTitle = mysqli_real_escape_string($connect,$_POST['postTitle']);
        $postBody = mysqli_real_escape_string($connect,$_POST['postBody']);
        $postLink = mysqli_real_escape_string($connect,$_POST['postLinkRef']);
        //address, postcode
        //admin priveleges for scertain accounts
        //wont allow me to use $_FILES and i dont know why...
        $postImageName = mysqli_real_escape_string($connect, $_FILES['imageTest']['name']); 
        $postImageData = mysqli_real_escape_string($connect, file_get_contents($_FILES["imageTest"]["tmp_name"]));
        $postImageType = mysqli_real_escape_string($connect, $_FILES["imageTest"]["type"]);
        $sql = "INSERT INTO news(title, body, link, imageName, imageData) VALUES('$postTitle','$postBody','$postLink','$postImageName','$postImageData')";
        if(mysqli_query($connect, $sql))
        {
            echo "Your post has been uploaded\n\n";
            echo "Thank you for your post $testUserName\n\n";
            echo "<a href='index.php'>Got to your news </a>";
        }
        else
        {
            echo "Sorry, something went wrong!! Check to make sure the image your are uploading is a jpeg image (not any other file)!";
        }
        }
        mysqli_close($connect);
}
else
{
    echo "Fail connection";
}
?>

[enter image description here 1 [enter image description here 2 enter image description here

0 个答案:

没有答案