Well I have an incredibly strange bug that I have posted on several different forums but to no avail, so I thought I'd give phpbuilder a try...
Basically when you upload a file, every single time no matter what the outcome - ths script sees all the file's information, it see's the name, adds it to a database, it even sees the mime and size.
But for some odd reason, from time to time - the file isn't moved when move_uploaded_file is called. Whats even stranger about this, is the fact that if it does this for fileA one time and you try to upload it again, it'll work correctly the second time around.
Even thought I don't really want to show my code, since its a jumbled mess of me trying to figure out whats going wrong (Although I have removed most of the debug prints/print_rs), and uses numerous temporary bits of code (Which I had acquired from various sources to see if that bit of code was the problem).
(Note: This bug appears on both my local windows testing machine, as well as my Redhat/cPanel/php 5 box)
<?
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
//include('functions.inc.php');
include_once('header.php');
//ob_start(); Called in header.php
$user_id = $_SESSION['userid'];
$upload_location = $GLOBALS['upload_location'];
?>
<script language="JavaScript" type="text/javascript">
function put(filename) {
window.opener.document.getElementById('main').innerHTML = window.opener.document.getElementById('main').innerHTML + "<br />" + filename;
}
</script>
<?
if ($_POST['upload']) {
//$uploaddir = 'uploaded/';
//$uploadfile = $uploaddir . basename($_FILES['file']['name']);
//if(isset($_POST['upload']) && $_FILES['file']['size'] > 0)
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
if (!$_SESSION['sort-id']) {
$timestamp = time();
//Can be done with a single query, I know
$sqls = "INSERT INTO sortbyid_ref (date) VALUES ('".$timestamp."')";
$mysql->query($sqls,'add_ref');
$sqls2 = "SELECT * FROM sortbyid_ref WHERE date='".$timestamp."'";
$mysql->query($sqls2,'get_ref');
$refidq = $mysql->fetch_row('get_ref');
$_SESSION['sort-id'] = $refidq['id'];
print $_SESSION['sort-id'];
}
$sort_id = $_SESSION['sort-id'];
$fileName = mysql_real_escape_string($_FILES['file']['name']);
//$tmpName = mysql_real_escape_string($_FILES['file']['tmp_name']);
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = mysql_real_escape_string($_FILES['file']['size']);
$fileType = mysql_real_escape_string($_FILES['file']['type']);
$sort_id = mysql_real_escape_string($sort_id);
$user_id = mysql_real_escape_string($user_id);
//$fp = fopen($tmpName, 'r');
//$content = fread($fp, filesize($tmpName));
//$content = addslashes($content);
$sql = "INSERT INTO files (name, sort_id, user_id, upload_dir, data, size, mime) VALUES ('$fileName','$sort_id','$user_id','$upload_location','','$fileSize','$fileType')";
$mysql->query($sql,'add_file');
$error = mysql_error();
$mysql->query("SELECT id FROM files WHERE name = '$fileName' AND size = '$fileSize'",'find_file');
$listid = $mysql->fetch_row('find_file');
$id = $listid[id];
//fclose($fp);
//Now we're going to figure out what we call the file.. and where' we upload it to.
$new_filename = $exact_dir.$upload_location . $id . "_" . basename($fileName);
print $new_filename."<br>".$tmpName."<br>";
if (!move_uploaded_file($tmpName,$new_filename)) {
echo "Failed...";
print_r($_FILES);
return;
}
$ffileName = $url_location . $id . "_" . basename($fileName);
$putStr = "<a href=\"". $ffileName ."\">$fileName</a> <input type=\"hidden\" name=\"file\" value=\"". $id ."\" />";
?>
<script language="JavaScript" type="text/javascript">
put(' <?php echo $putStr ?>');
window.opener.document.getElementById('uploaded_files').value = '<?php print $_SESSION['sort-id']; ?>';
</script>
File successfully uploaded. <a href="window.php">Add another</a> | <a href="javascript: window.close()">Close</a>
<?
print "sort-id: ".$_SESSION['sort-id'];
} else {
echo "Possible file upload attack!\n";
}
?>
<?
}
else {
?>
<form action="" method="post" enctype="multipart/form-data">
<?php
if(!isset($_SESSION['sort-id'])) {
echo "<span id='window-warn'>!!WARNING!! The files being uploaded have no sortby id!</span><br />";
}
print "sort-id: ".$_SESSION['sort-id'];
?>
<span id="window-title">Select a file to upload.</span><br />
<label for="file">File:</label>
<input type="file" name="file" id="file" />
<input type="hidden" name="upload" id="upload" value="upload" />
<input type="hidden" name="article-id" id="article-id" value="<?php echo $article_id; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>
<?
}
//ob_end_flush(); Taken out because its called in footer.php
include_once('footer.php');
?>
Some variables:
$upload_location = '/uploaded/';
$exact_dir = getcwd();