473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Downloading Uploaded files under a different name than the one on the server

Hi,
I am very new to php. And I have got to submit a project very soon (By tomorrow evening). So please help.
The problem is this. I have created a file upload mechanism where the uploaded files are stored under a different id for security something like 0001.pdf, 0002.jpg etc.. The original names are stored in a mysql database. Now I have a file download page. The links are the original names of the files like user1.pdf, user2.pdf... When the link is clicked the file that comes up has the name 0001,0002 etc ie the one on server.
I want it to open as user1.pdf, user2.pdf etc that is under the original name in which they were uploaded.. How to do this?
Sep 16 '07 #1
14 2111
pbmods
5,821 Expert 4TB
Heya, suryadithya. Welcome to TSDN!

You'd need to create a PHP script that outputs the images directly and then make the PHP script the src of the image tag.

For an example, check out this article. Some of the reading is a bit heavy, but you should find the concepts and syntax that you're looking for about halfway down the page.
Sep 16 '07 #2
Heya, suryadithya. Welcome to TSDN!

You'd need to create a PHP script that outputs the images directly and then make the PHP script the src of the image tag.

For an example, check out this article. Some of the reading is a bit heavy, but you should find the concepts and syntax that you're looking for about halfway down the page.
Thank you for that reply pbmods. But the article was very heavy for me. And I don't think that solved my problem. I have allowed upload of several file types not just images. They are not stored under their original names anywhere on the server. The only source of the names is the mysql db.
What I want is when the user clicks on "save as" the file should be saved under it's original name on the db. Not the one on the server.
Sep 16 '07 #3
pbmods
5,821 Expert 4TB
Heya, Suryadithya.

Hm. So you need to be able to load any file by using an alias.

Expand|Select|Wrap|Line Numbers
  1. <a href="getFile.php?file=0001.pdf" />
  2.  
getFile.php would then connect to the database and load the file:
Expand|Select|Wrap|Line Numbers
  1. $_file = mysql_real_escape_string($_GET['file']);
  2. $_sql = "
  3. SELECT
  4.         *
  5.     FROM
  6.         `Ext_Uploads`
  7.     WHERE
  8.         `file` = '{$_file}'
  9.     LIMIT 1";
  10. .
  11. .
  12. .
  13.  
That's the easy stuff; I'm sure I'm just repeating what you've already got working so far

The tricky part is to output the file, you need to know the Mime-type of the file. By default, the Content-type header will be set to 'text/html', so the browser won't display the file properly.

You could add a switch that matches off the file extension:
Expand|Select|Wrap|Line Numbers
  1. $_fileName = $mysql_row['realFileName'];
  2. switch( strtolower(substr($_fileName, 0, strrpos($_fileName, '.'))) )
  3. {
  4.     case 'jpg':
  5.     case 'jpeg':
  6.         $type = 'image/jpeg';
  7.     break;
  8.  
  9.     case 'pdf':
  10.         $type = 'application/pdf';
  11.     break;
  12.  
  13.     .
  14.     .
  15.     .
  16. }
  17.  
  18. header('Content-type: ' . $type);
  19.  
After all that, actually outputting the file is quite easy:
Expand|Select|Wrap|Line Numbers
  1. readfile($_fileName);
  2.  
The readfile() function opens a file and sends its content to the output buffer (outputs it to the browser).
Sep 16 '07 #4
Atli
5,058 Expert 4TB
Hi.

Additionally, if you want to change the name (most) browsers will download the file as, you can set this header aswell:
Expand|Select|Wrap|Line Numbers
  1. header("Content-Disposition: attachment; filename=\"newFileName.pdf\";" );
  2.  
Noe, that this apparently does not work with Safari. Works on pretty much everything else tho.
Sep 16 '07 #5
Thank you both..That shows me a direction..
Actually I had given only a html link to the files on server. I didn't get files using php.
But I have a doubt. I think your solution will work only if I have stored the file in the db. I have only stored the user given file names. The files are on the server. How do I load those files and connect them to the output buffer.
What is the command in php to read files from an external source? This I can then rename using your script.
Sep 16 '07 #6
pbmods
5,821 Expert 4TB
Heya, Suryadithya.

readfile() will output a file on the server; to output a file stored in the database, you would simply echo the data that you fetched from the database.
Sep 16 '07 #7
Atli
5,058 Expert 4TB
Check out this thread.. They are discussing pretty much the same thing and they have posted a couple of examples.
Sep 16 '07 #8
I think I can do it now. The switch case and content disposition should do fine.
Can you people just explain me the syntax of
Expand|Select|Wrap|Line Numbers
  1. <a href = "getfile.php?file=0001.ext">
Here my file name is itself a variable say $id.Will
Expand|Select|Wrap|Line Numbers
  1. $link="getfile.php?file=".$id;
  2. <a href ="$link">;
do fine?
And why does this go to $_GET array?
PBMODS shouldn't the last line of the code be readfile($file) because that is the name of the file on the server? ($file=0001.ext,00002.ext etc)
I assume that you meant by realfilename the user given name on db which is $_filename as per your code. How can I read it when there is no such file on server?
And thanks in advance.
Sep 16 '07 #9
pbmods
5,821 Expert 4TB
Heya, Suryadithya.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]

Unless I read your post incorrectly, you are storing the *real* file names in the database, while the value of $_GET['file'] is actually the 'alias' that you would use to look up the *real* file name in the database.

The goal is to readfile() using the file name stored in the database.

In terms of $_GET, some PHP setups have register_globals turned on, which means that any variables passed in the URL will be automagically turned into global variables. This behavior has been widely deplored for security reasons, and is in fact being discontinued in PHP 6. Using $_GET is a safer way to do it because it encourages you to validate your input before using it, for example by passing it through mysql_real_escape_string().
Sep 16 '07 #10
Thanks.I will be careful while posting codes in the future. My db has two fields. One corresponds to the id on server 0001 0002 etc and the other to user given names.
By alias I think you mean the id. Then
Expand|Select|Wrap|Line Numbers
  1. header('Content-Disposition: attachment; filename="realname.ext"');
  2. readfile('idlike0001.ext);
  3.  
should be thecode I think.You say the goal is to readfile(realname).But how is it possible when it is stored only under alias in the server.?
Sep 16 '07 #11
pbmods
5,821 Expert 4TB
Heya, Suryadithya.

In this line:
Expand|Select|Wrap|Line Numbers
  1. readfile('idlike0001.ext');
  2.  
instead of the literal 'idlike0001.ext', you'd want to use the filename you fetched from the database.

For example:
Expand|Select|Wrap|Line Numbers
  1. $_id = mysql_real_escape_string($_GET['file']);
  2. $_sql = "
  3. SELECT
  4.         `filename`
  5.     FROM
  6.         `uploads`
  7.     WHERE
  8.         `id` = '{$_id}'
  9.     LIMIT 1";
  10. $_res = mysql_query($_sql);
  11.  
  12. if( empty($_res) )
  13. {
  14.     header('HTTP/1.0 404 Not Found');
  15.     exit;
  16. }
  17.  
  18. $_data = mysql_fetch_assoc($_res);
  19. mysql_free_result($_res);
  20.  
  21. // content-type stuff goes here...
  22.  
  23. .
  24. .
  25. .
  26.  
  27. readfile('/path/to/uploads/' . $_data['filename']);
  28.  
This will pass the filename stored in the database to readfile() instead of the database ID number.
Sep 16 '07 #12
Thankyou everyone... I got it running.. But still I have a problem.I am not able to view image files like jpg,gif after downloading. The error message displays that it is not a valid gif file.. The file is viewable on the uploads folder and after downloading the properties are the same ie the file size is same. But still I can't open them in image viewer. My content type is ok. I have echoed it and checked it. And the code too must be ok since I am able to view pdf files. I am using
Expand|Select|Wrap|Line Numbers
  1. readfile("uploads/".$filename);
  2.  
The headers I have used it the same way pbmods posted. Any guess what's the problem?
Sep 18 '07 #13
pbmods
5,821 Expert 4TB
Heya, Suryadithya.

Does your script output *anything* else? Is there any whitespace before the first <?php tag? Is there anything after the final ?> tag? Any other output from your script, intentional or otherwise, will cause the image data to be invalid.
Sep 18 '07 #14
Yeah.. Thanks.. That was the problem. There was a space b4 my php tags. Now it works fine.Thanks a lot.
Sep 18 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kajol | last post by:
Hi I am facing a strange problem in downloading files from the web. CASE 1: I have an ActiveX.dll file developed using Visual Basic 6.0 and uploaded on a Linux Server. I am typing...
1
by: fibreiv | last post by:
I am trying to download files from my database that I uploaded to it. I can download bmp, txt file but have not been able to d/l pdf files. I am able to d/l pdf files stored in the file system...
1
by: Garett Rogers | last post by:
I am creating a document manager for our intranet in VB.NET and I have stumbled across a problem that I cant seem to find a solution for. Everything is working as planned: 1) upload a file from...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
4
by: RedHair | last post by:
I'd like to set up a file system for the ASP.NET 2.0 application to store user-uploaded files, since the members are more than 100,000 people, the basic requirements are as below: (1) The file...
1
by: Vic Spainhower | last post by:
Hello, I am trying to download a file to my local machine using PHP. However, when the script runs the file is placed in the folder assigned to the ftp user on the Web server instead of...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
1
by: =?Utf-8?B?U2FjaGluIENoYXZhbg==?= | last post by:
Hi, I need to download files from the FTP location. This FTP location is readonly n I dont hv access to delete files from this location once downloaded. The files to the ftp location keeps...
0
by: jannyguy | last post by:
Hi, I have a web application running on an apache server. It has a download link for downloading a file. My requirement is to download this file without asking the user where to download....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.