Link to home
Start Free TrialLog in
Avatar of Narusegawa
Narusegawa

asked on

PHP File Upload to Current Folder

I'm trying to upload an image to a folder using php.

I've got a very simple form.

--
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']?>">
   <input type="file" name="userfile"><br/>
   <input type="submit" value="Upload">
</form>
--

I just want to save the file that the user uploads, to a sub-folder under where this script is, called uploaded_images. I'd like to keep the filename the same as what the user enters. If the file already exists, then append a timestamp or something to the filename, or even a sessionid to keep it unique.

I'm running PHP5 on Apache2 on Windows XP.

This is only so that some of my friends can upload files to me from various locations where they have limited internet access. I'll worry about security and stuff later on.

Thanks if anyone can help. And if anyone gives complete code in one full page I'll double the points.
ASKER CERTIFIED SOLUTION
Avatar of gruntar
gruntar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Narusegawa
Narusegawa

ASKER

Excellent, works a treat. Thanks very much!

----------------------------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE></TITLE>
<META name="description" content="">
<META name="keywords" content="">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<?php

if (isset($action) && !empty($action))
{

$uploaddir = './';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (file_exists($uploadfile)) $uploadfile = $uploaddir . time() . "_" . basename($_FILES['userfile']['name']);


echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

}

?>




<form method="post" enctype="multipart/form-data" action="<?php echo($_SERVER['PHP_SELF']); ?>">
   <input type="hidden" name="action" value="upload">
   <input type="file" name="userfile"><br/>
   <input type="submit" value="Upload">
</form>

</BODY>
</HTML>

----------------------------------------------------------------------------------------------------

If you want to make a file size limit that gets checked both by the browser and by php add the hidden form field <input type="hidden" name="MAX_FILE_SIZE" value="200000">.


Regards,
Kevin