How can you upload a file?
Thanks,
~someonewhois
| SitePoint Sponsor |
How can you upload a file?
Thanks,
~someonewhois
- Nathan
simple question; simple answer: www.php.net/manual/en/features.file-upload.php
![]()
![]()
- Matt
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
"Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR
I didn't understand that post vars thing... Any help on it?
- Nathan
here's a sample file that should work for you. it should save the uploaded file in the same directory as the script (make sure it's writable by PHP), with the same name as it had when it was uploaded.
if your version of PHP is less than 4.1.0, change $_FILES to $HTTP_POST_FILES and $_SERVER to $HTTP_SERVER_VARS.PHP Code:<?php
if (isset($_FILES['test']))
{
if ($_FILES['test']['tmp_name'] == '' || $_FILES['test']['tmp_name'] == 'none')
{
die('No file selected.');
}
if (move_uploaded_file($_FILES['test']['tmp_name'], "./{$_FILES[test][name]}"))
{
echo 'File successfully saved to: ', dirname($_SERVER['SCRIPT_FILENAME']), "/{$_FILES[test][name]}";
}
else
{
echo 'File upload failed!';
}
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
Select a file to upload: <input type="file" name="test" /><br />
<input type="submit" value="Submit" />
</form>
<?php
}
?>
Thank you, I'll try this!![]()
- Nathan
IT WORKED!
Thank you very much,
~someonewhois
- Nathan
If I were to make something to display these, would I make a mysql table to store the filename?
What would I store? The full path? What variable?
And hwo could I make it go into a different folder?
Would I change the upload and move path?
I want it so that uploader.php upload to /uploads/ but it's at main root.
Thanks,
~someonewhois
- Nathan
Thank you for your help, please continue!
(This is equivilent to *bump*)
Thanks,
~someonewhois
- Nathan
Thats pretty easy, thankfully!
Find this line:
specifically,PHP Code:if (move_uploaded_file($_FILES['test']['tmp_name'], "./{$_FILES[test][name]}"))
more specifically:PHP Code:move_uploaded_file($_FILES['test']['tmp_name'], "./{$_FILES[test][name]}"
now you see the "./"? that says "current directory". If you wish to change it to uploads, just add uploads/!PHP Code:"./{$_FILES[test][name]}"
full:PHP Code:"./uploads/{$_FILES[test][name]}"
hope that helps with thatPHP Code:if (move_uploaded_file($_FILES['test']['tmp_name'], "./uploads/{$_FILES[test][name]}"))
Now, when you are talking about listing all the files in a directory? Well, theirs a simplier way to do it without using mysql...
that will simply list every file in a directory.PHP Code:<?php
if ( $dir = @opendir("./uploads") )
{
while ( ($file = readdir($dir)) !== false )
{
if ( $file != "." && $file != ".." )
{
echo $file . "<br />\n";
}
}
closedir($dir);
}
else
{
echo "Could not open directory!";
}
?>
Last edited by z00om; Apr 30, 2002 at 19:07.
Awesome, thank you very much!
Thanks,
~someonewhois
- Nathan
Bookmarks