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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.