PHP Upload Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xeroinsanity89
Forum Newbie
Posts: 5
Joined: Fri Oct 22, 2004 5:50 pm
Contact:

PHP Upload Script

Post by xeroinsanity89 »

Hello, I have a few questions. I have tried to find some PHP File Upload scripts but I cant get a good one. I have a couple things in mind I really want with it and I dont know too much about PHP.

I have read about these scripts and they dont sound that hard to make by someone who is familer with PHP. What I am trying to do is get a script that will let users upload images in any format, under 1 meg and 1024 x 768. This service will be free to my freinds, and I will have 500 megs of space to go around. I would like it so they have the choice of uploading multiple pictures at a time, like three. After they upload it, it would show them their picture, and give them the link of the picture below. Also when you upload a picture with the same name, have it add a number from 1-99 at the end to bypass the duplication.

I would also just have a link on the home page so all my freinds can see all my other freinds images they posted. And I would allow people to hotlink and use my pictures on other sites of course, just with my web link in the bottem right of the picture.

Since this is free, I would like it to say my link at the bottem right hand side of each picture that is hosted, not big, by easy enough to read (http://www.xero.insanityhost.com/). I would just want the pictures to be uploaded to http://www.xero.insanityhost.com/fileupload/uploads/. Also I would like to be able to put this on my front page, with the upload box, etc. Right now I have this, http://www.xero.insanityhost.com/fileupload/upload.php. It is good but I need the options I said above.

I was wondering if anyone is interested in writing a script like this for me, I would really appreciate it. All my image hosting will be free and I am making no money off this whatsoever, just a way to help some of my freinds out that dont have money to pay for hosting. Thanks in advance. Any advice/tips, I am open too, remember I dont know too much about PHP, but I am trying to learn.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

i kinda doubt anyone is going to do it, but hey, dont hurt to ask :)

ive seen some upload scripts that didnt look to bad, might wanna look at a few more.

if your trying to learn, looking at a completed script is often a great way, tailoring it to your needs.

im very sure people on here would help you mod it. Its just once you enter that whole "do it all for me"
zone, people want money lol.

good luck
xeroinsanity89
Forum Newbie
Posts: 5
Joined: Fri Oct 22, 2004 5:50 pm
Contact:

Post by xeroinsanity89 »

Hahha, well thanks for the info, if it helps, here is the script im using now, which works perfectly, just need the options I said above and I dont know how to edit it, maybe someone could help me edit it? I need it so people can upload more then 1 file at a time, that it would put my link in the bottom right hand side of the picture being hosted, display the picture details i.e. link and I dont need the error message stuff. Do I need to post the fileupload-class.php also? Its kinda long.

Code: Select all

<?php

require("fileupload-class.php");

#--------------------------------#
# Variables
#--------------------------------#

// The path to the directory where you want the 
// uploaded files to be saved. This MUST end with a 
// trailing slash unless you use $path = ""; to 
// upload to the current directory. Whatever directory
// you choose, please chmod 777 that directory.

	$path = "uploads/";

// The name of the file field in your form.

	$upload_file_name = "userfile";

// ACCEPT mode - if you only want to accept
// a certain type of file.
// possible file types that PHP recognizes includes:
//
// OPTIONS INCLUDE:
//  text/plain
//  image/gif
//  image/jpeg
//  image/png
	
	// Accept ONLY gifs's
	#$acceptable_file_types = "image/gifs";
	
	// Accept GIF and JPEG files
	$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";
	
	// Accept ALL files
	#$acceptable_file_types = "";

// If no extension is supplied, and the browser or PHP
// can not figure out what type of file it is, you can
// add a default extension - like ".jpg" or ".txt"

	$default_extension = "";

// MODE: if your are attempting to upload
// a file with the same name as another file in the
// $path directory
//
// OPTIONS:
//   1 = overwrite mode
//   2 = create new with incremental extention
//   3 = do nothing if exists, highest protection

	$mode = 2;
	
	
#--------------------------------#
# PHP
#--------------------------------#
	if (isset($_REQUEST['submitted'])) {
		/* 
			A simpler way of handling the submitted upload form
			might look like this:
			
			$my_uploader = new uploader('en'); // errors in English
	
			$my_uploader->max_filesize(30000);
			$my_uploader->max_image_size(800, 800);
			$my_uploader->upload('userfile', 'image/gif', '.gif');
			$my_uploader->save_file('uploads/', 2);
			
			if ($my_uploader->error) {
				print($my_uploader->error . "<br><br>\n");
			} else {
				print("Thanks for uploading " . $my_uploader->file['name'] . "<br><br>\n");
			}
		*/
			
		// Create a new instance of the class
		$my_uploader = new uploader($_POST['language']); // for error messages in french, try: uploader('fr');
		
		// OPTIONAL: set the max filesize of uploadable files in bytes
		$my_uploader->max_filesize(5242880);
		
		// OPTIONAL: if you're uploading images, you can set the max pixel dimensions 
		$my_uploader->max_image_size(2000, 2000); // max_image_size($width, $height)
		
		// UPLOAD the file
		if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) {
			$my_uploader->save_file($path, $mode);
		}
		
		if ($my_uploader->error) {
			echo $my_uploader->error . "<br><br>\n";
		
		} else {
			// Successful upload!
			print($my_uploader->file['name'] . " was successfully uploaded! <a href="" . $_SERVER['PHP_SELF'] . "">Try Again</a><br>");
			
			// Print all the array details...
			//print_r($my_uploader->file);
			
			// ...or print the file
			if(stristr($my_uploader->file['type'], "image")) {
				echo "<img src="" . $path . $my_uploader->file['name'] . "" border="0" alt="">";
			} else {
				$fp = fopen($path . $my_uploader->file['name'], "r");
				while(!feof($fp)) {
					$line = fgets($fp, 255);
					echo $line;
				}
				if ($fp) { fclose($fp); }
			}
 		}
 	}




#--------------------------------#
# HTML FORM
#--------------------------------#
?>
	<form enctype="multipart/form-data" action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
	<input type="hidden" name="submitted" value="true">
		
		Upload this file:<br>
		<input name="<?= $upload_file_name; ?>" type="file">
		<br><br>
		
		Error Messages:<br>
		<select name="language">
			<option value="en">English</option>
			<option value="fr">French</option>
			<option value="de">German</option>
			<option value="nl">Dutch</option>
			<option value="it">Italian</option>
			<option value="fi">Finnish</option>
			<option value="es">Spanish</option>
			<option value="no">Norwegian</option>
			<option value="da">Danish</option>
		</select>
		<br><br>
		
		<input type="submit" value="Upload File">
	</form>
	<hr>

<?php
	if (isset($acceptable_file_types) && trim($acceptable_file_types)) {
		print("This form only accepts <b>" . str_replace("|", " or ", $acceptable_file_types) . "</b> files\n");
	}
?>
xeroinsanity89
Forum Newbie
Posts: 5
Joined: Fri Oct 22, 2004 5:50 pm
Contact:

Post by xeroinsanity89 »

well i played around with the settings and get everything perfect, except i still cant find out how to put my link in the bottem right hand side of each picture, any help?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Guess you worked out what you need by the look of your other post.
Rosco404
Forum Newbie
Posts: 2
Joined: Tue Oct 26, 2004 4:35 pm

Post by Rosco404 »

I have this exact upload script! I have been searching all round the net for a solution to my problem.....

Can this script be edited so the user can have a drop down list of directorys so they can choose were they upload to on my server?

It would save me a lot of sorting of images if the user can upload an image to the folder relvent to the image they are uploading!

Hope someone can help : )
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Sure you can. Add a drop down box to the form and then have that value overwrite the $path variable in the script. Do some checking before you let them upload of course - dont want them uploading to any old folder.
Rosco404
Forum Newbie
Posts: 2
Joined: Tue Oct 26, 2004 4:35 pm

Post by Rosco404 »

Thanks kettle_drum, i thought about that the problem is... i just dont know how to incorparate it?

Thanks for your help m8 :D
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post by myleow »

I think when you want to include your company name in the image as a form of free advertising, you need to include the byte code that represent the company name when you are saving the image into the file/db
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Post Reply