Amit
Amit

Reputation: 3754

PHP: Best way to Uploading and download large files

I have seen the other threads around this topic but they haven't helped much.

I have a simple requirement. I need to create a simple app that allows users to upload and download large files (400 - 1500 MB) from a server. Each user will have his login and the large file needs to be uploaded and downloaded securely.

What is the best way to achieve this in with PHP? Any pitfalls that I should watch out for?

Thanks, Amit

Upvotes: 1

Views: 2079

Answers (2)

inquam
inquam

Reputation: 12942

Does the transfer have to be done through http? You could otherwise have a client application that is triggered when you select to upload an image on your site and handles the upload of the file in question using FTP, SFTP or something similar. This would probably be the best way to achieve good performance and not bog the webserver down.

You could just crank the allowed filesizes up and letting the script run until finished.

ini_set('upload_max_filesize', '1024M');  
ini_set('post_max_size', '1024M');  
ini_set('max_input_time', 0);  
ini_set('max_execution_time', 0);  

But this is not recommended for the sizes of the files you are talking about. Webserver connections would be open for a very long time and aborted uploads would be hard to resume.

So I would look into having a client application handling the actual upload. You could have it made as an Java-applet or as a CLI/GUI application the the user has to download before being able to upload files.

Upvotes: 1

JMax
JMax

Reputation: 26611

PHP supports upload file streaming.

You can use functions like stream_open() & stream_write().

i also found a way on the web to do so with PEAR but i never tried this:

Here is what the author said:

There is alternate method in PEAR to transfer files to server1 to server2. PEAR have HTTP package for uploading files.

HTTP_Request supports GET/POST/HEAD/TRACE/PUT/DELETE, Basic authentication, Proxy, Proxy Authentication, SSL, file uploads etc.

<?php
  require_once "HTTP/Request.php";
  $req =& new HTTP_Request("http://domain.com/upload");
  $req->setBasicAuth("login", "pass");
  $req->setMethod(HTTP_REQUEST_METHOD_POST);

  $result = $req->addFile("data", "/home/svnlabs.mp4");
  if (PEAR::isError($result)) {
    echo "Error occurred during file upload!";
  } else {
    $response = $req->sendRequest();
    if (PEAR::isError($response)) {
      echo "Error occurred during file upload!";
    } else {
      echo "File successfully uploaded!";
    }
  }
?>

Upvotes: 2

Related Questions