Jump to content
aradhya

downloading files in php

Recommended Posts

i created a php form were users can upload and download the file....Everything works just fine...I am able to upload and download the file.....but the problem is.....i am not able to open the downloaded files!....only text files would open....image files are just displayed as text in the browser and on pc no other file opens other thn txt....appreciate the help....thnx...

Share this post


Link to post
Share on other sites
You need to send some headers before the file data. Look at the examples here: http://us2.php.net/manual/en/function.header.php
this is my download page<?phpinclude("database.php");if(isset($_GET['id'])){$fileid=$_GET['id'];$query="SELECT file_name,file_type,file_size,file_content FROM upload WHERE file_id='$fileid'";$result=mysql_query("$query") or die ("cannot retreive file");list($fname,$ftype,$fsize,$fcontent)=mysql_fetch_array($result);header("Content-length:$fsize");header("Content-type:$ftype");header("Content-Disposition:attachment;filename=$fname");echo $fcontent;}?>but the files I am getting are corrupted except .txt files...

Share this post


Link to post
Share on other sites

First: do a test where you echo the values of $fname, $ftype, and $fsize . If they are incorrect, you may have trouble.Next: it is good practice and may still be necessary to format your headers correctly. That means correct case:Content-Lengthand a space after the punctuationheader("Content-Length: $fsize");And quotation marks around the filename:header('Content-Disposition: attachment; filename="$fname"');

Share this post


Link to post
Share on other sites

when i echo....the values are displayed correctly....but when the files are downloaded....the download size is not proper and files are corrupted too!!!!!...

Share this post


Link to post
Share on other sites
the download size is not proper and files are corrupted too
Then it sounds like the values are not correct. The file size should be the length of the content, that's not even something you need to store in the database. Just check how long the content is.

Share this post


Link to post
Share on other sites

It might be useful to echo $fsize AND strlen($fcontent)If there is a difference, you have learned something.

Share this post


Link to post
Share on other sites

file 116365 ->this is the fsizeapplication/vnd.oasis.opendocument.text --> this is the ftype14 --> this is the strlen($fcontent)file 218247 ->this is the fsizeimage/jpeg -> type14 ->strlen($fcontent)there is something wrong with $fcontent....ryt???... :)

Share this post


Link to post
Share on other sites

It looks like it, yes. What kind of data does the file_content column hold? You probably need something like a blob type. If it's a varchar, you may have a problem.

Share this post


Link to post
Share on other sites

Add var_dump($fcontent) and post what that prints out. If the file is supposed to be 16kb, then there's something wrong when $fcontent is 14 bytes. It may have gotten inserted into the database incorrectly. It may just be a filename.

Share this post


Link to post
Share on other sites

Hey,thank you so much.There was a problem in upload,the file wasn't getting stored....You were right! Only the tmp name was getting stored.Really appreciate all the help....:)

Share this post


Link to post
Share on other sites

what are the advantages of download files from a sql database????im using file download from a file system but it loades my RAM too much =(

<?php$share='../es/es-share/';$share_bad_directories=array('','.','..','~');$share_ask='';if(isset($_POST['directory'])){foreach(explode('/',$_POST['directory']) as $u) {if(!in_array($u,$share_bad_directories)&&file_exists($share.$share_ask.$u)&&is_dir($share.$share_ask.$u))  {$share_ask.=$u.'/';}else{break;}}}$share_ask='os/unix/';if($dh=opendir($share.$share_ask)){while(($u=readdir($dh))!==false) {if($u!='.'&&$u!='..')  {if(is_dir($share.$share_ask.$u)){$share_ds[]=$u;}   elseif(is_file($share.$share_ask.$u)){$share_fs[]=$u;}}}}unset($share_fs[1]);if(isset($_POST['file'])&&isset($share_fs)&&in_array($_POST['file'],$share_fs)){header('Content-Type: application/octet-stream;'); header('Content-Description: File Transfer;'); header('Content-Transfer-Encoding: binary;'); header('Content-Disposition: attachment; filename="'.$_POST['file'].'"'); header('Content-Length: '.filesize($share.$share_ask.$_POST['file'])); if($fo=fopen($share.$share_ask.$_POST['file'],'r')) {while(!feof($fo))  {print(fread($fo,8192));   flush();   if(connection_status()!=0){fclose($fo);die();}  }  @fclose($fo);  die(); }}$share_types=array('rar'=>'archive','zip'=>'archive','tar'=>'archive','gz'=>'archive','tgz'=>'archive','bz'=>'archive','bz2'=>'archive','mp3'=>'audio','wma'=>'audio','ogg'=>'audio','wav'=>'audio','wmv'=>'video','pdf'=>'book','djvu'=>'book','iso'=>'disk','exe'=>'exe','jpeg'=>'image','jpe'=>'image','gif'=>'image','png'=>'image','bmp'=>'image','doc'=>'office','docx'=>'office','avi'=>'video','mpg'=>'video','mpeg'=>'video','mov'=>'video',);$css='table#list {width:100%;}table#list tr th {font-size:20px;}table#list tr td.icon {height:36px;width:36px;}table#list tr td.icon img{height:36px;width:36px;vertical-align:bottom;}table#list tr td.name {width:200px;padding-left:10px;}table#list tr td.type {width:75px;text-align:center;}table#list tr td.size {width:75px;}table#list tr td.modified {width:150px;}';$body='<h2>Share</h2><table id="list"><form id="share"><tr><th>Icon</th><th>Name</th><th>Type</th><th>Size</th><th>Modified</th></tr>';if(isset($share_ds)){foreach($share_ds as $u) {$body.='<tr><td class="icon"><img src="/source/share/dir.png" /></td><td class="name">'.((strlen($u)<22)?$u:substr($u,0,22).'...').'</td><td class="type">dir</td><td class="size"></td><td class="modified"></td></tr>';}}if(isset($share_fs)){foreach($share_fs as $u) {$body.='<tr><td class="icon"><img src="/source/share/'.((end(explode('.',$u))!=$u&&array_key_exists(end(explode('.',$u)),$share_types))?($share_types[end(explode('.',$u))]):('file')).'.png" /></td><td class="name">'.((strlen($u)<22)?$u:substr($u,0,22).'...').'</td><td class="type">'.((end(explode('.',$u))!=$u)?(end(explode('.',$u))):('file')).'</td><td class="size"></td><td class="modified"></td></tr>';}}$body.='</table>';?>

moreover function explode()ALLWAYS gives me out

Strict Standards: Only variables should be passed by reference in /usr/local/www/data/body/act/share.php on line 86

whats wrong with$u='a NORMAL file name.jpg';end(explode('.',$u))??????

Share this post


Link to post
Share on other sites

The end functions takes an array reference as a parameter, and you are passing the output of explode. The output of explode is not a variable, so that's why you get that message. That would go away if you saved the return value from explode before using end.

Share this post


Link to post
Share on other sites

As the online manual very clearly explains:

This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Share this post


Link to post
Share on other sites

If you're trying to get the file extension, use array_pop instead of end. The array is still passed by reference, but I don't get that error when I use it with the return value from explode. Maybe I just don't have strict notices on though.

Share this post


Link to post
Share on other sites

Why NOT assign it to a variable? Except when I'm chaining object references, I really don't like terse code.(Okay, maybe a few other exceptions, like everyday tasks that everyone recognizes instantly and never need debugging.)Since debugging is the whole problem in this case . . .

Share this post


Link to post
Share on other sites
if(isset($share_fs)){foreach($share_fs as $u) { $unit['name']=((strlen($u)<25)?($u):(substr($u,0,22).'...'));  $unit['type']=((current(array_slice(explode('.',$u),-1))!=$u)?(current(array_slice(explode('.',$u),-1))):('file'));  $unit['icon']=((array_key_exists($unit['type'],$share_types))?('/source/share/'.$share_types[$unit['type']].'.png'):('/source/share/file.png'));  $unit['size']=shell_exec('du -h \''.$share.$share_ask.$u.'\' | grep \''.$share.$share_ask.$u.'$\' | awk \'{print $1;}\'');  $unit['modified']=date('Y/n/j H:i:s',filemtime($share.$share_ask.$u));  $body.='<tr class="unit"><td class="icon"><img src="'.$unit['icon'].'" /></td><td class="name">'.$unit['name'].'</td><td class="type">'.$unit['type'].'</td><td class="size">'.$unit['size'].'</td><td class="modified">'.$unit['modified'].'</td></tr>';}}

$unit['type']=((current(array_slice(explode('.',$u),-1))!=$u)?(current(array_slice(explode('.',$u),-1))):)'file'));

Edited by es131245

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...