...

View Full Version : File not uploading



LearningCoder
06-21-2012, 04:54 PM
Hello,

I had my site working perfectly and decided to upload it to my free domain to test it out. Had issues with the file size's. I took it offline and carried on working on it.

There seems to be issues with the code now. The information is being sent to the database. But the filesize is saying 0kb and nothing is being uploaded to the 'files' directory. I didn't change anything with regards to the directory destination when moving the file. I've been sat here for about 4 hours now trying different things and trying to figure it out on my own but I just cannot.

Looking for a bit of guidance from you guys.

Here is my index.php:


<?php
ini_set('post_max_size', '20M');
ini_set('upload_max_filesize', '20M');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Deus Ex Demo Uploader</title>
<link rel="stylesheet" type="text/css" href="styles/styling.css" />
<script type="text/javascript" src="scripts/lab.js"></script>
</head>

<body>
<div id="container">
<div id="headerDiv">
<img src="images/dxmp111.png" alt="dxdu logo" title="DXDU Logo" id="logo" border="0" />
</div>
<h3>Welcome to the Deus Ex Demo Upload/Download Site. This is was designed to allow the players of Deus Ex to upload and download their demos
in hope people will be interested in looking at other peoples styles.</h3>

<h2 class="floatL">UPLOAD FORM:</h2>
<div id="formDiv">
<?php
$form = "<form action='index.php' method='POST' enctype='multipart/form-data'>
<table>
<tr>
<th>Demo Title:</th>
<td><input type='text' name='title' /><input type='hidden' name='MAX_FILE_SIZE' value='10240' /></td>
</tr>
<tr>
<th>Description:</th>
<td><textarea name='description' cols='28' rows='5' maxlength='170'></textarea></td>
</tr>
<tr>
<th>Browse:</th>
<td><input type='file' name='myfile' /></td>
</tr>
<tr>
<th>Upload:</th>
<td><input type='submit' name='submitbutton' value='Submit' /></td>
</tr>

</table>
</form>";

function check_input($data) {
$illegalChars = array('!','@','#','$','%','^','&','*','(',')','+','=','-','[',']','.',';',',','/','{','}','|','"',':','<','>','?');
$data = str_replace($illegalChars,'',$data);

$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
$data = mysql_real_escape_string($data);
return $data;
}

if(isset($_POST['submitbutton'])){

$title = check_input($_POST['title']);
$description = check_input($_POST['description']);
$hidden = $_POST['MAX_FILE_SIZE'];

$name = $_FILES['myfile']['name'];
$type = $_FILES['myfile']['type'];
$size = $_FILES['myfile']['size'];
$tmpname = $_FILES['myfile']['tmp_name'];
$date = date("d/m/Y");
$ext = substr($name, strpos($name, '.'));

if($ext != ".dem"){
echo "$form.<span class='marginL'>You cannot upload that file type...Please try again.</span>";
}
else{
if($name){

if($title && $description){

if(move_uploaded_file($tmpname, "files/"."$name")){

require("connect.php");
$query = mysql_query("INSERT INTO demos VALUES ('', '$name','$title', '$description','$size', '$date')");
echo "$form.<span class='marginL'>Your file has been successfully uploaded to the database.</span>";

}
else{
echo "$form.<span class='marginL'>Your file was not uploaded to the database.</span>";
}
}
else{
echo "$form.<span class='marginL'>You did not fill in the form completely...Please try again.</span>";
}
}
else{
echo "$form.<span class='marginL'>You did not select a file...</span>";
}
}
}
else{
echo "$form";
}?>
</div>
<iframe width="450" height="260" id="vid" src="http://www.youtube.com/embed/xP4_0z2M85Q" frameborder="0" allowfullscreen></iframe>
<hr id="hrule" size="5" noshade="noshade" />

<h2>DOWNLOADS:</h2>

<div id="results">
<table id="resultsTable">
<tr>
<th>Demo Name:</th>
<th>Description:</th>
<th>File Size:</th>
<th>Upload Date:</th>
</tr>
<?php
require("connect.php");
$qry = mysql_query("SELECT * FROM demos");

while($row = mysql_fetch_array($qry)){
echo '<tr><td><a href="download.php?id='.urlencode($row['id']).'&name='.urlencode($row['name']).'">'.$row['name'].'</a></td><td> '.$row['description'].'</td><td> '.$row['size'].'kb</td><td> '.$row['date'].'</td></tr>';
}
?>
</table>
</div>

<h4>Author:<span>Labtec</span></h4>
<div id="clanDiv">
<a href="http://z13.invisionfree.com/datacollective/index.php?" id="clan" target="_blank">[DC]Data Collectives Clan Forum</a>
</div>
</div>

</body>
</html>


Here is my connect.php:


<?php

$con = mysql_connect("localhost", "root", "");
if(!$con){
die ("<span class='marginL'>Error: Could not connect to server.</span>");
header("Location: index.php");
}

$db = mysql_select_db("files");
if(!$db){
die("<span class='marginL'>Error: Could not connect to database.</span>");
header("Location: index.php");
}
?>


I believe the issue lies somewhere either with the $_FILES global because the move_uploaded_file() function within the if statement never returns true.

Thanks in advance for any help.

Regards,

LC.

Fou-Lu
06-21-2012, 07:19 PM
You have not done anything to check for a valid upload. You need to determine if its suffered an error (expand switch as necessary to show the error you want using this guide: http://ca.php.net/manual/en/features.file-upload.errors.php):


switch ($_FILES['myfile']['error'])
{
case UPLOAD_ERR_OK:
// This is the only valid condition.
break;
default:
print 'An error has occurred during the upload.';
}


Error MUST be checked before moving anything on an uploaded file. The link I have describes each error condition, and you can look into that to find out which one it is. Must be 0 to be valid.
Could be that max input size. The HTML is constraining it as 10KB which isn't very big when compared to your post file size which is 20MB.

DrDOS
06-21-2012, 07:23 PM
First, make sure your files folder has complete read/write permission. Second, if you're working on file uploading, I would suggest that you always have a big fat 'print_r($_FILES);' statement right below the isset statement. Also, there is a big discrepancy between your file size statements.

LearningCoder
06-21-2012, 09:31 PM
You have not done anything to check for a valid upload. You need to determine if its suffered an error (expand switch as necessary to show the error you want using this guide: http://ca.php.net/manual/en/features.file-upload.errors.php):


switch ($_FILES['myfile']['error'])
{
case UPLOAD_ERR_OK:
// This is the only valid condition.
break;
default:
print 'An error has occurred during the upload.';
}


Error MUST be checked before moving anything on an uploaded file. The link I have describes each error condition, and you can look into that to find out which one it is. Must be 0 to be valid.
Could be that max input size. The HTML is constraining it as 10KB which isn't very big when compared to your post file size which is 20MB.

The link you gave doesn't work bud.

So within the switch statement, if the condition returns 0, that means the file is....the correct file size, or the correct type etc?

switch statements are neater to me, could you elaborate a little on what you mean with checking for all errors?

Regards,

LC.:thumbsup:

LearningCoder
06-21-2012, 09:33 PM
First, make sure your files folder has complete read/write permission. Second, if you're working on file uploading, I would suggest that you always have a big fat 'print_r($_FILES);' statement right below the isset statement. Also, there is a big discrepancy between your file size statements.

Which file size statements? Between the ini_set() statements at the very beginning and the hidden <input> tag within the form?

Regards,

LC.

Fou-Lu
06-22-2012, 12:18 AM
The forums didn't interpret the characters properly for the url match and replace. Remove the ending ): from it.

0 means that the file uploaded without error. It contains no other information.

LearningCoder
06-22-2012, 12:23 AM
Would I make cases for all these possible errors?

Regards,

LC.

Fou-Lu
06-22-2012, 12:26 AM
If you want to display specific errors, then yep. I put the default case in there as a generic one.

LearningCoder
06-22-2012, 12:36 AM
I think it would be good at the moment to make cases for all to see exactly what's going on.

Where would I put this statement, before I do $name = $_FILES['myfile']['name']; etc etc, or do I do it just before?

Thanks for the help.

Regards,

LC.

LearningCoder
06-22-2012, 12:54 AM
Ok, managed to get it working. It's returning 2, which means:

Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

I have changed the value to 163840(kb) which is 20MB. It doesn't seem to be solving the problem and the switch statement is still returning number 2.

Regards,

LC.

Fou-Lu
06-22-2012, 12:57 AM
I would also recommend all cases.
There are a few different paths you can choose. Given the nature of the check, it should be the first thing you do. There is no point in evaluating anything else within it if the end result is you can't move it anyway. You can choose from there if you want to block everything within the case statement, or if you would like to assign a variable and use it outside of the switch.


switch ($_FILES['uploadfile']['error'])
{
case UPLOAD_ERR_OK:
// do all stuff here
break;
}


For an example. I'd probably assign a variable or call a function and trigger a warning instead as I like to keep switches as slim as possible.


As for your last post, that's still not right. Its in bytes, not bits, so max file size of 20MB would be 20971520. Best I know it doesn't deal with shorthand so you have to put the full value in.

LearningCoder
06-22-2012, 01:04 AM
So for instance I could do:



switch($_FILES['myfile']['error']){
case 0: $var = getFileInfo(); break;
return $var;
}




function getFileInfo(){
$name = $_FILES['myfile']['name'];
$size = $_FILES['myfile']['size'];
//etc etc etc
}


Is that what you mean?

Regards,

LC.

Fou-Lu
06-22-2012, 01:13 AM
$_FILES is superglobal, but I'd never write a function to utilize it. I'd instead accept the array parameter and use that. This is mostly due to how I write generic code though, so its certainly not required.
The idea is right, but the implementation wrong. You'd have to return something from getFileInfo of course, but the switch shouldn't issue a return.

LearningCoder
06-22-2012, 01:22 AM
What do you mean by accepting the array parameter? So with the switch I use it to check for any errors, if no errors, I should do all the stuff that my getFileInfo() function would do within case 0?

Really trying to grasp what you mean, sorry if I still haven't.


Edit: when you say accept the array parameter, you mean like set a variable like this:
$var = $_FILES;?


Regards,

LC.

LearningCoder
06-22-2012, 03:12 AM
Has my 'MAX_FILE_SIZE' input value have to be the same as post_max_size and upload_max_filesize in my php.ini?

Does the hidden input value automatically reject any files above that value or do I still have to get the value for example:



if($size > $_POST['MAX_FILE_SIZE']){
//reject file?
}



Regards,

LC.

Fou-Lu
06-22-2012, 04:20 PM
For the array:


function dostuffs(array $aFiles)
{
print isset($aFiles['anentry']['name']) ? $aFiles['anentry']['name'] : '';
}
// called with
doStuffs($_FILES);

Would suffice. Can be a reference although I see no need to do so.
MAX_FILE_SIZE must be <= upload_max_filesize, and post_max_size must be > upload_max_filesize.
The hidden field although client controlled and cannot be relied on will issue an error code 2 if it exceeds. It is also rejected client end first if I'm not mistaken which makes the check faster. Don't use it in any comparisons, use a variable to do so or rely on the ini settings instead. MAX_FILE_SIZE from your post is easily alterable.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum