GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Programming  >  PHP
Go To First  |  Previous Question  |  Next Question 
 PHP  |  Question 59 of 62    Print  
File Upload
How to upload files using PHP?


  
Total Answers and Comments: 4 Last Update: November 06, 2008     Asked by: rubin2008 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: prasantk
 
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"

Above answer was rated as good by the following members:
gurukatre
August 11, 2008 14:59:02   #1  
prasantk Member Since: August 2008   Contribution: 1    

RE: File Upload
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"

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 19, 2008 04:54:38   #2  
kambletrupti Member Since: September 2008   Contribution: 1    

RE: File 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


 
Is this answer useful? Yes | No
October 14, 2008 19:10:24   #3  
dev_002 Member Since: October 2008   Contribution: 1    

RE: File Upload
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
?>

 
Is this answer useful? Yes | No
November 06, 2008 01:11:54   #4  
Raghavaiah Member Since: November 2008   Contribution: 1    

RE: File Upload
<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"

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape