473,698 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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 2131
pbmods
5,821 Recognized Expert Expert
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
suryadithya
7 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
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
suryadithya
7 New Member
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 Recognized Expert Expert
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 Recognized Expert Expert
Check out this thread.. They are discussing pretty much the same thing and they have posted a couple of examples.
Sep 16 '07 #8
suryadithya
7 New Member
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 Recognized Expert Expert
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_global s 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_esca pe_string().
Sep 16 '07 #10

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

Similar topics

1
1578
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 http://somedomain.com/somefolder/filename.dll] into the addressbar of the IE, IE is allowing me to download that file with a save dialog box, this is working as per my requirement. but,
1
1981
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 but not in the database. Any help would be great. Here is my code, I have hard coded the file to download for testing purposes. Connect = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=;Initial Catalog=INNB;Data...
1
1165
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 webform 2) saves it in a folder outside the current application 3) you can download the file once it's uploaded The problem happens when a file is uploaded with an extension that's protected.. such as .vb, .config, .cs, etc. etc.
0
1515
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 = file.ContentLength; byte fileContent = new byte; int lastPos = file.FileName.LastIndexOf('\\');
4
2547
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 system is separate with front-end web site (2) Need to re-size user-uploaded image file to same size (3) Need to rename file name to avoid duplicate name (4) How to design directory and file structure to increase disk I/O performance (5) How to...
1
2388
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 downloading to my local C drive. After the following script runs I find a file named C:\TestShowID.txt in the user's FTP folder on the web server. Can some tell me why this happens? The local machine is a Windows XP machine. Thanks,
35
9352
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 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
1
1373
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 adding n I need to download the recently uploaded files only each time my schedular runs ingnoring the previously downloaded files as there will be huge number of already downloaded files n downloading them again is not worth. How do I acheive this...
0
1791
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. This file is kind of an executable. I also want this file to execute after download. This executable will generate some binary files. After the creation, I want these files to be uploaded back to the server. All this should happen on a single click on...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.