Click to See Complete Forum and Search --> : Upload form
yonibensimon
05-28-2003, 08:10 AM
I have a site and i want users to have the option to upload files to my server. You know, like a text field, a Browse button and a Submit button. Now I know this must be done in the server side, but HOW exactly do i do this ??
Try something like this:
<html>
<head>
<title>Upload a File</title>
</head>
<body>
<?PHP
$uploaddir = "uploads"; # must be chmoded to 777
if ($_POST['submit'])
{
$name = $_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
if (copy ($tmpname, "$uploaddir/$name"))
{
print ("File Name: $name<br/><br/>\n");
print ("File Size: $size<br/><br/>\n");
print ("Your file was successfully uploaded!<br/><br/>\n");
}
else
{
print ("Your file could not be uploaded.");
}
unlink ($tmpname);
}
?>
Upload a file to the server
<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="File" size="20"><br/>
<input type="submit" name="submit" value="Upload File"></form>
</body>
</html>
AdamBrill
05-28-2003, 10:19 AM
In order for that code to work, make sure you name the file upload.php ;)
Yes, and you'll also have to be running PHP 4.1.0 (due to the autoglobal $_FILES), so you might as well write it like this. (named upload.php, again... ;))
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Upload a File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?PHP
$uploaddir = "uploads/"; # must be chmoded to 777
if (isset($_POST['submit'])) {
$name = $_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
if (move_uploaded_file($tmpname, $uploaddir . $name)) {
print ("<p>File Name: $name</p>\n");
print ("<p>File Size: $size</p>\n");
print ("<p>Your file was successfully uploaded!</p>\n");
}
else {
print ("<p>Your file could not be uploaded.</p>");
}
}
?>
<p>Upload a file to the server</p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>File: <input type="file" name="File" size="20"><br/>
<input type="submit" name="submit" value="Upload File"></p></form>
</body>
</html>
This can be re-written to work in eariler versions, if necessary...
bibistroc
04-20-2007, 03:04 AM
But can you make a code that allow only *.jpg files,
and files that are less than 9000kb?
Suhas Dhoke
04-20-2007, 05:03 AM
Code for Image.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Upload a File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?PHP
$uploaddir = "uploads/"; # must be chmoded to 777
if (isset($_POST['submit'])) {
$name = $_FILES['File']['name'];
$tmpname = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
// If the user uploads not a valid image, then display an error msg.
$imgSize = @getImageSize($tmpname);
if (empty($imgSize)) {
print ("<p>Please upload a valid image.</p>\n");
} else {
/*** Get the mime type of the image and check, manager entered the valid image or not. ***/
$imageType = explode("/",$imgSize['mime']);
$imageType = trim(strtolower($imageType[1]));
$validImageTypes = array("jpg", "jpeg", "png", "gif"); // you can also use the same code for checking other type of files like, PDF.
if (!in_array($imageType, $validImageTypes)) {
print("<p>Please upload a valid image for cover.</p>\n");
} else {
if (move_uploaded_file($tmpname, $uploaddir . $name)) {
print ("<p>File Name: $name</p>\n");
print ("<p>File Size: $size</p>\n");
print ("<p>Your file was successfully uploaded!</p>\n");
} else {
print ("<p>Your file could not be uploaded.</p>");
}
}
}
}
?>
<p>Upload a file to the server</p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>File: <input type="file" name="File" size="20"><br/>
<input type="submit" name="submit" value="Upload File"></p></form>
</body>
</html>
elisionista
05-07-2008, 11:15 PM
These forms above works perfectly, I dont know anything about php but I was looking for something like this for some time. The only problem is that this form only works well whit files that has less then 3 MB and I needed one that can upload until 9 GB. :eek: Can you help me whit that?
I tank you in advance for the time dispended in reading this message, and hoppe to ear about you guys soon.
(obs): I'm Portuguese so I'm sorry about my rusty Inglish writing. ;)
Sheldon
05-07-2008, 11:35 PM
9 gig ? you will have to change options in your PHP.INI to allow for files that big.
elisionista
05-08-2008, 12:15 PM
9 gig ? you will have to change options in your PHP.INI to allow for files that big.
Like I said previosly, I dont know how to program PHP, I just copy the form above and created an upload directory and it work, but only with files that has less than 2MB. Can you please help me with the ini.php?
Tank you, and sorry for being anoyng.
:p