Upload Something?? Anything??

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
gilliepit
Forum Newbie
Posts: 9
Joined: Sat Oct 12, 2002 12:52 pm
Location: usa.sc.cola
Contact:

Upload Something?? Anything??

Post by gilliepit »

I have been poking around for what seems to be forever trying to find a tutorial on how to upload a file using a very, very simple form..

something like:

Code: Select all

<form action="upload_process.php" method="post" enctype="multipart/form-data"> 
<input type="file" name="file">
<input type="submit" value="upload">
</form>
How freakin simple can you get, right? Right. Though in all it's simplicity, I cannot find a tutorial that just tells me how to drop whatever file I've selected into a folder.

Yes, there are complex tutorials out there, but it doesn't matter what file type, how large, of it nothing has been selected- I'd just like to know how to upload something. :D

Does anyone have a suggestion on where to find a tutorial? Or have a snippet of code that could be of some use to me? I've become very frustrated and would greatly appreciate any help I could get.. thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

as usual the manual provide the simple examples ;)
http://www.php.net/manual/en/features.file-upload.php
gilliepit
Forum Newbie
Posts: 9
Joined: Sat Oct 12, 2002 12:52 pm
Location: usa.sc.cola
Contact:

Post by gilliepit »

That's very helpful - it explains what to do.. gives me a lot to work with.. however, I cannot get the code to work..

in the form file I have:

Code: Select all

<form enctype="multipart/form-data" action="process.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
File: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
and in the process.php file I have:

Code: Select all

<?php 
	if (is_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name'])) &#123;
		copy($_FILES&#1111;'userfile']&#1111;'tmp_name'], "/uploads");
		echo "Complete";
	&#125; else &#123;
		echo "Possible file upload attack. Filename: " . $_FILES&#1111;'userfile']&#1111;'name'];
	&#125;
?>
It runs and echo's "Complete", but it doesn't actually store the file in the directory "uploads" is there something I'm missing while manipulating this snippet?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

you need to specify a filename, not just a location, in your copy statement. try this:

Code: Select all

copy($_FILES&#1111;'userfile']&#1111;'tmp_name'], "/uploads/&#123;$_FILES&#1111;'userfile']&#1111;'name']");
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$target_dir = '/uploads';
if (is_dir($target_dir) AND is_writable($target_dir))
   echo (copy($_FILES&#1111;'userfile']&#1111;'tmp_name'], $target_dir)) ? 'Complete' : 'copy failed';
else
   echo $target_dir. ': either invalid or write permission denied';
note that your file will have a name like php3465, the original name is stored in $_FILES['userfile']['name']

also take a look at move_uploaded_file
gilliepit
Forum Newbie
Posts: 9
Joined: Sat Oct 12, 2002 12:52 pm
Location: usa.sc.cola
Contact:

Post by gilliepit »

Both of you! Thanks for your help! I sorta hacked and mixed your code a bit so that it would run, and this is what I came up with.. if you have any further suggestions, please let me know!

Code: Select all

$target_dir = 'uploads';
  if (is_dir($target_dir) AND is_writable($target_dir)) 
  {
    $filename = $_FILES&#1111;'userfile']&#1111;'name'];		
    copy($_FILES&#1111;'userfile']&#1111;'tmp_name'], "$target_dir/$filename"); 
  }
  else
  {
    print("upload failed");
  }
User avatar
gijs
Forum Commoner
Posts: 53
Joined: Wed Aug 28, 2002 4:05 am
Location: Belgium

You might wanna check this as well

Post by gijs »

Hi,

you might wanna check this article as well:
http://www.php4.com/forums/viewtopic.php?t=6

HIH,

Gijs
Post Reply