![]() |
|
How to make a File Uploader in PHP - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: How to make a File Uploader in PHP (/Thread-How-to-make-a-File-Uploader-in-PHP) |
How to make a File Uploader in PHP - Cake - 08-12-2013 Create an Upload-File Form To allow users to upload files from a form can be very useful. Look at the following HTML form for uploading files: Code: <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>Notice the following about the HTML form above: The enctype attribute of the <form> tag specifies which content-type to use when submitting the form. "multipart/form-data" is used when a form requires binary data, like the contents of a file, to be uploaded The type="file" attribute of the <input> tag specifies that the input should be processed as a file. For example, when viewed in a browser, there will be a browse-button next to the input field Note: Allowing users to upload files is a big security risk. Only permit trusted users to perform file uploads. Create The Upload Script The "upload_file.php" file contains the code for uploading a file: Code: <?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"];
}
?>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 This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload. Restrictions on Upload In this script we add some restrictions to the file upload. The user may upload .gif, .jpeg, and .png files; and the file size must be under 20 kB: Code: <?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
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"];
}
}
else
{
echo "Invalid file";
}
?>Saving the Uploaded File The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server. The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location: Code: <?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
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"];
}
}
}
else
{
echo "Invalid file";
}
?>
//Source=w3schools.org fgtsThe script above checks if the file already exists, if it does not, it copies the file to a folder called "upload". RE: How to make a File Uploader in PHP - Crimin4L - 08-12-2013 Hmm, very nice and clean code. Did you make this or just copy this from somewhere? RE: How to make a File Uploader in PHP - Cake - 08-12-2013 (08-12-2013, 07:23 AM)Bane__ Wrote: Hmm, very nice and clean code. Did you make this or just copy this from somewhere?I've known how to do this but I cited my source. RE: How to make a File Uploader in PHP - devit - 08-14-2013 Quite nice tutorial actually, thanks for this, I might try it out tomorow
RE: How to make a File Uploader in PHP - 0x4b33 - 08-19-2013 I would strongly advise you not to "cite" a source, incase you knew of the content you could atleast take your time and type it up yourself. RE: How to make a File Uploader in PHP - Cake - 08-19-2013 (08-19-2013, 05:27 PM)0x4b33 Wrote: I would strongly advise you not to "cite" a source, incase you knew of the content you could atleast take your time and type it up yourself.I would cite a source because I'm too lazy to type the entire thing up and this was just so people on dz could make a file uploader if interested. RE: How to make a File Uploader in PHP - 0x4b33 - 08-19-2013 (08-19-2013, 05:30 PM)Skizzy Wrote:(08-19-2013, 05:27 PM)0x4b33 Wrote: I would strongly advise you not to "cite" a source, incase you knew of the content you could atleast take your time and type it up yourself.I would cite a source because I'm too lazy to type the entire thing up and this was just so people on dz could make a file uploader if interested. Well, its fine. I would just love if it was actually written by yourself. RE: How to make a File Uploader in PHP - Cake - 08-19-2013 (08-19-2013, 05:38 PM)0x4b33 Wrote:I'll write a tutorial later when my desktop is fixed on how to make a simple antivirus uploader that runs from virus totals api.(08-19-2013, 05:30 PM)Skizzy Wrote:(08-19-2013, 05:27 PM)0x4b33 Wrote: I would strongly advise you not to "cite" a source, incase you knew of the content you could atleast take your time and type it up yourself.I would cite a source because I'm too lazy to type the entire thing up and this was just so people on dz could make a file uploader if interested. RE: How to make a File Uploader in PHP - Sapientia - 08-23-2013 I feel like I've seen this before, but good job anyways. |