Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Upload to MYSQL, some files don't upload properly 1

Status
Not open for further replies.

laurin1

MIS
Apr 28, 2003
77
US
I can upload .PDF and HTML files just fine, but when I try to upload anything else (.jpg, .txt, .xls), the file gets messed up. When I download the file, it's damaged and won't open properly.

Help!
 
CORRECTION:

I just checked the files using Query Browser and the file downloads fine from there. So the file uploads correctly and is stored in the database correctly. The problem must be in my download code:

Code:
<?php session_start();
require_once('connections/local.php');
require_once('library/common.php');

if(isset($sFileID)){

	$sql 	= "SELECT name, type, size, file FROM files WHERE id=" . $sFileID;
	$result 	= mysql_query($sql) or die('Error, query failed');
	$array 	= mysql_fetch_array($result);
	
	header("Content-length: " . $array['size']);
	header("Content-type: " . $array['type']);
	header("Content-Disposition: attachment; filename=" . $array['name']);
	echo $array['file'];
		
	}

?>
 
try adding
Code:
set_magic_quotes_runtime(0);

to the start of your code. i'm surprised that some types of binary file work and that txt files do not. i'd also suspect that the problem is in your upload script rather than download but checking magic quotes is a good starting point.
 
I tried this, but they still don't work. I'm certain it's not the upload, as I can download the files from the MySQL Query Browser just fine.

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
try forcing the mimetype to application/octet-stream
 
How do I do that?

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
change
Code:
header("Content-type: " . $array['type']);
to
Code:
header("Content-type: application/octet-stream");
 
Ok, that's what I thought. I tried that and it has no effect. PDF and text documents work fine, everything other file type does not download correctly.

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
ok try this script instead (i'm assuming that you are populating the $sFileID somehow outside the script):

Code:
session_start();
require_once('connections/local.php');
require_once('library/common.php');

if(isset($sFileID)){

    $sql     = "SELECT name, type, size, file FROM files WHERE id=" . $sFileID;
    $result     = mysql_query($sql) or die('Error, query failed');
    $array     = mysql_fetch_array($result);
    header("Content-Length: " . strlen($array['file'])  );
    header("Content-Type: application/octet-stream");
    header('Content-Disposition: attachment; filename="'.$array['name'].'"');
header("Content-Transfer-Encoding: binary\n");
echo $array['file'];
}
 
That did not work either. I just checked a bitmap I uploaded and the file size of the one I downloaded is 4 bytes larger than the one I uploaded. What could it be adding?

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Oh, and I looked in the MySQL table and the file size is correct there. When I download it from the Query Browser the file size is also correct. Something is being added upon download.

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Also, I did this to the code and it reports the right number:

Code:
<?php 
session_start();
require_once('connections/local.php');
require_once('library/common.php');

if(isset($sFileID)){

	$sql		= "SELECT name, type, size, file FROM files WHERE id=" . $sFileID;
	$result	= mysql_query($sql) or die('Error, query failed');
	$array	= mysql_fetch_array($result);

	echo strlen($array['file']);

	}

?>

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Keith

i can't see why there should be errors but you have not provided your upload script or form definition. the fact that another php application (like phpmyadmin) may work is not necessarily conclusive that the file upload is not to blame.

in any event, i have written an upload, download and management script and posted it to my webserver for you to try. the source code is also present if you want to recreate the applet on your own servers.


Justin
 
I disagree. If the file size is correct in MySQL, but is not after download via a script, I believe that the upload is not to blame. Here is my upload script:

Code:
elseif($cmdSubmit == 'Attach'){
		
		if($_FILES['Attach']['size'] > 0) {
			
			$sFileName 	= $_FILES['Attach']['name'];
			$sTmpName  	= $_FILES['Attach']['tmp_name'];
			$sFileSize 	= $_FILES['Attach']['size'];
			$sFileType 	= $_FILES['Attach']['type'];
			$fp			= fopen($sTmpName, 'r');
			$sContent		= fread($fp, filesize($sTmpName));
			$sContent		= addslashes($sContent);
			fclose($fp);
			$sFileName 	= addslashes($sFileName);
			
			$sql 	= "SELECT * FROM files WHERE name='" . $sFileName . "' AND caseid=" . $id;
			$result	= mysql_query($sql) or die('SQL to find same file names failed');
			
			if(mysql_num_rows($result) > 0){
			
				$sql = "DELETE FROM files WHERE name='" . $sFileName . "' AND caseid=" . $id;
				mysql_query($sql) or die('Deletion of duplicate file failed');
				
				}
				
			$sql 	= "INSERT INTO files SET name='" . $sFileName . "', size='" . $sFileSize . "', type='"
					. $sFileType . "', modified_date=NULL, create_date=NULL, file='" . $sContent 
					. "', caseid='" . $id . "'";
			mysql_query($sql) or die('Upload Failed');
		
			}
			
		elseif($_FILES['name'] != '') echo 'Upload Failed';
	
		}

Code:
			<FORM ENCTYPE="MULTIPART/FORM-DATA" METHOD="POST" ACTION="<?=$_SERVER['PHP_SELF']?>" NAME="frmFiles">
				<INPUT TYPE="HIDDEN" NAME="MAX_FILE_SIZE" VALUE="5000000">
				<INPUT TYPE="HIDDEN" NAME="id" VALUE="<?=$id?>">
				<INPUT TYPE="FILE" NAME="Attach">
				<INPUT TYPE="SUBMIT" NAME="cmdSubmit" VALUE="Attach">
			</FORM>

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
i still think it is your upload script.

try changing these lines
Code:
$fp            = fopen($sTmpName, 'r');
$sContent        = fread($fp, filesize($sTmpName));
$sContent        = addslashes($sContent);
fclose($fp);
to either
Code:
$fp            = fopen($sTmpName, 'rb'); //make binary safe
$sContent        = fread($fp, filesize($sTmpName));
$sContent        = addslashes($sContent);
fclose($fp);
or
Code:
$sContent        = file_get_contents($sTmpName);
$sContent        = addslashes($sContent);

i recomment mysql_escape_string over addslashes and mysql_real_escape_string over the former.

 
I just tried both methods you described and with mysql_real_escape_string and that did not solve my problem.

I just copied the code from your server, which works fine on your server, but it does not work on my server (same problem.) Maybe it's something in my server configuration.

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Ok. I'm a little closer to a solution. I was able to open one of the downloaded files in word and could read some errors at the top (php errors.) I added

Code:
 error_reporting(0);

to your code and the page you created works on my server. My code is still not working, but I'm willing to bet it's the same problem. It seems when I'm downloading the file, the error messages are being addded to the file, causing it to contain invalid data.

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Ok, it wasn't the errors on my download.php, it was common.php. It was sticking some data at the top of the file. I removed it and whala!!!

I should have caught that.

Thanks everyone for all your help, especially you, jpadie!

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top