Jump to content

checking if user has already uploaded a file


Smudly

Recommended Posts

I've got a page that allows users to upload a file (pdf, jpg, gif png). The user must be logged in in order to upload something. I have a query that checks if the user has already uploaded a file with the same name as the name of the file they are trying to upload.

If they have not uploaded the file yet, the file uploads and they get a "Success" message. If the file has already been uploaded by the user, they will get the message, "You have already uploaded that file".

When the query goes through, the message that shows is, "You have already uploaded that file". I ensured the file was not already in the database, and it still shows this error. I tried changing the if statement to say:

 

if ($duplicate==0)

 

instead of:

 

if ($duplicate!=0)

 

but it always shows the same error. Any ideas of what could be wrong with my code?

 

my sql table looks like:

 

Field  Type  Null 

id int(11) No   

userid int(11) No   

artist varchar(50) No   

title varchar(50) No   

file varchar(2083) No   

uploaded varchar(3) No   

 

Code:

 

<?php
session_start();

if (isset($_SESSION['username'])){

$username = $_SESSION['username'];
$submit = $_POST['submit'];

include_once('inc/connect.php');

$uploadsql = mysql_query("SELECT * FROM `users` WHERE `username`='$username'"); 
$uploadrow = mysql_fetch_assoc($uploadsql); 

$userid = $uploadrow['id']; 
$folder = "sheets/fromusers/";

if (isset($submit)){

// Name of file
$name = $_FILES["location"]["name"];
// Type of file (video/avi) or image/jpg, etc
$type = $_FILES["location"]["type"];
//size of file
$size = $_FILES["location"]["size"];
//stores file in a temporary location
$temp = $_FILES["location"]["tmp_name"];
// if there is an error
$error = $_FILES["location"]["error"];

$artist = strtolower($_POST['artist']);
$title = strtolower($_POST['title']);

// Check if fields are filled in
if($artist&&$title){

if ($error > 0)
{
$sheeterror = "<div id='messageerror'>An error occured. Please try again.</div>";
}
else
{

// Determine the extension of the file
// If file is This File.pdf
// Then $ext is now equal to pdf

$ext = strtolower(substr($name, strrpos($name, '.') + 1)); 

if ($ext=="pdf" || $ext=="gif" || $ext=="jpeg" || $ext=="jpg" || $ext=="png")
{
if ($size <= 26214400) // If size <= 25 megabytes
{
		$duplicatecheck = mysql_query("SELECT file FROM upload WHERE id='$userid'");
		$duplicate = mysql_num_rows($duplicatecheck);

		if ($duplicate!=0){
			$sheeterror = "<div id='messageerror'>You have already uploaded this file!</div>";

			}
			else{
				$sheetquery = mysql_query("INSERT INTO upload VALUES ('','$userid','$artist','$title','$name','no')");

		move_uploaded_file($temp, $folder.$name);

		$success = "<div id='messagesuccess'>Upload Complete!</div><div align='center'>".ucwords($artist)." - ".ucwords($title)."</div>";
			}

	}
else{
	$sheeterror = "<div id='messageerror'>Your sheet must be less than 25 megabytes.</div>";
}
}
else
{
$sheeterror = "<div id='messageerror'>".ucfirst($ext)." files are not allowed!</div>";

}	
}
}
else{
$sheeterror = "<div id='messageerror'>Fill In All Fields</div>";
}
}
}
else{
$sheeterror = "<div id='messageerror'>You must be logged in to add sheets!</div>";
}

?>

<html>
<head>
<title>Add Sheet</title>
<style>
#container{
width: 350px;
height: 150px;
margin-left: auto;
margin-right: auto; 
background-color: #cccccc;
}
  
#formhold{
width: 300px;
text-align: right;
margin-right: auto; 
}
#messagesuccess{
background-color: #66CD00; 
width: 350px; 
margin-left: auto; 
margin-right: auto;
}
  #messageerror{
background-color: #ff2211; 
width: 350px; 
margin-left: auto; 
margin-right: auto;
}
</style>
</head>
<body OnLoad="document.newsheet.artist.focus();">
<?php
include_once('inc/nav.php');
?>
<center>
<h1>Add Sheet</h1>
<br />
<div id="container">
<br />
<div id="formhold">
<form action="addsheet.php" method="post" name="newsheet" enctype="multipart/form-data">
Artist: <input type="text" name="artist" size="30"><br />
Title: <input type="text" name="title" size="30"><br />
Sheet: <input type="file" name="location" size="17"><br />
</div>
<center><input type="submit" name="submit" value="Submit"></center>
</form>


</div>
<div id="bottomcont">
<?php echo $success, $sheeterror; ?>
</div>
</center>
</body>
</html>

 

 

 

Link to comment
Share on other sites

Your WHERE clause says to check the ID of the upload record, and not the ID of the user:, based on your tables that is.

 

$duplicatecheck = mysql_query("SELECT file FROM upload WHERE id='$userid'");

 

So:

 

$duplicatecheck = mysql_query("SELECT file FROM upload WHERE userid='$userid'");

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.