php教程 php手册 [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现) - M_Lee

[PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现) - M_Lee

May 20, 2016 pm 01:15 PM

一.安装XAMPP   http://www.cnblogs.com/lidongxu/p/5256330.html

二. 配置MySql http://www.cnblogs.com/lidongxu/p/5256515.html

然后呢, 今天我们就来接触下PHP开发语言

1. 首先呢, 需要在我们本机服务器文件夹资源下新建个.php文件,   废话嘛(你要写php啦!)

2. 在register.php 输入以下代码

<span style="color: #000000;">php 

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 1. 获取客户端利用post方式网络请求的body里的字段对应的value (这个字段
// 是这里规定的, 前端必须遵守这个name2, pass2等key值)</span>
<span style="color: #800080;">$nameP</span> = <span style="color: #800080;">$_POST</span>['name2'<span style="color: #000000;">];
</span><span style="color: #800080;">$passP</span> = <span style="color: #800080;">$_POST</span>['pass2'<span style="color: #000000;">];
</span><span style="color: #800080;">$ageP</span> = <span style="color: #800080;">$_POST</span>['age2'<span style="color: #000000;">];
</span><span style="color: #800080;">$telephoneP</span> = <span style="color: #800080;">$_POST</span>['telephone2'<span style="color: #000000;">];

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 2. 建立数据库连接 (127.0.0.1 数据库所在的ip地址)
// root 是数据库用户名(默认的)
// "" 密码(默认是空)</span>
<span style="color: #800080;">$con</span> = <span style="color: #008080;">mysql_connect</span>("127.0.0.1", "root", ""<span style="color: #000000;">);
</span><span style="color: #800080;">$myCon</span> = <span style="color: #008080;">mysql_select_db</span>("lidongxu", <span style="color: #800080;">$con</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 3. 先查询, 如果存在就不要在插入了</span>
<span style="color: #800080;">$select</span> = "select userName from User where userName = '<span style="color: #800080;">$nameP</span>'"<span style="color: #000000;">;
</span><span style="color: #800080;">$seleResult</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$select</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 4. 如果查到了, 说明已经存在这个用户了, 则返回-1给客户端代表已经注册过了</span>
<span style="color: #0000ff;">if</span> (<span style="color: #008080;">mysql_num_rows</span>(<span style="color: #800080;">$seleResult</span><span style="color: #000000;">)) {

</span><span style="color: #008000;">//</span><span style="color: #008000;"> success 就是key值 对应的value 就是后面的字符串</span>
    <span style="color: #800080;">$a</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$a</span>['success'] = "-1"<span style="color: #000000;">;
    </span><span style="color: #800080;">$a</span>['status'] = "have"<span style="color: #000000;">;
    </span><span style="color: #800080;">$arr</span> = json_encode(<span style="color: #800080;">$a</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 5. 如果没注册过, 那么</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;"> {

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 6. 把数据都插入到mysql数据库中</span>
    <span style="color: #800080;">$sql</span> = "insert into User values('<span style="color: #800080;">$nameP</span>', '<span style="color: #800080;">$passP</span>', '<span style="color: #800080;">$ageP</span>', '<span style="color: #800080;">$telephoneP</span>')"<span style="color: #000000;">;

    </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);

    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$result</span> == 1) { <span style="color: #008000;">//</span><span style="color: #008000;"> 7. 代表执行成功</span>
        <span style="color: #800080;">$a</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
        </span><span style="color: #800080;">$a</span>['success'] = "1"<span style="color: #000000;">;
        </span><span style="color: #800080;">$a</span>['status'] = "ok"<span style="color: #000000;">;
        </span><span style="color: #800080;">$arr</span> = json_encode(<span style="color: #800080;">$a</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;

    }
    </span><span style="color: #0000ff;">else</span> { <span style="color: #008000;">//</span><span style="color: #008000;"> 8. 代表插入失败</span>
        <span style="color: #800080;">$a</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
        </span><span style="color: #800080;">$a</span>['success'] = "0"<span style="color: #000000;">;
        </span><span style="color: #800080;">$a</span>['status'] = "no"<span style="color: #000000;">;
        </span><span style="color: #800080;">$arr</span> = json_encode(<span style="color: #800080;">$a</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
    }

}

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 9. 接收用户头像图片
// 9.1. 接收图片传到服务器上默认的临时文件路径以及名字 (uploadfile 给前台使用的
// 字段)</span>
<span style="color: #800080;">$url</span> = <span style="color: #800080;">$_FILES</span>["uploadimageFile"]["tmp_name"<span style="color: #000000;">];

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 9.2 获取根路径下的downloads文件夹下的路径(download2 需要手动
// 去本地创建)</span>
<span style="color: #800080;">$destination_folder</span> = <span style="color: #800080;">$_SERVER</span>['DOCUMENT_ROOT'].'/download2/'<span style="color: #000000;">;


    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 9.3拼接要作为服务器上保存的文件名字</span>
    <span style="color: #800080;">$newfname</span> = <span style="color: #800080;">$destination_folder</span> .(<span style="color: #0000ff;">string</span>)<span style="color: #800080;">$nameP</span>.'.jpg'; <span style="color: #008000;">//</span><span style="color: #008000;">set your file ext


// 打开连接  rb+ 读写打开一个二进制文件,允许读写数据,文件必须存在。
// 获取客户端上传到缓存文件夹下的文件</span>
    <span style="color: #800080;">$file</span> = <span style="color: #008080;">fopen</span> (<span style="color: #800080;">$url</span>, "rb"<span style="color: #000000;">);

    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$file</span><span style="color: #000000;">) {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。
      // 获取要把客户端传递过来的文件复制到新的文件夹下的名字</span>
      <span style="color: #800080;">$newf</span> = <span style="color: #008080;">fopen</span> (<span style="color: #800080;">$newfname</span>, "a"<span style="color: #000000;">); 

      </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$newf</span><span style="color: #000000;">)
          </span><span style="color: #008000;">//</span><span style="color: #008000;"> 检查文件是否结束,如结束,则返回非零值</span>
      <span style="color: #0000ff;">while</span>(!<span style="color: #008080;">feof</span>(<span style="color: #800080;">$file</span><span style="color: #000000;">)) {
          </span><span style="color: #008000;">//</span><span style="color: #008000;"> 开始从某个文件读取1MB 然后写入到新的路径1MB</span>
        <span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$newf</span>, <span style="color: #008080;">fread</span>(<span style="color: #800080;">$file</span>, 1024 * 8 ), 1024 * 8<span style="color: #000000;"> );

      }
    }

    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$file</span><span style="color: #000000;">) {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 关闭文件链接</span>
      <span style="color: #008080;">fclose</span>(<span style="color: #800080;">$file</span><span style="color: #000000;">);
    }

    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$newf</span><span style="color: #000000;">) {
      </span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$newf</span><span style="color: #000000;">);
    }



 </span>?>
로그인 후 복사

3. 然后在本地服务器文件夹下新建download2文件夹

4. 然后再把登录的php代码也写了吧!在htdocs文件夹下, 新建loginGet.php文件 然后插入代码

<span style="color: #000000;">php 

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 1. 获取GET网络请求网址里的key值对应的value
// 声明变量name1 和pass1 接收</span>
<span style="color: #800080;">$name1</span> = <span style="color: #800080;">$_GET</span>['name'<span style="color: #000000;">];
</span><span style="color: #800080;">$pass1</span> = <span style="color: #800080;">$_GET</span>['pass'<span style="color: #000000;">];

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 2. 建立数据库连接
// 参数1: 数据库所在的服务器的地址(本机127.0.0.1或者localhost)
// 参数2: MySql数据库的账户(默认root)
// 参数3: MySql数据库的密码(默认无)</span>
<span style="color: #800080;">$con</span> = <span style="color: #008080;">mysql_connect</span>("127.0.0.1", "root", ""<span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 参数1: 自己建立的数据库的名字</span>
<span style="color: #800080;">$myCon</span> = <span style="color: #008080;">mysql_select_db</span>("lidongxu", <span style="color: #800080;">$con</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 3. 执行查询 (利用用户名和密码进行匹配查找, 如果找到了随意返回userName(用户名))</span>
<span style="color: #800080;">$sql</span> = "select * from User where userName = '<span style="color: #800080;">$name1</span>' And password = '<span style="color: #800080;">$pass1</span>'"<span style="color: #000000;">;

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 4. 接收结果</span>
<span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 4.2 如果查询结果为空的话</span>
<span style="color: #0000ff;">if</span>(<span style="color: #008080;">mysql_num_rows</span>(<span style="color: #800080;">$result</span>) == 0<span style="color: #000000;">) {
        </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
        </span><span style="color: #800080;">$a</span>['success'] = "0"<span style="color: #000000;">;
        </span><span style="color: #800080;">$a</span>['name'] = "null"<span style="color: #000000;">;
        </span><span style="color: #800080;">$a</span>['status'] = "no"<span style="color: #000000;">;
        </span><span style="color: #800080;">$arr</span> = json_encode(<span style="color: #800080;">$a</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;

        
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 5. 取出本条记录</span>
<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">);
    
    
        </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
        </span><span style="color: #800080;">$a</span>['success'] = "1"<span style="color: #000000;">;
        </span><span style="color: #800080;">$a</span>['name'] = <span style="color: #800080;">$row</span>[0<span style="color: #000000;">];
        </span><span style="color: #800080;">$a</span>['age'] = <span style="color: #800080;">$row</span>[2<span style="color: #000000;">];
        </span><span style="color: #800080;">$a</span>['telephone'] = <span style="color: #800080;">$row</span>[3<span style="color: #000000;">];
        </span><span style="color: #800080;">$a</span>['status'] = "ok"<span style="color: #000000;">;
        </span><span style="color: #800080;">$arr</span> = json_encode(<span style="color: #800080;">$a</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$arr</span><span style="color: #000000;">;
    
}


 </span>?>
로그인 후 복사

到此, php暂时告一段路, 然后进行iOS段代码开发(开心不???)

 

来写iOS 代码吧...........      http://www.cnblogs.com/lidongxu/p/5267753.html

 

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)