alex3 Posted August 10, 2009 Share Posted August 10, 2009 I have URL shortening service built that I've combined with a file upload service to give users short URLS (domain.com/xyz12) for their uploaded files. This is fine for shortening URLS, but for the locally hosted files (i.e. the uploaded files) the upload script redirects to the file, showing the real file location (domain.com/files/username/file.ext) which is very undesirable, as you can imagine. I've got the rewrite engine redirecting the short URLs to a redirect.php file, which uses the alias (the short part, xyz12) to retrieve the real URL from a MySQL database and redirect to this real URL. redirect.php: <?php // Get the DB connection details and some functions require_once("includes/config.php"); require_once("includes/functions.php"); // Custom connect to db function db_connect(); $alias = trim(mysql_real_escape_string($_GET['alias'])); if (!preg_match("/^[a-zA-Z0-9_-]+$/", $alias)) { header("Location: ".SITE_URL, true, 301); exit(); } // The function get_url is what retrieves the actual URL based on the alias if (($url = get_url($alias))) { header("Location: $url", true, 301); exit(); } header("Location: ".SITE_URL, true, 301); My problem is that I don't know how to hide the location of local files. I can think of a way to start this (I mean, I can easily detect whether the proper URL is a local one) but I can't think of a way to keep this hidden, only showing the alias. This link shows the desired effect, no iframes or anything. Quote Link to comment https://forums.phpfreaks.com/topic/169670-url-shortening-service/ Share on other sites More sharing options...
GingerRobot Posted August 10, 2009 Share Posted August 10, 2009 You could use PHP's file-handling functions to read the file contents and output it to the browser. That way, the user never sees the original file. Quote Link to comment https://forums.phpfreaks.com/topic/169670-url-shortening-service/#findComment-895114 Share on other sites More sharing options...
alex3 Posted August 10, 2009 Author Share Posted August 10, 2009 That sounds very promising indeed! Just a a couple of things: Can PHP deal with any file? If a non-browser displayable file was retrieved (a .zip, say) would that cause problems? Quote Link to comment https://forums.phpfreaks.com/topic/169670-url-shortening-service/#findComment-895130 Share on other sites More sharing options...
oni-kun Posted August 10, 2009 Share Posted August 10, 2009 Use something such as this instead to ensure the user is prompted with a download on a file.. <?php header('Content-type: application/zip'); // It will be called downloaded.zip.. if needed. header('Content-Disposition: attachment; filename="downloaded.zip"'); // Source of original file.. readfile('./files/user/original324_3423.zip'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/169670-url-shortening-service/#findComment-895172 Share on other sites More sharing options...
alex3 Posted August 11, 2009 Author Share Posted August 11, 2009 I would assume though that page the user goes to, if viewing a picture say, would be redirect.php. I was thinking you could create a new file on the fly called what ever the alias is, but that seems a very inefficient way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/169670-url-shortening-service/#findComment-895415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.