I am unable to upload any file to my website.
Is my code wrong? In addition I wanted to know the php code for setting the maximum upload size for the files I wanted to upload to a Server. Also how I am supposed to change my file upload maximum size in my XAMPP, I am using version 1.7.3.
This is my code:
<?php
//Load the settings
require_once("Setting.php");
require_once("db.php");
$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{
$_FILES['file']['tmp_name'];
$target_path = Setting::$uploadFolder;
$target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']);
echo $target_path;
//Try to move the uploaded file into the designated folder
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
{
$message = "The file ". basename( $_FILES['file']['name']). " has been uploaded";
$query ="insert into upload (path) values ('$target_path')";
$dbresult = mysql_query($query,$dblink);
} else{
$message = "There was an error uploading the file, please try again!";
}
}
//Clear the array
unset($_FILES['file']);
if(strlen($message) > 0)
{
$message = '<p class="error">' . $message . '</p>';
}
/** LIST UPLOADED FILES **/
$uploaded_files = "";
//Open directory for reading
$dh = opendir(Setting::$uploadFolder);
//LOOP through the files
while (($file = readdir($dh)) !== false)
{
if($file != '.' && $file != '..')
{
$filename = Setting::$uploadFolder . $file;
$parts = explode("_", $file);
$size = formatBytes(filesize($filename));
$added = date("m/d/Y", $parts[0]);
$origName = $parts[1];
$filetype = getFileType(substr($file, strlen($file) - 3));
$uploaded_files .= "<li class=\"$filetype\"><a href=\"$filename\">$origName</a> $size - $added</li>
";
}
}
closedir($dh);
if(strlen($uploaded_files) == 0)
{
$uploaded_files = "<li><em>No files found</em></li>";
}
function getFileType($extension)
{
$images = array('JPG', 'GIF', 'PNG', 'BMP');
$docs = array('TXT', 'RTF', 'DOC');
$apps = array('ZIP', 'RAR', 'EXE');
if(in_array($extension, $images)) return "Images";
if(in_array($extension, $docs)) return "Documents";
if(in_array($extension, $apps)) return "Applications";
return "";
}
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 1024) / log(1024));
$pow = min($pow, count($units) - 1); `enter code here`
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
?>