Jump to content

File Upload?


DJTim666

Recommended Posts

I coded a file upload system but it erroring out when I upload. If I upload something with the same file name it just uploads it instead of stopping the upload. Or if I leave the name field blank, it says that a file with that name already exsists. Any help is greatly appreciated.

 

Heres the code.

<?php
require_once("up_config.php");
require_once("up_func.php");
require_once("db_config.php");
?>
<html>
<head>
<title>
<?php
echo $upload_title;
?>
</title>
</head>
<body bgcolor="orange">
<?php
if (isset($_POST['submit'])) {
# Do upload stuff.

$fileName = $_FILES['userfile']['name'];
$fileType = $_FILES['userfile']['type'];
$fileSize = $_FILES['userfile']['size'];
$fileTmp = $_FILES['userfile']['tmp_name'];
$fileCat = $_POST['cate'];
$wantName = $_POST['name'];

$upload_dir = "uploads/" . $fileCat . "/";

$fNamePart = explode("/", $fileName);
$fLastOne = count($fNamePart) - 1;
$fLastPart = $fNamePart[$fLastOne];

$lastNameBit = getFileExt($fLastPart);
$finalName = $wantName;

switch ($fileCat) {
Case "Picture":
$cAllowed = $allowed_ext['Picture'];
break;
Case "Video":
$cAllowed = $allowed_ext['Video'];
break;
	}
if (!in_array(getFileExt($fileName), $cAllowed)) {
echo "Invalid file format. You cannot upload this file.";
exit;
	}
if (file_exists($upload_dir . $finalName)) {
echo "A file with that name already exists. Try using a file name like asdgsgg!";
exit;
}
if ($fileSize > getMaxSize($fileCat)) {
echo "Your file is too big. It shouldn't get over " . getMaxSize($fileCat) . " bytes";
exit;
	}

if (move_uploaded_file($fileTmp, $upload_dir . $finalName . "." . getFileExt($fileName))) {
echo "Your file was successfully uploaded.";
echo "<br>You can access it here: ";
echo "<a href='http://trulyours.com/" . $upload_dir . $finalName . "." . getFileExt($fileName) . 

"'>http://trulyours.com/" . $upload_dir . $finalName . "." . getFileExt($fileName) . "</a>";
	}

}
else {
?>
<center>
This is the uploads section. You can upload Videos, or Pictures.
<br />
<br />
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="POST">
File Name: <input type="text" name="name" />
<br />
<br />
Category: <select name="cate">
		<option>Please select...</option>
		<option>Picture</option>
		<option>Video</option>
	</select>
	<br /><br />
File: <input type="file" name="userfile" />
<br />
<input type="submit" name="submit" value="submit" />
<br />
<br />
Maximum filesize for Pictures: 5000 KB
<br>Maximum filesize for Videos: 900 MB
</center>
<?php
}
?>
</body>
</html>

 

--

DJ

Link to comment
https://forums.phpfreaks.com/topic/57471-file-upload/
Share on other sites

<?php
require_once("up_config.php");
require_once("up_func.php");
require_once("db_config.php");
?>
<html>
<head>
<title>
<?php
echo $upload_title;
?>
</title>
</head>
<body bgcolor="orange">
<?php
if (isset($_POST['submit'])) {
# Do upload stuff.

$fileName = $_FILES['userfile']['name'];
$fileType = $_FILES['userfile']['type'];
$fileSize = $_FILES['userfile']['size'];
$fileTmp = $_FILES['userfile']['tmp_name'];
$fileCat = $_POST['cate'];
$wantName = $_POST['name'];

$upload_dir = "uploads/" . $fileCat . "/";

$fNamePart = explode("/", $fileName);
$fLastOne = count($fNamePart) - 1;
$fLastPart = $fNamePart[$fLastOne];

$lastNameBit = getFileExt($fLastPart);
$finalName = $wantName;

switch ($fileCat) {
Case "Picture":
$cAllowed = $allowed_ext['Picture'];
break;
Case "Video":
$cAllowed = $allowed_ext['Video'];
break;
	}
if (!in_array(getFileExt($fileName), $cAllowed)) {
echo "Invalid file format. You cannot upload this file.";
exit;
	}
if (file_exists($upload_dir . $finalName)) {
echo "A file with that name already exists. Try using a file name like asdgsgg!";
exit;
}
if ($fileSize > getMaxSize($fileCat)) {
echo "Your file is too big. It shouldn't get over " . getMaxSize($fileCat) . " bytes";
exit;
	}
        if(!file_exists($upload_dir . $finalName . "." . getFileExt($fileName)) {
            if (move_uploaded_file($fileTmp, $upload_dir . $finalName . "." . getFileExt($fileName))) {
            echo "Your file was successfully uploaded.";
    echo "<br>You can access it here: ";
    echo "<a href='http://trulyours.com/" . $upload_dir . $finalName . "." . getFileExt($fileName) . 

"'>http://trulyours.com/" . $upload_dir . $finalName . "." . getFileExt($fileName) . "</a>";
    }
        } else {
            echo 'File already exists.';
        }
} else {
?>
<center>
This is the uploads section. You can upload Videos, or Pictures.
<br />
<br />
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data" method="POST">
File Name: <input type="text" name="name" />
<br />
<br />
Category: <select name="cate">
		<option>Please select...</option>
		<option>Picture</option>
		<option>Video</option>
	</select>
	<br /><br />
File: <input type="file" name="userfile" />
<br />
<input type="submit" name="submit" value="submit" />
<br />
<br />
Maximum filesize for Pictures: 5000 KB
<br>Maximum filesize for Videos: 900 MB
</center>
<?php
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/57471-file-upload/#findComment-284417
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.