Use apache2 + PHP to build file upload service in Windwos

Table of contents

1. Purpose

2. Install the Apache2 web server

2.1 Download Apache2

2.2 Install Apache2

2.3 Modify configuration 

2.4 Add Apache to system services

 3. PHP installation

4. Use PHP to write file upload service


1. Purpose

Build a file upload server so that other devices can upload files to the server through the HTTP protocol. Because the customer's system is windows11, it is different from the previous work on ubuntu, so we will record it.

2. Install the Apache2 web server

2.1 Download Apache2

Install URL:  https://httpd.apache.org/

 

 

 

 

2.2 Install Apache2

Unzip the compressed package to the system location you are used to storing

 ​​​​

 Add environment variables. Add the apache bin directory to the system path, so that you can directly use apache commands in the cmd console. The bin directory of my installation is D:\00_projects\software\httpd-2.4.52-lre342-x64-vs16\Apache24\bin. You need to configure your own directory

 

After the configuration is successful, open a new cmd box to check the version of apache: (you can use the httpd command directly, if you don’t add it to the path, you need to use the full path to use it, which is very troublesome)

 

2.3 Modify configuration 

open httpd.conf

 

Find the definition of SRVROOT, the default path is:

Modify to your own installation path:

 2.4 Add Apache to system services

Use httpd -k install -n Apache to install the service. Of course, the service name can be chosen by yourself. I'm used to being called Apache.

Use net start Apache to start the service. Then enter 127.0.0.1 in the browser to visit the home page of apache.

Use net stop Apache to shut down the service.

 2.5 Test HTTP function

We create a folder test in the htdocs directory (the default http access path), and then create a test.txt file in the test folder

 

Then enter in the browser: http://127.0.0.1/test/test.txt 

 3. PHP installation

php download address: PHP For Windows: Binaries and sources Releases

Download the thread safe version, otherwise it will lack many functions.

 Extract PHP to a directory of your liking:

Go to the php directory, copy php.ini-development as php.ini

 

Edit php.ini, modify the value of extension_dir: {PHP installation path}\ext:

 Add at the end of Apache's httpd.conf (fill in your own installation directory):

LoadModule php_module “${PHP_Installation_Path}\php8apache2_4.dll" 
PHPIniDir "${PHP_Installation_Path}" 
AddType application/x-httpd-php .php
AddType application/x-httpd-php .html

 After the configuration is complete, restart apache using the net command.

4. Use PHP to write file upload service

Create pm/data.php in the htdocs directory

Write code:

<?php
	$file = $_FILES["files"];
	$fileName=$file["name"];
	
	if (!is_dir("xml/")){
		mkdir("xml/");
	}

	$url = "xml/";
	
	if (file_exists($url.$fileName)) {
		echo $fileName." already exists.";
	}else{
		$url=$url.$fileName;
		move_uploaded_file($file["tmp_name"],$url);
		echo "upload ".$fileName." success";
	}
?>

Test with Postman:

 

 Thank you for watching, if you have any questions, welcome to discuss together.

 

 

Guess you like

Origin blog.csdn.net/qq_39298227/article/details/126673721