Solved

How can i extract image original name from URL?

Posted on 2016-10-08
9
64 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 31

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 31

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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
 
LVL 109

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:
Danny Child 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 14

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

829 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