Quality Image Upload Script?

Hi,

I am trying to find a quality image upload script. I found one however it bundles all the errors together

Is there a standard, reliable image upload script for PHP?

Hi,
Try the script from this page: http://www.coursesweb.net/php-mysql/upload-script-images-audio-gallery_s2
Checks the type (extension) and size of the files, width and height for images, before upload.
It works for images, and audio files. Data like: Title, Description, File_path can be stored in mysql table.

Cheers dude, it comes across as being a bit OTT. Is there a simpler script which can be done on one page?

You could either write one yourself which is not hard, modify one to your requirements or check out hotscripts.

Thanks, I’ll check out hotscripts.

I thought there would just be a standard, off the shelf upload script which is widely used.

Hi,
Try this script, it is very simple:

<?php
// Simple PHP Upload Script:  http://www.coursesweb.net/php-mysql/

$uploadpath = 'upload/';      // directory to store the uploaded files
$max_size = 2000;          // maximum file size, in KiloBytes
$alwidth = 900;            // maximum allowed width, in pixeli
$alheight = 800;           // maximum allowed height, in pixeli
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        // allowed extensions

if(isset($_FILES['fileup'])) {
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
  $type = end(explode('.', strtolower($_FILES['fileup']['name'])));
  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height
  $err = '';

  // Checks if the file has allowed type, size, width and height (for images)
  if(!in_array($type, $allowtype)) $err .= 'The file <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB';
  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x 
  '. $alheight;

  // If no errors, upload the image, else, output the errors
  if($err == '') {
    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
      echo 'File:<b> '. basename( $_FILES['fileup']['name']). '</b> succesfully uploaded:';
      echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
      if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
      echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\\\/').'/'.$uploadpath.'</b>';
    }
    else echo '<b>Unable to upload the file.</b>';
  }
  else echo $err;
}
?> 
<div style="margin:1em auto; width:333px; text-align:center;">
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
  Upload File: <input type="file" name="fileup" /><br/>
  <input type="submit" name='submit' value="Upload" /> 
 </form>
</div>

Brilliant thanks, I got this working straight away.

It uploads the image but how do I associate an image to a user. Lets say a company registers and then uploads their company logo how do I associate it to that company?

Add an extra line to the form asking for the company name and if you are using a database add the image location to the company name; as I pressume the company will have an address or something. Otherwise you could rename the image to the name of the company; although this could cause problems later.

What I need to do is to add the link that is created to a column under the company. Haven’t got a clue how to do that though!

This is where you want to start getting organised before you go to far.
Do you have a form to create the company details? If so the image upload form can be added to that so all the details are added in one go. If you start doing it with two different forms things start to get messy. If the company details are already in the database you need to have some way to link this image to the company and so you will need the company name or ID to add to the form from the database…

Hi,

I am building up together. So the form is new with both membership registration and image upload.

Heres a nice Upload class I wrote which makes it nice and easy to handle uploads.

<?php

/*

Simply Websites UK

Upload Class

This makes uploading files much easier and handles security issues too.

$upload = new Upload($_FILE[uploaded_file]);

if($upload->isSafeFile()){

$upload-&gt;process();



echo $upload-&gt;filename;

}

*/

class Upload {

private $file;



public $error;



public $image_extensions = array("gif","jpg","png","bmp","jpeg");



public $safe_extensions = array("gif","jpg","jpeg","png","bmp","zip","doc","txt","pdf","xls","exe","wmv","avi","rar","tar");



public $filename;



public function __construct($file){

	$this-&gt;file = $file;

}



public function getFileExtension(){

	return strtolower(substr(strrchr($this-&gt;file[name],'.'),1));

}



public function isImage(){

	if( in_array($this-&gt;getFileExtension(),$this-&gt;image_extensions) ) return true;

}



public function isSafeFile(){

	if( in_array($this-&gt;getFileExtension(),$this-&gt;safe_extensions) ) return true;

}



public function process($dir=DIR_UPLOAD){



	if(!$this-&gt;filename){

		if(file_exists($dir.$this-&gt;file[name])){

			$this-&gt;filename = date('d-m-y_h-i-s',time()).'_'.$this-&gt;file[name];

		}

		else

		{

			$this-&gt;filename = $this-&gt;file[name];

		}

	}



	$move = move_uploaded_file($this-&gt;file[tmp_name],$dir.$this-&gt;filename);

	

	if($move){

		$log = new Log('upload');

		$log-&gt;write('File Uploaded: '.$this-&gt;filename);

	

		return true;

	}

	else

	{

		return false;

		$this-&gt;error = 'Error Uploading File';

	}





}



public function getSize(){

	if($this-&gt;file){

		return $this-&gt;file['size'];

	}

}



public function getOriginalFilename(){

	return $this-&gt;file['name'];

}

}

?>

I just realised there are some lines in this that record logs. You can just remove these.

You will pressumably have a form already so if you are going to use the code from MarPlo I would put your current form code into the form part of his code.
You will then need to put your database code into the part of code starting if(isset($_FILES[‘fileup’])) { - Note the data from the form will only be uploaded if there is an image and it is a valid file type.
Add the $uploadpath variable to your database insert code and it should work.

There are some things I would change but it should get you started - Note the images will be saved into a folder called uploads that you will need to create and CHMOD to 755 or 777 depending on your server setup.