Skip to content

About

PHP 图床

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

ImageHost

PHP 循环图床,使用相对路径,可以在config.php中修改参数(最大云端文件数、支持的文件类型)

PHP部署

环境要求

  1. PHP环境
  2. Apache
  3. 具有同目录文件"files"下的读写权限

PHP部署步骤

  1. 部署PHP运行环境。可以使用PhpStudy或其他环境包一键部署。
  2. 下载并拷贝phpUpload文件夹到到www目录下,运行服务即可

Web测试

  1. 浏览器:http://yourip:yourport/phpUpload/upload.html

http api

  1. 使用Post请求的form表单,url为http://yourip:yourport/phpUpload/upload.php
  2. 对于的下载链接样式为:http://yourip:yourport/phpUpload/files/xxxxxxxxxx.png
  3. 上传成功后的相应消息为:
{
  "status": "success",
  "message": "文件上传成功",
  "file_path": "files/68e8efcb8dbf4.png"
}

示例代码

C#示例代码

using System;
using System.IO;
using System.Net;
using System.Text;

class FileUploadClient
{
    static void Main(string[] args)
    {
        Console.WriteLine("C#文件上传客户端");
        Console.Write("输入服务器URL: ");
        string url = Console.ReadLine();
        Console.Write("输入本地文件路径: ");
        string filePath = Console.ReadLine();

        try
        {
            UploadFile(url, filePath);
            Console.WriteLine("\n上传成功!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"上传失败: {ex.Message}");
        }
    }

    static void UploadFile(string url, string filePath)
    {
        string boundary = "----" + DateTime.Now.Ticks.ToString("x");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "multipart/form-data; boundary=" + boundary;

        using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        using (Stream requestStream = request.GetRequestStream())
        {
            // 写入表单头
            string header = $"--{boundary}\r\n" +
                          $"Content-Disposition: form-data; name=\"file\"; filename=\"{Path.GetFileName(filePath)}\"\r\n" +
                          "Content-Type: application/octet-stream\r\n\r\n";
            byte[] headerBytes = Encoding.UTF8.GetBytes(header);
            requestStream.Write(headerBytes, 0, headerBytes.Length);

            // 写入文件内容
            byte[] buffer = new byte[4096];
            int bytesRead;
            long totalBytes = fs.Length;
            long uploadedBytes = 0;

            while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
            {
                requestStream.Write(buffer, 0, bytesRead);
                uploadedBytes += bytesRead;
                UpdateProgress(uploadedBytes, totalBytes);
            }

            // 写入结束边界
            byte[] footerBytes = Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n");
            requestStream.Write(footerBytes, 0, footerBytes.Length);
        }

        // 获取响应
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            string responseText = reader.ReadToEnd();
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"服务器返回错误: {response.StatusCode}\n{responseText}");
            }
        }
    }

    static void UpdateProgress(long uploaded, long total)
    {
        double percent = (double)uploaded / total * 100;
        Console.Write($"\r上传进度: {percent:F2}% ({uploaded}/{total} bytes)");
    }
}

About

PHP 图床

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages