webent Posted August 22, 2008 Share Posted August 22, 2008 Hi, I have an image path field in a csv file which includes both the image path and the image name... what I need to do is separate the two into two different strings to insert into the database... for instance, "http://images.supplier.com/products/474/794043110894.jpg" The thing is, the path is always going to be different, so I can't say, use "http://images.supplier.com/products/474/" as the path and whatever is behind it, use as the image name... Somehow I have to be able to tell it that everything after the LAST forward slash is the image name and what's left is the image path... Can anyone give me a hand with this? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Use basename('http://images.supplier.com/products/474/794043110894.jpg') to retrieve the filename. Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 You can use both these methods: <?php $path = pathinfo('http://images.supplier.com/products/474/794043110894.jpg'); echo $dir = $path['dirname']; echo $file = $path['basename']; ?> or <?php $path = 'http://images.supplier.com/products/474/794043110894.jpg'; echo $file = substr($path, strrpos($path, '/') + 1); echo $dir = substr($path, 0, strrpos($path, '/')); ?> Quote Link to comment Share on other sites More sharing options...
webent Posted August 22, 2008 Author Share Posted August 22, 2008 Yes, I believe I'll go with this method, it seems like the simplest to accomplish both parts... <?php $path = pathinfo('http://images.supplier.com/products/474/794043110894.jpg'); echo $dir = $path['dirname']; echo $file = $path['basename']; ?> Thank you both for your help, I appreciate it. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Your choice, but it's faster and simpler to use basename(). Example: <?php $path = 'http://images.supplier.com/products/474/794043110894.jpg'; $filename = basename($path); ?> Quote Link to comment Share on other sites More sharing options...
webent Posted August 22, 2008 Author Share Posted August 22, 2008 Yes, but that only gives me the image name, not the image path, I would still then have to do another function, subtracting the image name value, to get the image path... Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 Your choice, but it's faster and simpler to use basename(). I doubt it's faster then pathinfo(), as it has to do run the same string manipulation functions, but to get only the base name. While as for simplicity, in both cases you run only one function, but pathinfo() provides also the dirname. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Yes, but that only gives me the image name, not the image path, I would still then have to do another function, subtracting the image name value, to get the image path... $path does contains the image path, "http://images.supplier.com/products/474/" would be the directory of the image in question. But yeah, I see what you mean. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted August 22, 2008 Share Posted August 22, 2008 Your choice, but it's faster and simpler to use basename(). I doubt it's faster then pathinfo(), as it has to do run the same string manipulation functions, but to get only the base name. While as for simplicity, in both cases you run only one function, but pathinfo() provides also the dirname. I see, didn't get he needed the image directory. Quote Link to comment 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.