search
HomeBackend DevelopmentPHP Tutorial[PHP] Use XAMPP to build a local server, and then use the iOS client to upload data to the local server. Three PHP code implementations

1. Install XAMPP http://www.cnblogs.com/lidongxu/p/5256330.html

2. Configure MySql http://www.cnblogs.com/lidongxu/p/5256515.html

Then, Today we are going to get in touch with the PHP development language

1. First of all, we need to create a new .php file under the resources of our local server folder. What nonsense (you have to write PHP!)

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

2. In Register.php Enter the following code

<span>php 

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

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

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

</span><span>//</span><span> 4. 如果查到了, 说明已经存在这个用户了, 则返回-1给客户端代表已经注册过了</span><span>if</span> (<span>mysql_num_rows</span>(<span>$seleResult</span><span>)) {

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

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

    </span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span><span>);

    </span><span>if</span> (<span>$result</span> == 1) { <span>//</span><span> 7. 代表执行成功</span><span>$a</span> = <span>array</span><span>();
        </span><span>$a</span>['success'] = "1"<span>;
        </span><span>$a</span>['status'] = "ok"<span>;
        </span><span>$arr</span> = json_encode(<span>$a</span><span>);
        </span><span>echo</span><span>$arr</span><span>;

    }
    </span><span>else</span> { <span>//</span><span> 8. 代表插入失败</span><span>$a</span> = <span>array</span><span>();
        </span><span>$a</span>['success'] = "0"<span>;
        </span><span>$a</span>['status'] = "no"<span>;
        </span><span>$arr</span> = json_encode(<span>$a</span><span>);
        </span><span>echo</span><span>$arr</span><span>;
    }

}

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

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


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


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

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

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

      }
    }

    </span><span>if</span> (<span>$file</span><span>) {
        </span><span>//</span><span> 关闭文件链接</span><span>fclose</span>(<span>$file</span><span>);
    }

    </span><span>if</span> (<span>$newf</span><span>) {
      </span><span>fclose</span>(<span>$newf</span><span>);
    }



 </span>?>

3. Then create a new download2 folder under the local server folder

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

4. Then write the login php code too! In the htdocs folder, create a new Then insert the code into the loginGet.php file

<span>php 

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

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

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

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

</span><span>//</span><span> 4. 接收结果</span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span><span>);

</span><span>//</span><span> 4.2 如果查询结果为空的话</span><span>if</span>(<span>mysql_num_rows</span>(<span>$result</span>) == 0<span>) {
        </span><span>$a</span> = <span>array</span><span>();
        </span><span>$a</span>['success'] = "0"<span>;
        </span><span>$a</span>['name'] = "null"<span>;
        </span><span>$a</span>['status'] = "no"<span>;
        </span><span>$arr</span> = json_encode(<span>$a</span><span>);
        </span><span>echo</span><span>$arr</span><span>;

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


 </span>?>

At this point, php has come to an end for the time being, and then we will develop the iOS code (not happy???)

Let’s write iOS code... .. http://www.cnblogs.com/lidongxu/p/5267753.html

The above introduces [PHP] using XAMPP to build a local server, and then using the iOS client to upload data to the local server. The three PHP side code implementations include aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor