Web Design, Development & Marketing

Web Development Articles

PHP: Image / File Upload Scripts

Right... So I've been trying to sort a PHP script for uploading images to a server. Simple! Well... I got there in the end. Check out the following:

<form name="uploadForm" method="post" action="" enctype="multipart/form-data">
<input type="file" name="imagefile">
<br />
<input type="submit" name="Submit" value="Submit">
<?php
if(isset($Submit))
{
  if ($_FILES['imagefile']['type'] == "image/gif")
  {
    copy ($_FILES['imagefile']['tmp_name'], "../uploads/".$_FILES['imagefile']['name']) or die ("Could not copy");
    echo "\n\n";
    echo "Copy Done....";
  }
  else
  {
    echo "\n\n";
    echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")";
  }
}
?>
</form>

The above code has actually been adapted from http://www.phpfreaks.com/tutorials/36/0.php. Thanks to Morgan Andersson.

The issue of permissions may arise in the form of a PHP error message stating that the copy could not be completed. If you do encounter a problem, you may be able to resolve it by changing the permissions associated with the upload destination folder (using chmod for linux servers).

Here is another PHP image upload script you may find of use:

<FORM ENCTYPE="multipart/form-data" ACTION="imageupload.php" METHOD="POST">
The file: <INPUT TYPE="file" NAME="userfile">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>

<?php

// Image file upload by Bloody
// http://www.bloodys.com/
// email: info@bloodys.com
// If you use this script, please put a link back to http://www.bloodys.com/

$path = "";
$max_size = 200000;

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "upload failed!<br>\n"; exit; } else { echo "upload sucessful<br>\n"; }

echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
} else { echo "Wrong file type<br>\n"; exit; }

}

?>

This script can be found at Bloody's PHP Scripts along with some other useful PHP code. Enjoy!

Subscribe to RSS Feed Bookmark and Share

Related Links

Related Articles / Posts

Barclaycard ePDQ CPI Integrations (27/05/2009)

HSBC XML API / CPI E-commerce Integrations (02/04/2009)

Recommended Open Source Invoicing System (25/03/2009)

Recent Recommended Links - October 2008 (03/10/2008)

Controlling PHP Register Globals Using .htaccess File (04/03/2008)