Jump to content

Error in uploading file.


usapphire

Recommended Posts

Hey everyone  :D

Thanks previously for your help!

 

I have a quick problem, which I'm sure is just something very silly.. I made an upload script thanks to PHP Freaks and the person who replied to my previous forum, but after finally finishing it.. there seemed to be an error. Okay, so this is what I did:

 

1. Script worked fine. Integrated into the site layout with no problem.

2. Made "categories" within the site.. So this means that there is more than 1 upload/download page.*

3. Made the MySQL databases to hold data for the other categories.

4. Uploaded the new pages, and CHMOD the upload folder to 777.

5. Tried it out - doesn't work anymore.

 

* The different categories are on different pages. For example; category1.php, category2.php, etc.

 

Here is the code I'm using.

 

Upload form / display download files page.

<?php include("header.php"); ?>
<div align="center"><img src="images/index_category1.jpg" border="0"></div><br>

<?php
$uploadDir = 'category1/upload/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);

// make the random file name
$randName = md5(rand() * time());

// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "There was an error uploading the file. Click the back button and try again.";
exit;
}

include 'category1/config.php';
include 'category1/opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'category1/closedb.php';

echo "<div align=center><font color=red><b>YOUR FILE HAS BEEN UPLOADED!</b><br></div></font>The Direct link is found on the right of your file name. Can't see your file? New uploaded files are placed on the bottom of the list, so scroll down. To copy the link, Right-Click on the <b>DOWNLOAD FILE</b> text, and select <b>Copy Link Location</b> or <b>Copy Shortcut</b>. Thank you for using our services!</font><br>";

}
?>

<script>
function showProgress(){document.getElementById('progressRow').style.display = 'inline';
}
</script>

<div align="center">
<span id="progressRow" style="display:none;"><table border=0 cellpadding=10 cellspacing=0 class=forText width=90%><tr><td valign=middle bgcolor=#ececec><div align=center><b><font color="black"><div align="center">Your file is being uploaded. Please wait <img src="images/dot.gif"></div></b></font></tr></td></table></span></div>

<form method="post" enctype="multipart/form-data" onSubmit="submitonce(this)">
  <table border="0" cellpadding="0" cellspacing="5" class="forText">
    <tr> 
      <td><img src="images/index_52.jpg"></td><td><input name="userfile" type="file" id="userfile" size="35" class="forText">
         </td>
      <td><input name="upload" type="Submit" id="upload" value="Upload" onclick="showProgress();" onclick="this.disabled=true;"></td>
    </tr>
  </table>
<div align="center">
<table border=0 cellpadding=10 cellspacing=0 class=forText width=90%><tr><td valign=middle bgcolor=#ececec><div align=center>-DISCLAIMER-</tr></td></table>
</div>
</form><br><br>

<?php include("category1_download.php"); ?>
<?php include("footer.php"); ?>

 

 

The Download page (script for include)

<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
    include 'category1/config.php';
    include 'category1/opendb.php';

    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $filePath) = mysql_fetch_array($result);

    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    
    readfile($filePath);

    include 'category1/closedb.php';    
    exit;
}

?>

<?php
include 'category1/config.php';
include 'category1/opendb.php';

$query  = "SELECT id, name FROM upload2";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "<div align=center><table border=0 cellpadding=5 cellspacing=0 class=forText width=90%><tr><td valign=middle bgcolor=#ececec><div align=center>There are no files uploaded in this directory. <b>Be the first!</b></tr></td></table></div>";
} 
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <table border=0 cellpadding=0 cellspacing=5 class=forText width=100%><tr><td valign=middle bgcolor=#ececec width=75><div align=center>
<img src=images/category1.jpg></div></td><td valign=middle bgcolor=#ececec><div align=center><?=$name;?></div></td><td valign=middle bgcolor=#ececec width=100><div align=center><a href="category1_download.php?id=<?=$id;?>">DOWNLOAD FILE</a></div></tr></td></table>
<?php        
    }
}
include 'category1/closedb.php';
?>

 

The error that comes up is "There was an error uploading the file. Click the back button and try again."  ???

Link to comment
Share on other sites

$ext = substr(strrchr($fileName, "."), 1);

Shouldn't this be more like

$ext = substr($fileName, strrpos($fileName, "."));

Changed, and it didn't seem to make a difference.. However, I found something that made the error.

 

<script>
function submitonce(theform){
if (document.all||document.getElementById){
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
tempobj.disabled=true
}
}
}
</script>

This code is located in the header.php file between <HEAD></HEAD>. It's to prevent people from clicking "Upload" (submit button) more than once.. If I take it off, it works - but now I'm left with the Upload button still able to be clicked. Does anyone have another code that may allow it to work? Or know how I can change this one so it can work?

 

Thank you ^_^

 

EDIT: Just a note, it has actually worked in the past with the code!! Thanks again.

 

 

Link to comment
Share on other sites

Why does it matter if they click it more than once?

It's an upload site.. if they click it more than once, it'll restart the upload? I allowed files up to 100mb to be uploaded, so I don't think people who want to upload a file 100mb want to keep clicking it if they think it'll make it go fast..  :-\

Link to comment
Share on other sites

just add like: document.form_name.submit somewhere, i am pretty sure that would work. put it after the disabled submit button.

Sorry, should this go <input name="upload" type="Submit" id="upload" value="Upload" onclick="showProgress();" onclick="this.disabled=true;" HERE>? I don't understand.

Link to comment
Share on other sites

like this:

 

<input name="upload" type="Submit" id="upload" value="Upload" onclick="showProgress(); this.disabled=true; this.submit()">

 

i think, i am not to sure, javascript isnt my strong.

It disables it, but still doesn't submit.. :( This is what's been happening..

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.