Reputation: 1516
These are the codes i use for a file upload. Everything works fine in Xammp windows. But its not working under centos server. It throws the error "Invalid file".
upload.php
<?php
$allowedExts = array("json");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/json"))
&& ($_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("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$dir="uploads";
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
unlink("uploads/$entry");
}
}
closedir($handle);
}
move_uploaded_file($_FILES["file"]["tmp_name"],
$f="uploads/" . $_FILES["file"]["name"]);
chmod($f, 0777);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
header("Location: user.php");
}
}
}
else
{
echo "Invalid file";
}
?>
HTML
<form action="upload.php" method="POST" enctype="multipart/form-data">
<label for="file"><span style="color:#ffffff;">Upload File:</span></label>
<input type="file" name="file" id="file">
<input class="btn btn-success btn-block" type="submit" name="submit" value="Submit">
The uploaded file is a Json file and its file size is 1.02kb.
Someone please help me solve this.
Upvotes: 1
Views: 4068
Reputation: 54212
If the program flow goes into "Invalid File" case, it means either:
Assume that you choose a valid file to upload
$_FILES["file"]["type"]
is not application/json
$_FILES["file"]["size"]
>= 20000 bytes$extension
is not json
To debug,
$_FILES["file"]["type"]
$_FILES["file"]["size"]
$extension
Currently there is not enough details for further diagnose your issue.
Upvotes: 2
Reputation: 456
Use this it will work,
Replace below line,
move_uploaded_file($_FILES["file"]["tmp_name"], $f="uploads/" . $_FILES["file"]["name"]);
with the below line,
move_uploaded_file($_FILES["file"]["name"], "uploads/" . $_FILES["file"]["name"]);
And as well as check for your folder permissions, or any further doubts make a look at this,
http://www.projectpier.org/node/285
Upvotes: 3