Upload and Download Files Over HTTP with PHP

Nov 4, 2008 | Tags: PHP, HTTP | del.icio.us del.icio.us | digg Digg

These functions are used to upload and download files over HTTP. It opens socket connection to the remote host and make HTTP requests. Use these functions to easily upload and download files within your PHP code.

Features:

  • No dependencies
  • Upload and download any files
  • Easy to use
  • Easy to customize

Usage:

1. download file http://www.example.com/path/spec.pdf and save to C:/tmp/spec_local.pdf

Listing 1: download.php

  1. <?php
  2. $remote = "http://www.example.com/path/spec.pdf";
  3. $local  = "C:/tmp/spec_local.pdf";
  4. $res = get_file($remote, $local);
  5. if ($res) {
  6.     echo 'file saved.';
  7. } else {
  8.     echo 'something went wrong.';
  9. }
  10. ?>

2. Simulate HTML form to upload a file

Listing 2: upload.php

  1. <?php
  2. /*
  3.  * the code below simulate HTML form like this:
  4.  *
  5.  * <form enctype="multipart/form-data"
  6.  *       method="post"
  7.  *       action="http://www.example.com/upload.php">
  8.  * <input type="file" name="image">
  9.  * <input type="submit">
  10.  * </form>
  11.  *
  12.  * the script http://www.example.com/upload.php should be able
  13.  * to process the incoming file.
  14.  * maybe something like this:
  15.  *
  16.  * <?php
  17.  * move_uploaded_file(
  18.  *     $_FILES['image']['tmp_name'],
  19.  *     '/var/www/image/uploaded-file.jpg'
  20.  * );
  21.  * echo 'file saved.';
  22.  * ?>
  23.  */
  24. $filename = 'C:/tmp/myphoto.jpg';
  25. $handler  = 'http://www.example.com/upload.php';
  26. $field    = 'image';
  27.  
  28. $res = send_file($filename, $handler, $field);
  29.  
  30. if ($res) {
  31.     echo 'done.';
  32. } else {
  33.     echo 'something went wrong.';
  34. }
  35. ?>

Related Articles

The Downloads

12 Comments

Alexei Korolev on Jun 17, 2008:

Thank you for script. I like it!

Jack on Jul 20, 2008:

Awesome

i like it

blH on Oct 28, 2008:

can't download source.., error msg#: The Server replies that you don't have permissions to download this file.

HTTP/1.0 403 Forbidden

Nash on Oct 29, 2008:

Oops, I'm sorry the file was missing

I've uploaded the file, please try again.

le3on9 on Feb 10, 2009:

I'm still pretty new with PHP. So could you explain more about how to install this?

Nash on Feb 10, 2009:

Just include the file from your code. Maybe something like this:
  1. <?php
  2. include_once 'functions/http_files.php';
  3.  
  4. /* ...your code here... */
  5. ?>

ghprod on Mar 14, 2009:

Cool script .. simple n nice

Thnx .. test it later

Regards

ghprod on Mar 16, 2009:

Hi ...

i'm test ur script to upload local file to tinypic.com, my question is ..

how to take generated link?

regards

using cURL?

Nash on Mar 16, 2009:

That's cool, upload image to tinypic.com and retrieve the links. give me a day or two to write the code to do that.

mahfooz on May 16, 2009:

i want to download file using download dialog box, is it possible to use this script in this case???

Nash on May 17, 2009:

It's not possible since this is an automated script. If you want dialog boxes, use browsers instead

Nash on Jun 24, 2009:

Guys, I've made an advanced HTTP client. It is a utility for HTTP scripting. Using pure PHP, no curl or other dependencies needed.

@ghprod:
Check this script to upload images to TinyPic and retrieve the URLs.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000

Tags

Newsletter

Send me your new post on:
OpenCV
PHP
Projects
Networking
Regex
to my email:
No, I won't spam your email.

Recent Posts

  1. PHP Script for Converting Relative to Absolute URL
  2. Streaming OpenCV Videos Over the Network
  3. Password Protected Images with PHP
  4. OpenCV Region of Interest (ROI)
  5. Web Based Surveillance System with OpenCV, PHP and Javascript

Popular Posts

  1. OpenCV Eye Detection
  2. OpenCV Examples Part 1
  3. OpenCV Face Detection
  4. OpenCV Eye Tracking
  5. Display Video from Webcam
  6. Phase Correlation in OpenCV
  7. Simple File Server and Downloader Script
  8. Building Dynamic SQL String from Associative Array
  9. Template Matching with OpenCV
  10. Fetching a Web Page From Your PHP Code

About the Author

avatar Cool PHP programmer writing cool PHP scripts. Feel free to contact
Tel. +62 31 8662872
+62 856 338 6017
ICQ 489571630
Skype dede_bl4ckheart
Yahoo dede_bl4ckheart
Google nashruddin.amin