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

13 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:
<?php
include_once 'functions/http_files.php';

/* ...your code here... */
?>

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.

Naeem Pasha on Sep 27, 2009:

Wonderfull thank you short and nice script very supportive thanks again

Leave a comment

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

Characters left = 1000

Tags

Recent Posts

  1. OpenCV 2.0 Installation on Windows XP and Visual Studio 2008
  2. Runtime ROI Selection using Mouse
  3. Real Time Eye Tracking and Blink Detection
  4. HTTP Scripting Made Really Easy
  5. PHP Script for Converting Relative to Absolute URL
View Archives

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

Recommended Sites:

Hacker's HTTP Client
HTML and CSS Tutorials
Stop Dreaming Start Action
Online Quran and Translation