Solved

How can i extract image original name from URL?

Posted on 2016-10-08
9
58 Views
Last Modified: 2016-11-11
I'm a beginner,my question is how how can i extract image name from URL. lets say an Image in this form "https://media.8ch.net/file_store/dba2bf277a91c75d94e48d47e03024784decb09b385fc3a4b8 81e32e4bc1c727.jpg"
how can i get it's original name ?
0
Comment
Question by:marcel sith
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 41835560
Use http://php.net/manual/en/function.basename.php function:

$filename = basename("https://media.8ch.net/file_store/dba2bf277a91c75d94e48d47e03024784decb09b385fc3a4b8 81e32e4bc1c727.jpg");

Open in new window

0
 
LVL 6

Expert Comment

by:huacat
ID: 41835586
Basename just extract the stored file name of that file.
The orginal file name is different, I think Marcel want to know the file name like before stored at server.

for example, the 8ch.net page show codes like below:

<a download="14449883_253103011752100_8604454551979855724_n.jpg" href="https://8ch.net/file_store/d141f83bf107454f5bd83b40b334fdaf3d001e696403f919bf1075f9b53309c9.jpg" title="Save as original filename (14449883_253103011752100_8604454551979855724_n.jpg)">14449883_253103011752100_8….jpg</a>

Open in new window


The file URL is https://8ch.net/file_store/d141f83bf107454f5bd83b40b334fdaf3d001e696403f919bf1075f9b53309c9.jpg but the orginal file name is 14449883_253103011752100_8604454551979855724_n.jpg

If you want to get the original file name, you need to grab the html page and parse the links.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 41835594
basename extract the filename from the given string: it's up to you to pass the correct string.
0
 

Author Comment

by:marcel sith
ID: 41835596
huacat
That's exactly what i want, but excuse me, this would sound pretty amateur, but how can i parse the links ?
can you offer me any guide to the whole process ? I'm not by any means an expert in that field so i guess i need more help, and thanks for the replies
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41835767
... how can i parse the links ?
You may be able to get better help from E-E or any other online forum if you can use the SSCCE.  In this case we would need to see the URL of the web page you want to parse.  We would need to know which links you want to extract.  Once we understand that information we can probably show you how to do it.

If you're new to PHP and want to learn the language, this article can help you get started.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
0
 

Author Comment

by:marcel sith
ID: 41835789
ok guys thank you so much for the help
0
 
LVL 6

Expert Comment

by:huacat
ID: 41836357
Hi Marcel,

You need to learn the regexp and some shell scripts or a language to parse html, anyway, I give a sample below:

It parse the https://8ch.net/pol/res/7805200.html page, and list all orignal file name(post filename):
You should run below command at a Unix server or with Cygwin at Windows system.
curl -s https://8ch.net/pol/res/7805200.html | sed 's/<\/p>/<\/p>\r\n/g' | sed -n 's:.*"postfilename">\(.*\)</span>.*:\1:p' | sed 's:</span>)::g'

Open in new window


The output:
Cerj6BFWwAAf-rq.jpg large.jpg
1443849993124.jpg
1468117291744.png
SmugPlushieKek.jpg
1392571678789.jpg
1354659143392.jpg
1470529530964.gif
hillaryREKT_pepe.gif
Alpha-5.jpg
414.jpg
muslimquestioner.jpeg
1471066461424.png
votebar.png
1476067639990.jpg
kek-fly.jpg
Obama_fly21.jpg
moshi moshi.JPG
hanslaugh.webm
1476068047714s.jpg
1470901536488.jpg
pepe maga 2.jpg
Trump Stand.jpg
sweet.webm
Happy_merchant_preview.webm
__23.png
wtf_i_love_hillary_now.jpg
Yuri 114.png
Trump on Trump.webm
1476066902928.jpg
TheClintons.jpeg
kek______.jpg
KekSighting.png
nuclear-explosion-emp.jpg
stop_penis_erect_archer.gif
final solution.jpg
made of rape.jpg
1473693098196.jpg
....

If you want to process all pages, you need make a crawler/spider to grab all pages.
0
 
LVL 23

Accepted Solution

by:
DanCh99 earned 500 total points
ID: 41836816
If, as the question suggests, you have a list of these URLs in Excel, you can use this formula to extract just the filename from the end of the string.  It assumes the URL is in A1

=IF(ISERROR(FIND("/",A1)),A1,RIGHT(A1,LEN(A1)-FIND("~",SUBSTITUTE(A1,"/","~",LEN(A1)-LEN(SUBSTITUTE(A1,"/",""))))))

It should work with strings of many different lengths, and with different numbers of subfolders, etc.
0
 
LVL 13

Expert Comment

by:frankhelk
ID: 41883549
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: DanCh99 (https:#a41836816)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
This article discusses how to create an extensible mechanism for linked drop downs.
The goal of the tutorial is to teach the user the full work flow of how to use flash media encoder to stream onto YouTube.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now