开发者

Sending a large file from a client to a server, is my logic correct?

开发者 https://www.devze.com 2023-04-06 07:02 出处:网络
I have some relatively large files (1Mb-32Mb) and I need to send them from a desktop app to a script written in PHP (with the aid of a PHP framework called CodeIgniter). Since the files are pretty big

I have some relatively large files (1Mb-32Mb) and I need to send them from a desktop app to a script written in PHP (with the aid of a PHP framework called CodeIgniter). Since the files are pretty big I'm considering to split those files into small chunks / packets, encode each of them with Base64 and send them one by one through a JSON-formatted message.

I've an idea about how to send them to the server, but I'd like to ask you first what do you think about that, if you think that it's allright or if it's flawed.

Here it is. Since English is not my native language I prefer to expose it as a dialoge between two subjects, a client and a server. I though you would understand a little bit better what I'm trying to do:

Message #1:

Client: "Hi server, I need to send you a -X- kbytes long file. The file I'll send you will have a checksum of -file_cheksum-"

Server: "Ok I'm ready to receive it. Split it in -N- small packets, each with a length of -Y- Kbytes, and send them here one by one, I'll remember what to do with them if in your message you mention that your are sending开发者_StackOverflow packages to assemble a file with the ID -FILE_ID- "

Message #2:

Client: "Here's the first packet of -N-, this packet is required to assemble a file named -FILE_ID-, and here's the checksum of the first packet so you can check if the packet has arrived in good shape"

Server: "Thank you client, I've just memorized it in a temporary folder. Keep going."

...

Message #N + 1:

Client: "Here's the -N- packet of -N-, this is the last packet and it's required to assemble a file named -FILE_ID-, and here's the checksum of the last packet so you can check if the packet has arrived in good shape"

Server: "Thank you client, I've just assembled together all the packets and verified the checksum; the resulting file is OK. Good job."

Each message will be sent via HTTP Post and will be formatted with JSON, and each packet containing a part of the file would be encoded in Base64 and then decoded by the server. Here's an example of a message sent from the client to the server:

{
   "request": "set-packet",
   "id": " [ file ID ] ",
   "packet":
   {
      "file": " [ here goes the packet in Base64 ] ",
      "checksum": " [ here goes the checksum ] "
   }
}

If everything is allright, the server would respond:

{
   "status": "ok",
   "message": "packet memorized"
}

If there's a problem, the server instead would respond for example:

{
   "status": "error",
   "message": "Checksum doesn't match, try again" 
}

What do you think about this logic? Is it correct or flawed, and would you make any improvements on it?

Thank you


I believe your suggested solution is way too much overkill.. You're doing transmission protocol level checks (packet checksums) on an application layer (HTTP). Yes, it could work, but it's litterly doubling the work for nothing, the packet checksums are already happening at a TCP/IP level (long before the HTTP abstraction level).

I would also go for what Dagon suggested, use a protocol intended for file transfer: FTP (or SSH). Any shared host SHOULD have either ftp or ssh (else, how do you get any of your files on the server in the first place :P). Checkout the ftp http://be.php.net/manual/en/book.ftp.php and ssh http://be.php.net/manual/en/book.ssh2.php extensions of php.

Cheers


Just use simple HTTP POST uploads and make sure upload_max_filesize (php.ini setting) is at least 32M. Not point in reinventing the wheel here.

There is also a CI class that you might find useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消