"PHP additions and Deletions Instance" section 24th-specific application of file upload in the project

Source: Internet
Author: User

File upload in the project, generally have two to use, respectively, to set the user's avatar and upload attachments. This section shows you how to upload a user picture.

Because a user individually and uniquely corresponds to an avatar, is a one-to-ones relationship, so we need to add an avatar field to the Tm_users table –header.


Click the Save button to complete the table field additions.

In this section of the Avatar upload, you can crop the image, and ultimately uploaded to the server is a cropped image.

The image is stored on disk, and the database is only responsible for storing the address of the Avatar.

Now copy the Imageupload folder in the 5-10 Class profile folder to the 5-10 folder.

Imageupload is an image-clipping component that supports image cropping, supported by JavaScript Technology and PHP technology. (This component was found on GitHub.) )

Its directory structure is as follows:

To open the User Management page:

The next thing to do is to draw a button to set the avatar, select a piece of data, and then click Set Avatar, will jump out of a page to upload avatar.

Setting the Avatar button's Click event to Openheaderdialog (), we plan to click this button and a dialog box pops up. So, we also need to draw a dialog panel.

Paste the following code at the very end of the body tag.


The address of SRC is:.. /imageupload/index.html

Then, write the method to open the Upload Avatar page:

So far, we can successfully save the image to the corresponding upload folder, but we can't save the image's address to the Tm_users table.

In this case, the index.html of the uploaded avatar is nested outside the usermanage.html page.

Then, in index.html, you can directly access a method on the Usermanage.html page. (Provided that the method is global.) )

Index.html:

Modify Openheaderdialog

var userid = null;function openHeaderDialog(){    var rows = $("#grid0").datagrid("getSelections");    if(rows.length != 1){        $.messager.alert("系统提醒","请选择一位要设置头像的用户!");        return;    }    userid = rows[0].id; //获取当前勾选用户的ID    //只有当你选中一条数据的时候,才打开上传头像的页面    $("#dialog2").dialog("open");}

A method in the parent page can be accessed through parent.

Background:

Background files are basically the same as deleteuser.php, only need to modify a small amount of code.

<?php        $resultData = array();    $resultData["errCode"] = 0;    $resultData["errMsg"] = "";        //连接MySQL数据库    $conn = mysql_connect("localhost","root","");    $db = mysql_select_db("test",$conn);    mysql_query("set names utf8");    $id = $_POST["id"];    $header = $_POST["header"];    $sql = "update tm_users set header = ‘$header‘ where id = $id;";    mysql_query($sql) or die(err());    echo json_encode($resultData);        function err(){        $resultData["errCode"] = -1;        $resultData["errMsg"] = mysql_error();        echo json_encode($resultData);        return;    }?>

SOURCE Access: HTTPS://WWW.JIANSHU.COM/P/4977BD0073D5

"PHP additions and Deletions Instance" section 24th-specific application of file upload in the project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.