Page 1 of 1

Upload Something?? Anything??

Posted: Fri Oct 18, 2002 4:03 pm
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!

Posted: Fri Oct 18, 2002 4:24 pm
by volka
as usual the manual provide the simple examples ;)
http://www.php.net/manual/en/features.file-upload.php

Posted: Fri Oct 18, 2002 4:54 pm
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?

Posted: Fri Oct 18, 2002 5:14 pm
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']");

Posted: Fri Oct 18, 2002 5:14 pm
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

Posted: Fri Oct 18, 2002 6:56 pm
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");
  }

You might wanna check this as well

Posted: Wed Oct 23, 2002 12:01 am
by gijs
Hi,

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

HIH,

Gijs