File Upload

How to upload files using PHP?

Questions by rubin2008   answers by rubin2008

Showing Answers 1 - 12 of 12 Answers

prasantk

  • Aug 11th, 2008
 

Consider the html form:

<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" size="20" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Script for saving the uploaded file://upload_file.php
<?php
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
?>

The script above checks if the file already exists, if it does not, it copies the file to the specified folder.This example saves the file to a new folder called "upload"

<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

By using the global PHP $_FILES array you can upload files from a client computer to the remote server.

The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:

  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload

dev_002

  • Oct 14th, 2008
 

You can upload file via HTML form by connecting PHP file with it.

codes on HTML side

<form action="uploadmanager.html" enctype="multipart/form-data" method="post">
Name:<br /> <input type="text" name="name" value="" /><br />
Email:<br /> <input type="text" name="email" value="" /><br />
Class notes:<br /> <input type="file" name="homework" value="" /><br />
<p><input type="submit" name="submit" value="Submit Homework" /></p>
</form


codes on PHP file

<?php
/* Set a constant */
define ("FILEREPOSITORY","/home/www/htdocs/class/classnotes/");
/* Make sure that the file was POSTed. */
if (is_uploaded_file($_FILES['classnotes']['tmp_name'])) {
/* Was the file a PDF? */
if ($_FILES['classnotes']['type'] != "application/pdf") {
echo "<p>Class notes must be uploaded in PDF format.</p>";
} else {
/* move uploaded file to final destination. */
$name = $_POST['name'];
$result = move_uploaded_file($_FILES['classnotes']['tmp_name'],
FILEREPOSITORY."/$name.pdf");
if ($result == 1) echo "<p>File successfully uploaded.</p>";
else echo "<p>There was a problem uploading the file.</p>";
} #endIF
} #endIF
?>

Raghavaiah

  • Nov 6th, 2008
 

<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" size="20" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Script for saving the uploaded file://upload_file.php
<?php
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
?>

The script above checks if the file already exists, if it does not, it copies the file to the specified folder.This example saves the file to a new folder called "upload"

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions