Shortening file names downloaded from webpage
Hello, I am maintaining a module FeedAPI ImageGrabber. It downloads a relevant image from a webpage of the feed-item and stores it along with the node. I found that the module sometimes gave the 'filename too long error' ( http://drupal.org/node/505964). It happens because I was saving the image file with the name as basename($image_url). But to solve the issue, I am now trying this code to shorten the filename. $arr = parse_url($image_url); $filename = basename($arr['path']); This code solves the problem by removing the query and fragments of the url. Does anyone know of any better way to handle this? Can anyone point out any issues with this code? Regards, Nitin Kumar Gupta http://publicmind.in/blog
Why not just md5() the full URL and store that instead. i.e. $filename = md5($arr['path']); 2009/9/14 nitin gupta <nitingupta.iitg@gmail.com>
Hello, I am maintaining a module FeedAPI ImageGrabber. It downloads a relevant image from a webpage of the feed-item and stores it along with the node. I found that the module sometimes gave the 'filename too long error' ( http://drupal.org/node/505964). It happens because I was saving the image file with the name as basename($image_url).
But to solve the issue, I am now trying this code to shorten the filename.
$arr = parse_url($image_url); $filename = basename($arr['path']);
This code solves the problem by removing the query and fragments of the url. Does anyone know of any better way to handle this? Can anyone point out any issues with this code?
Regards, Nitin Kumar Gupta http://publicmind.in/blog
Quoting nitin gupta <nitingupta.iitg@gmail.com>:
$arr = parse_url($image_url); $filename = basename($arr['path']);
This code solves the problem by removing the query and fragments of the url. Does anyone know of any better way to handle this? Can anyone point out any issues with this code?
I think this is the better way. I don't find anything in the Drupal API to help. -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
I believe the problem Nitin had was around long file names and that approach doesn't guarantee a file name that is within a certain length, where as the md5 does. I agree that his approach using those two lines is a nicer cleaner approach if you don't have to worry about file name length. Stew 2009/9/15 Earnie Boyd <earnie@users.sourceforge.net>
Quoting nitin gupta <nitingupta.iitg@gmail.com>:
$arr = parse_url($image_url); $filename = basename($arr['path']);
This code solves the problem by removing the query and fragments of the url. Does anyone know of any better way to handle this? Can anyone point out any issues with this code?
I think this is the better way. I don't find anything in the Drupal API to help.
-- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
If uniqueness is an issue, http://us2.php.net/manual/en/function.md5-file.php may be good in conjunction with a sanitized file name. For one site with public uploads, I use [sanitized and shortened file name]-[md5 of file].[ext], which ensures duplicate files will overwrite each other but otherwise you don't have to really worry about the file names being unique. Using [md5 of file].[ext] only means much less helpful file names when downloaded and a much larger (but still small) chance of collisions. - Ken Winters Stewart Robinson wrote:
I believe the problem Nitin had was around long file names and that approach doesn't guarantee a file name that is within a certain length, where as the md5 does. I agree that his approach using those two lines is a nicer cleaner approach if you don't have to worry about file name length.
Stew
2009/9/15 Earnie Boyd <earnie@users.sourceforge.net <mailto:earnie@users.sourceforge.net>>
Quoting nitin gupta <nitingupta.iitg@gmail.com <mailto:nitingupta.iitg@gmail.com>>:
$arr = parse_url($image_url); $filename = basename($arr['path']);
This code solves the problem by removing the query and fragments of the url. Does anyone know of any better way to handle this? Can anyone point out any issues with this code?
I think this is the better way. I don't find anything in the Drupal API to help.
-- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
participants (4)
-
Earnie Boyd -
Ken Winters -
nitin gupta -
Stewart Robinson