PHP uploader

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1586 Views - Last Post: 31 May 2009 - 12:32 PM Rate Topic: -----

#1 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

PHP uploader

Posted 31 May 2009 - 09:35 AM

Hey guys.
I own a website called uplodu (hence the name...)
Basically it's a file sharing website dedicated to making sharing simple.

I've got the upload form in place and it all works, the only problem is - with the code I have the upload opens in a new page and doesn't have any tags such as code for forums, direct url, code for websites etc etc.

I was just wondering if there is any code i could use to add these tags?

As far as my (limited) knowledge goes, to show on the same page requires AJAX, but if that too can be provided it will help.

If any of that explanation was unclear you can go to www.uplodu.com and try the form for yourself.

here's a copy of the php upload file:
<?php
   // Configuration - Your Options
	  $allowed_filetypes = array('.JPG','.jpg','.gif','.bmp','.PNG','.png','.GIF','.tif','.tiff','.ai','.psd','.jpeg','.psp','.bmp','.dxf','.eps','.ps','.svg','.pdf','.aac','.aif','.iff','.m3u','.mid','.midi','.mp3','.mpa','.ra','.ram','.wav','.wma','.3gp','.asf','.asx','.avi','.mov','.mp4','.mpg','.qt','.rm','.swf','.wmv','.asp','.css','.js','.jsp','.xhtml','.exe','.zip','.rar','.7z','.gz','.doc','.txt');
	  $max_filesize = 104857600; // Maximum filesize in BYTES (currently 0.5MB).
	  $upload_path = './upload/'; // The place the files will be uploaded to (currently a 'files' directory).
 
   $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
 
   // Check if the filetype is allowed, if not DIE and inform the user.
   if(!in_array($ext,$allowed_filetypes))
	  die('<font size="3" face="Tahoma" font color="black"><center>Sorry, you cant upload that file type at the moment.</font></a></center>');
 
   // Now check the filesize, if it is too large then DIE and inform the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
	  die('<font size="3" face="Tahoma" font color="black"><center>Sorry, that file is bigger than 100 MB, try a different file.</font></a></center>');
 
   // Check if we can upload to the specified path, if not DIE and inform the user.
   if(!is_writable($upload_path))
	  die('You cannot upload to the specified directory, please CHMOD it to 777.');

   
// Upload the file to your specified path.
$newname = substr(strtolower($filename),0,4).time().$ext;
  if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $newname))
echo '<font size="3" face="Tahoma" font color="black"><center>Congratulations, your upload is ready. View it <a href="' . $upload_path . $newname . '" title="Your File" target="_blank">here</font></a></center>'; // It worked.
	  else
		 echo '<font size="3" face="Tahoma" font color="black"><center>Looks like an error occured, please try again!</font></a></center>'; // It failed :(.
 
?> 


Is This A Good Question/Topic? 0
  • +

Replies To: PHP uploader

#2 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Re: PHP uploader

Posted 31 May 2009 - 09:49 AM

When you say 'code for forums', are you referring to like the embed code people use to place videos in forum posts?
Was This Post Helpful? 0
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 09:55 AM

Nevermind.....I missread (again)

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2009 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:01 AM

View PostAlienWebguy, on 31 May, 2009 - 08:49 AM, said:

When you say 'code for forums', are you referring to like the embed code people use to place videos in forum posts?


Thanks for the quick response - Yes that's what I meant, the BBC code.

Something similar to the following:

Links to share:
HTML for Website
IMG Code for Forums & Messaging Boards
URL for E-Mail and IM

So - Lets say the file was uploaded to www.uplodu.com/upload/image
Links to share:
HTML for Website  -  <a href="http://uplodu.com" target="_blank"><img src="www.uplodu.com/upload/image" border="0" alt="File hosting by www.uplodu.com"></a>


IMG Code for Forums & Messaging Boards - [IMG]http://www.uplodu.com/upload/image[/IMG]


URL for E-Mail and IM - www.uplodu.com/upload/image 



View PostShaneK, on 31 May, 2009 - 08:55 AM, said:

To enable your viewers to be able to view your files you need to give them a method to view it lol.....
I don't really know how to explain that, really, but it seems to me that if you were capable of making the upload script you're surely capable of being able to tell users how to view the file they just uploaded...

Yours,
Shane~


Shane I think you misunderstood what I meant.
The users CAN view the file. One upload is complete the form shows "You're file has been successfully uploaded > View It <"

This in turn is a link to the file location, but it opens in a new window (which I would like to make the uploader show automatically on the same page) and this also doesn't show any embed tags etc.
Was This Post Helpful? 0
  • +
  • -

#5 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:08 AM

I prefer text boxes!
You mean something like what I use on my site?
Example here: (Though it only tells how to link it in one place, it could easily be reproduced for the others)
http://tibialottery.com/view/38824 (Where it says "Link to this lottery~")

Yeah, that's pretty easy. Just a simple text box with what you want in it set to the value, like so:
echo("IMG Code for Forums & Messaging Boards <input type=\"text\" name=\"link\" id=\"link\" value=\"[IMG]http://www.uplodu.com/upload/image[/IMG]\">");



That's just one example, but you should be able to figure out the rest.
Also, it's really simple to add whatever javascript you need or whatever.....like you could add
onclick="document.getElementById('link').select();"
to the link and anyone using javascript gets the entire link highlighted whenever they click on the box...or whatever you wanna do :P

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2009 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#6 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:14 AM

View PostShaneK, on 31 May, 2009 - 09:08 AM, said:

I prefer text boxes!
You mean something like what I use on my site?
Example here: (Though it only tells how to link it in one place, it could easily be reproduced for the others)
http://tibialottery.com/view/38824 (Where it says "Link to this lottery~")

Yeah, that's pretty easy. Just a simple text box with what you want in it set to the value, like so:
echo("IMG Code for Forums & Messaging Boards <input type=\"text\" name=\"link\" id=\"link\" value=\"[IMG]http://www.uplodu.com/upload/image[/IMG]\">");



That's just one example, but you should be able to figure out the rest.
Also, it's really simple to add whatever javascript you need or whatever.....like you could add
onclick="document.getElementById('link').select();"
to the link and anyone using javascript gets the entire link highlighted whenever they click on the box...or whatever you wanna do :P

Yours,
Shane~


Will the IMG value change with each upload though?
If the value between the IMG tags was manually input?

This post has been edited by uplodu: 31 May 2009 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:17 AM

Well, no, it wouldn't :P I was actually just providing an example, but like:
echo("IMG Code for Forums & Messaging Boards <input type=\"text\" name=\"link\" id=\"link\" value=\"[IMG]".$upload_path.$newname."[/IMG]\">");



That should work, assuming you're putting this on the same page as the uploader. (Also assuming the link you give on your uploader page where it says "View it here" works)

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2009 - 10:19 AM

Was This Post Helpful? 1
  • +
  • -

#8 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:22 AM

View PostShaneK, on 31 May, 2009 - 09:17 AM, said:

Well, no, it wouldn't :P I was actually just providing an example, but like:
echo("IMG Code for Forums & Messaging Boards <input type=\"text\" name=\"link\" id=\"link\" value=\"[IMG]".$upload_path.$newname."[/IMG]\">");



That should work, assuming you're putting this on the same page as the uploader. (Also assuming the link you give on your uploader page where it says "View it here" works)

Yours,
Shane~


I shall try this now. I've +1'd to your Thanks. EDIT // It wouldn't let me. But I meant well.

This post has been edited by uplodu: 31 May 2009 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:24 AM

Did it give you any errors? What was wrong?

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#10 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:29 AM

View PostShaneK, on 31 May, 2009 - 09:24 AM, said:

Did it give you any errors? What was wrong?

Yours,
Shane~


All it said was "This user could not be Thanked" My assumption is that it's because I'm new to the forum, and it's to prevent people making accounts in order to add thank to themselves?
Was This Post Helpful? 0
  • +
  • -

#11 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:35 AM

OH! I thought you were talking about the code I provided XD
Lol, nevermind that :P It said you thanked me though 0.o (And my "thanks count" went up)

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2009 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#12 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

Re: PHP uploader

Posted 31 May 2009 - 10:57 AM

View PostShaneK, on 31 May, 2009 - 09:35 AM, said:

OH! I thought you were talking about the code I provided XD
Lol, nevermind that :P It said you thanked me though 0.o (And my "thanks count" went up)

Yours,
Shane~


Must be why - It must have sent the form twice, hence not allowing me to thank you for the same thing.

Also, The code works a charm on the same page as the uploader - but now I just need to find a code that will allow the image to show on the same page,

Otherwise it will look silly having

File uploaded successfully view it here
IMG code for forums: etc etc.
Was This Post Helpful? 0
  • +
  • -

#13 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 11:13 AM

Just put:
echo("<img src=\"".$upload_path.$newname."\" alt=\"Your image!\" title=\"Your image!\">");

Wherever you want the image...

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#14 uplodu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 31-May 09

Re: PHP uploader

Posted 31 May 2009 - 12:05 PM

View PostShaneK, on 31 May, 2009 - 10:13 AM, said:

Just put:
echo("<img src=\"".$upload_path.$newname."\" alt=\"Your image!\" title=\"Your image!\">");

Wherever you want the image...

Yours,
Shane~


Fantastic, That's done the trick.
However, (PHP isn't my strong point as you've probably noticed)

Because the uploader is in an iFrame the image is restricted by that, There there any way i can just use that to generate just a thumbnail - I should have made this clearer.

Also,

with the text box embed codes, it's rejecting the html one because of syntax errors, apparently its not accepting it as a value, but reading it as a code - is there a way around this?

Sorry to be a pain,
_Uplodu
Was This Post Helpful? 0
  • +
  • -

#15 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: PHP uploader

Posted 31 May 2009 - 12:27 PM

About the thumbnail thing, you should probably just add sizing attributes to the image (width=32 height=32) and make the image link to it, like this:
echo("<a href=\"".$upload_path.$newname."\"><img src=\"".$upload_path.$newname."\" alt=\"Your image!\" title=\"Your image!\" width=32 height=32></a>");


32 isn't very big, but if the image is smaller than that then it will get pixelated. There are better methods, but your PHP knowledge isn't that great...

For the HTML code, try this in its place:
$string = htmlentities("<a href=\"http://uplodu.com\" target=\"_blank\"><img src=\"".$upload_path.$newname."\" border=\"0\" alt=\"File hosting by www.uplodu.com\"></a>");
echo("HTML for Website: <input type=\"text\" name=\"link\" value=\"$string\">");


This uses htmlentities on the HTML string, so that in the code of the site it doesn't do anything, but to the user it appears correctly.

Also, I really hope, since your site supports formats that aren't images, that you're making sure whatever you're displaying is an image before displaying it like this. Also, I suggest you don't support .exe files on your uploader.

Yours,
Shane~

This post has been edited by ShaneK: 31 May 2009 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2