PHP file upload, file will not move to local directory
I have a problem when moving an uploaded file into a local directory.
When running the following code, the output is always "error uploading file". It seems to always not meet the condition for the 'move_uploaded_media' function and therefore $result is not being set?
Are there any glaring mistakes?
<?php
$page_title = 'Admin | Multimedia Portfolio';
include('includes/admin_header.html');
if(isset($_POST['submitted']))
{
$uploadDir = 'files/';
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file"; // Here is were t开发者_开发知识库he it always gets caught
exit;
}
require_once('mysql_connect.php');
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO files (name, size, type, path ) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysqli_query($dbc, $query) or die('Error, query failed : ' . mysql_error());
mysqli_close($dbc);
echo "<br>Files uploaded<br>";
}
?>
<div id="content-wrap">
<h1>Upload Media</h1>
<div id="content">
<form method="post" action="upload.php" encytype="multipart/form-data">
<fieldset>
<div class="entry">
<label>Which media <span class="highlight">file</span> would you like to upload?</label>
<input type="file" name="userfile" id="userfile" size="30" />
</div>
<fieldset id="button">
<input type="submit" value="Register" />
<input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</fieldset>
</form>
</div>
</div>
<?php
include('includes/admin_footer.html');
?>
Not sure if there is more, but you have encytype
instead of enctype
in your <form>
.
You might also want to perform an is_uploaded_file()
check on the temporary file, just to be sure...
精彩评论