I have tried this coding which ive taken from w3schools for file uploading in localhost.
---------------------------------------------------------------------------------
<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>
---------------------------------------------------------------------
upload_file.php
<?phpif ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
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";
}?>
-----------------------------------------------------------------------------------------
Can i test this the above coding in localhost or should this be run only in the server.
This is the output I get after running the above file.What do i conclude from the following output?
0) { echo "Error: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; } } else { echo "Invalid file"; } ?>
----------------------------------------------------------------------------
File upload in PHP from localhost
Moderator: General Moderators
Re: File upload in PHP from localhost
Try this, if it doesn't display an invalid size or type error, and does nothing.
var_dump($_FILES['file']) and check the error code, your php.ini may need the max size increasing.
var_dump($_FILES['file']) and check the error code, your php.ini may need the max size increasing.
Code: Select all
<?php
define("FILE_MAX_SIZE", 20000);
$types = array("image/gif", "image/jpeg", "image/pjpeg");
if(isset($_FILES['file'])) {
if(!in_array($types, $_FILES['file']['type'])) {
die("Error: Invalid File Type\n");
} else if($_FILES['file']['size'] > FILE_MAX_SIZE) {
die("Error: File size too large.\n");
} else {
var_dump($_FILES['file']);
}
} else {
die("Please upload a file");
}
?>